Skip to content

Commit ea227bb

Browse files
authored
chore: Config Updates ( TOML / JSON / YAML ) (#205)
* chore: Moving config from JSON to YAML Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * Config cleanup Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * tests Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * lint Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * fixing test Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * moves project directory to -C Signed-off-by: Lucas Fontes <lucas@cosmonic.com> --------- Signed-off-by: Lucas Fontes <lucas@cosmonic.com>
1 parent 84335ce commit ea227bb

File tree

28 files changed

+484
-437
lines changed

28 files changed

+484
-437
lines changed

.wash/config.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

.wash/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version: 2.0.0-rc.5

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ clap-markdown = { workspace = true }
4141
ctrlc = { workspace = true }
4242
dialoguer = { workspace = true, features = ["editor", "password", "zeroize", "fuzzy-select", "fuzzy-matcher"] }
4343
etcetera = { workspace = true }
44-
figment = { workspace = true, features = ["json", "env"] }
44+
figment = { workspace = true, features = ["json", "env", "yaml", "toml"] }
4545
flate2 = { workspace = true, features = ["rust_backend"] }
4646
humantime = { workspace = true }
4747
indicatif = { workspace = true }
@@ -50,6 +50,7 @@ reqwest = { workspace = true, features = ["json", "rustls-tls"] }
5050
semver = { workspace = true }
5151
serde = { workspace = true, features = ["derive"] }
5252
serde_json = { workspace = true, features = ["std"] }
53+
serde_yaml_ng = { workspace = true }
5354
tar = { workspace = true }
5455
tempfile = { workspace = true }
5556
tokio = { workspace = true, features = ["full"] }
@@ -119,6 +120,7 @@ names = { version = "0.14", default-features = false }
119120
semver = { version = "1.0.26", default-features = false }
120121
serde = { version = "1.0.219", default-features = false }
121122
serde_json = { version = "1.0.140", default-features = false }
123+
serde_yaml_ng = { version = "0.9", default-features = false }
122124
sha2 = { version = "0.10.8", default-features = false }
123125
sysinfo = { version = "0.32", default-features = false, features = ["system"] }
124126
tar = { version = "0.4", default-features = false }

crates/wash/src/cli/component_build.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,18 @@ use std::io::IsTerminal;
1414
use tokio::{fs, process::Command};
1515
use tracing::{debug, error, info, instrument, trace, warn};
1616

17-
use crate::component_build::ProjectType;
1817
use crate::plugin::bindings::wasmcloud::wash::types::HookType;
1918
use crate::wit::WitConfig;
2019
use crate::{
2120
cli::{CliCommand, CliContext, CommandOutput},
22-
config::{Config, generate_project_config, load_config, save_config},
21+
config::{Config, generate_project_config, save_config},
2322
wit::{CommonPackageArgs, WkgFetcher, load_lock_file},
2423
};
24+
use crate::{component_build::ProjectType, config::local_config_path};
2525

2626
/// CLI command for building components
2727
#[derive(Debug, Clone, Args, Serialize)]
2828
pub struct ComponentBuildCommand {
29-
/// Path to the project directory
30-
#[clap(name = "project-path", default_value = ".")]
31-
project_path: PathBuf,
32-
33-
/// Path to a configuration file in a location other than PROJECT_DIR/.wash/config.json
34-
#[clap(name = "config", long = "config")]
35-
build_config: Option<PathBuf>,
36-
3729
/// The expected path to the built Wasm component artifact
3830
#[clap(long = "component-path")]
3931
component_path: Option<PathBuf>,
@@ -57,7 +49,7 @@ impl CliCommand for ComponentBuildCommand {
5749
#[instrument(level = "debug", skip(self, ctx), name = "component_build")]
5850
async fn handle(&self, ctx: &CliContext) -> anyhow::Result<CommandOutput> {
5951
// Load configuration with CLI arguments override
60-
let mut config = load_config(&ctx.config_path(), Some(&self.project_path), None::<Config>)?;
52+
let mut config = ctx.load_config(None::<Config>)?;
6153
// Ensure the CLI argument takes precedence
6254
if let Some(wit) = config.wit.as_mut() {
6355
wit.skip_fetch = self.skip_fetch;
@@ -67,7 +59,7 @@ impl CliCommand for ComponentBuildCommand {
6759
..Default::default()
6860
})
6961
}
70-
let result = build_component(&self.project_path, ctx, &config, Some(&self.args)).await?;
62+
let result = build_component(ctx.project_dir(), ctx, &config, Some(&self.args)).await?;
7163

7264
Ok(CommandOutput::ok(
7365
format!(
@@ -77,7 +69,7 @@ impl CliCommand for ComponentBuildCommand {
7769
Some(serde_json::json!({
7870
"component_path": result.component_path,
7971
"project_type": result.project_type,
80-
"project_path": self.project_path,
72+
"project_path": result.project_path,
8173
})),
8274
))
8375
}
@@ -744,7 +736,7 @@ impl ComponentBuilder {
744736

745737
// Add WIT world - this is required for TinyGo builds when WIT is present
746738
let Some(wit_world) = &tinygo_config.wit_world else {
747-
// Generate project config to ensure .wash/config.json exists with placeholder
739+
// Generate project config to ensure .wash/config.yaml exists with placeholder
748740
let mut config_with_placeholder = config.clone();
749741
let component_path_relative = PathBuf::from("build/output.wasm");
750742

@@ -764,11 +756,7 @@ impl ComponentBuilder {
764756
}
765757

766758
// Write config with placeholder
767-
let config_dir = self.project_path.join(".wash");
768-
let config_path = config_dir.join("config.json");
769-
tokio::fs::create_dir_all(&config_dir)
770-
.await
771-
.context("failed to create .wash directory")?;
759+
let config_path = local_config_path(&self.project_path);
772760
save_config(&config_with_placeholder, &config_path).await?;
773761

774762
bail!(

crates/wash/src/cli/config.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tracing::instrument;
44

55
use crate::{
66
cli::{CliCommand, CliContext, CommandOutput},
7-
config::{generate_default_config, local_config_path},
7+
config::{Config, generate_default_config, load_config, local_config_path},
88
};
99

1010
/// Create a new component project from a template, git repository, or local path
@@ -33,11 +33,9 @@ impl CliCommand for ConfigCommand {
3333
match self {
3434
ConfigCommand::Init { force, global } => {
3535
let config_path = if *global {
36-
ctx.config_path()
36+
ctx.user_config_path()
3737
} else {
38-
local_config_path(
39-
&std::env::current_dir().context("failed to get current dir")?,
40-
)
38+
local_config_path(ctx.project_dir())
4139
};
4240

4341
generate_default_config(&config_path, *force)
@@ -48,6 +46,7 @@ impl CliCommand for ConfigCommand {
4846
"Configuration initialized successfully.".to_string(),
4947
Some(serde_json::json!({
5048
"message": "Configuration initialized successfully.",
49+
"config_path": config_path.display().to_string(),
5150
"success": true,
5251
})),
5352
))
@@ -56,29 +55,41 @@ impl CliCommand for ConfigCommand {
5655
let version = env!("CARGO_PKG_VERSION");
5756
let data_dir = ctx.data_dir().display().to_string();
5857
let cache_dir = ctx.cache_dir().display().to_string();
59-
let config_dir = ctx.config_dir().display().to_string();
60-
let config_path = ctx.config_path().display().to_string();
58+
let user_config_dir = ctx.config_dir().display().to_string();
59+
let user_config_path = ctx.user_config_path().display().to_string();
60+
let project_path = ctx.project_dir();
61+
let project_config_path = ctx.project_config_path().display().to_string();
6162

6263
Ok(CommandOutput::ok(
6364
format!(
64-
"wash version: {version}\nData directory: {data_dir}\nCache directory: {cache_dir}\nConfig directory: {config_dir}\nConfig path: {config_path}"
65+
r"
66+
wash version: {version}
67+
Data directory: {data_dir}
68+
Cache directory: {cache_dir}
69+
User Config directory: {user_config_dir}
70+
User Config path: {user_config_path}
71+
Project Config path: {project_config_path}
72+
"
6573
),
6674
Some(serde_json::json!({
6775
"version": version,
6876
"data_dir": data_dir,
6977
"cache_dir": cache_dir,
70-
"config_dir": config_dir,
71-
"config_path": config_path,
78+
"user_config_dir": user_config_dir,
79+
"user_config_path": user_config_path,
80+
"project_path": project_path,
81+
"project_config_path": project_config_path,
7282
})),
7383
))
7484
}
7585
ConfigCommand::Show {} => {
76-
let config = ctx
77-
.ensure_config(None)
78-
.await
79-
.context("failed to load config")?;
86+
let config = load_config(
87+
&ctx.user_config_path(),
88+
Some(ctx.project_dir()),
89+
None::<Config>,
90+
)?;
8091
Ok(CommandOutput::ok(
81-
serde_json::to_string_pretty(&config).context("failed to serialize config")?,
92+
serde_yaml_ng::to_string(&config).context("failed to serialize config")?,
8293
Some(serde_json::to_value(&config).context("failed to serialize config")?),
8394
))
8495
}

crates/wash/src/cli/dev.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ use crate::{
4343

4444
#[derive(Debug, Clone, Args)]
4545
pub struct DevCommand {
46-
/// The path to the project directory
47-
#[clap(name = "project-dir", default_value = ".")]
48-
pub project_dir: PathBuf,
49-
5046
/// The path to the built Wasm file to be used in development
5147
#[clap(long = "component-path")]
5248
pub component_path: Option<PathBuf>,
@@ -84,11 +80,12 @@ pub struct DevCommand {
8480

8581
impl CliCommand for DevCommand {
8682
async fn handle(&self, ctx: &CliContext) -> anyhow::Result<CommandOutput> {
87-
info!(path = ?self.project_dir, "starting development session for project");
83+
let project_dir = ctx.project_dir();
84+
info!(path = ?project_dir, "starting development session for project");
8885

8986
let config = load_config(
90-
&ctx.config_path(),
91-
Some(self.project_dir.as_path()),
87+
&ctx.user_config_path(),
88+
Some(project_dir),
9289
// Override the component path with the one provided in the command line
9390
Some(Config {
9491
build: Some(BuildConfig {
@@ -100,20 +97,8 @@ impl CliCommand for DevCommand {
10097
)
10198
.context("failed to load config for development")?;
10299

103-
// Validate project directory
104-
ensure!(
105-
self.project_dir.exists(),
106-
"Project directory does not exist: {}",
107-
self.project_dir.display()
108-
);
109-
ensure!(
110-
self.project_dir.is_dir(),
111-
"Project path is not a directory: {}",
112-
self.project_dir.display()
113-
);
114-
115100
// Check for required tools (e.g., wasmCloud, WIT)
116-
let project_context = detect_project_context(&self.project_dir)
101+
let project_context = detect_project_context(project_dir)
117102
.await
118103
.context("failed to detect project context")?;
119104
let (issues, recommendations) = check_project_specific_tools(&project_context)
@@ -137,7 +122,7 @@ impl CliCommand for DevCommand {
137122
debug!("no recommendations found for project tools");
138123
}
139124

140-
let component_path = match build_component(&self.project_dir, ctx, &config, None).await {
125+
let component_path = match build_component(project_dir, ctx, &config, None).await {
141126
// Edge case where the build was successful, but the component path in the config is different
142127
// than the one returned by the build process.
143128
Ok(build_result)
@@ -282,14 +267,15 @@ impl CliCommand for DevCommand {
282267
let mut last_build_time = SystemTime::now();
283268

284269
// Canonicalize project root once to ensure consistent path comparisons
285-
let canonical_project_root = self.project_dir.canonicalize().with_context(|| {
270+
// It should be already canonicalized, but just in case
271+
let canonical_project_root = project_dir.canonicalize().with_context(|| {
286272
format!(
287273
"failed to canonicalize project directory: {}",
288-
self.project_dir.display()
274+
project_dir.display()
289275
)
290276
})?;
291277
debug!(
292-
original = ?self.project_dir.display(),
278+
original = ?project_dir.display(),
293279
canonical = ?canonical_project_root.display(),
294280
"canonicalized project root for file watching"
295281
);
@@ -312,7 +298,7 @@ impl CliCommand for DevCommand {
312298
let ignore_paths_notify = ignore_paths.clone();
313299

314300
let canonical_project_root_notify = canonical_project_root.clone();
315-
debug!(path = ?self.project_dir.display(), "setting up watcher");
301+
debug!(path = ?project_dir.display(), "setting up watcher");
316302

317303
// Watch for changes and rebuild/deploy as needed
318304
let mut watcher = notify::recommended_watcher(move |res: _| match res {
@@ -386,7 +372,7 @@ impl CliCommand for DevCommand {
386372
info!("rebuilding component after file changed ...");
387373

388374
// Check if WIT-related files have changed since the last build
389-
let wit_changed = wit_files_changed(&self.project_dir, last_build_time);
375+
let wit_changed = wit_files_changed(project_dir, last_build_time);
390376

391377
// Create a modified config with skip_fetch set based on WIT file changes
392378
let mut rebuild_config = config.clone();
@@ -405,7 +391,7 @@ impl CliCommand for DevCommand {
405391
}
406392

407393
let rebuild_result = build_component(
408-
&self.project_dir,
394+
project_dir,
409395
ctx,
410396
&rebuild_config,
411397
None,

0 commit comments

Comments
 (0)