Skip to content

Commit

Permalink
fix: adjust config apply for workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dodokek authored and lowitea committed Mar 5, 2025
1 parent 17686d9 commit a22a7ad
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 34 deletions.
146 changes: 114 additions & 32 deletions src/commands/config/apply.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use derive_builder::Builder;
use log::info;
use serde::Deserialize;
use serde_yaml::Value;
use std::{
collections::HashMap,
fs,
env, fs,
io::{BufRead, BufReader, Read, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
};

const WISE_PIKE: &str = r"
________________________________________
/ You are trying to apply config from \
| custom directory, however to use this |
| flag, you must specify the plugin with |
\ --plugin-name /
----------------------------------------
o
o ______/~/~/~/__ /((
o // __ ====__ /_((
o // @)) )))) ===/__((
)) ))))))) __((
\\ \) )))) __===\ _((
\\_______________==== \_((
\((
";

fn apply_service_config(
plugin_name: &str,
plugin_version: &str,
Expand Down Expand Up @@ -74,6 +91,50 @@ fn apply_service_config(
Ok(())
}

fn apply_plugin_config(params: &Params, current_plugin_path: &str) -> Result<()> {
let cur_plugin_dir = env::current_dir()?
.join(&params.plugin_path)
.join(current_plugin_path);

let admin_socket = params
.plugin_path
.join(&params.data_dir)
.join("cluster")
.join("i1")
.join("admin.sock");

let cargo_manifest: &CargoManifest = &toml::from_str(
&fs::read_to_string(cur_plugin_dir.join("Cargo.toml"))
.context("failed to read Cargo.toml")?,
)
.context("failed to parse Cargo.toml")?;

let config: HashMap<String, HashMap<String, Value>> = serde_yaml::from_str(
&fs::read_to_string(cur_plugin_dir.join(&params.config_path)).context(format!(
"failed to read config file at {}",
params.config_path.display()
))?,
)
.context(format!(
"failed to parse config file at {} as toml",
params.config_path.display()
))?;
for (service_name, service_config) in config {
apply_service_config(
&cargo_manifest.package.name,
&cargo_manifest.package.version,
&service_name,
&service_config,
&admin_socket,
)
.context(format!(
"failed to apply service config for service {service_name}"
))?;
}

Ok(())
}

#[derive(Debug, Deserialize)]
struct Package {
name: String,
Expand All @@ -91,42 +152,63 @@ pub struct Params {
config_path: PathBuf,
#[builder(default = "PathBuf::from(\"./tmp\")")]
data_dir: PathBuf,
#[builder(default = "PathBuf::from(\"./\")")]
plugin_path: PathBuf,
plugin_name: Option<String>,
}

pub fn cmd(params: &Params) -> Result<()> {
info!("Applying plugin config...");
// If plugin name flag was specified, apply config only for
// this exact plugin
if let Some(plugin_name) = &params.plugin_name {
info!("Applying plugin config for plugin {}", plugin_name);
apply_plugin_config(params, plugin_name)?;
return Ok(());
}

let admin_socket = params
.data_dir
.join("cluster")
.join("i1")
.join("admin.sock");
let cargo_manifest: &CargoManifest =
&toml::from_str(&fs::read_to_string("Cargo.toml").context("failed to read Cargo.toml")?)
.context("failed to parse Cargo.toml")?;
let config: HashMap<String, HashMap<String, Value>> =
serde_yaml::from_str(&fs::read_to_string(&params.config_path).context(format!(
"failed to read config file at {}",
params.config_path.display()
))?)
.context(format!(
"failed to parse config file at {} as toml",
params.config_path.display()
))?;
for (service_name, service_config) in config {
apply_service_config(
&cargo_manifest.package.name,
&cargo_manifest.package.version,
&service_name,
&service_config,
&admin_socket,
)
.context(format!(
"failed to apply service config for service {service_name}"
))?;
info!("Applying plugin config in each plugin");

let root_dir = env::current_dir()?.join(&params.plugin_path);

let cargo_toml_path = root_dir.join("Cargo.toml");
let cargo_toml_content = fs::read_to_string(&cargo_toml_path).context(format!(
"Failed to read Cargo.toml in {}",
&cargo_toml_path.display()
))?;

let parsed_toml: toml::Value = cargo_toml_content
.parse()
.context("Failed to parse Cargo.toml")?;

if let Some(workspace) = parsed_toml.get("workspace") {
if params.config_path.to_str().unwrap() != "plugin_config.yaml" {
bail!(WISE_PIKE);
}

if let Some(members) = workspace.get("members") {
if let Some(members_array) = members.as_array() {
for member in members_array {
let member_str = member.as_str();
if member_str.is_none() {
continue;
}

if !root_dir
.join(member_str.unwrap())
.join("manifest.yaml.template")
.exists()
{
continue;
}
apply_plugin_config(params, member_str.unwrap())?;
}
}
}

return Ok(());
}

info!("Plugin config successfully applied.");
apply_plugin_config(params, "./")?;

Ok(())
}
2 changes: 2 additions & 0 deletions tests/assets/plugin_config_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main:
value: config1
2 changes: 2 additions & 0 deletions tests/assets/plugin_config_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main:
value: config2
Loading

0 comments on commit a22a7ad

Please sign in to comment.