Skip to content

Commit 65ae43e

Browse files
committed
simple config command
1 parent c5c6218 commit 65ae43e

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

crates/wasm-pkg-common/src/config.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
collections::{hash_map::Entry, HashMap},
33
io::ErrorKind,
4-
path::Path,
4+
path::{Path, PathBuf},
55
};
66

77
use serde::{Deserialize, Serialize};
@@ -102,13 +102,18 @@ impl Config {
102102
Ok(config)
103103
}
104104

105+
/// Returns the default global config file location
106+
pub fn global_config_path() -> Option<PathBuf> {
107+
dirs::config_dir().map(|path| path.join("wasm-pkg").join("config.toml"))
108+
}
109+
105110
/// Reads config from the default global config file location
106111
pub async fn read_global_config() -> Result<Option<Self>, Error> {
107-
let Some(config_dir) = dirs::config_dir() else {
108-
return Ok(None);
112+
let path = match Config::global_config_path() {
113+
Some(path) => path,
114+
None => return Ok(None),
109115
};
110-
let path = config_dir.join("wasm-pkg").join("config.toml");
111-
let contents = match tokio::fs::read_to_string(path).await {
116+
let contents = match tokio::fs::read_to_string(&path).await {
112117
Ok(contents) => contents,
113118
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(None),
114119
Err(err) => return Err(Error::ConfigFileIoError(err)),

crates/wkg/src/main.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use wasm_pkg_client::{
1010
Client, PublishOpts,
1111
};
1212
use wasm_pkg_common::{
13+
self,
1314
config::{Config, RegistryMapping},
1415
package::PackageSpec,
1516
registry::Registry,
@@ -83,6 +84,8 @@ impl Common {
8384
#[derive(Subcommand, Debug)]
8485
#[allow(clippy::large_enum_variant)]
8586
enum Commands {
87+
/// Set registry configuration
88+
Config(ConfigArgs),
8689
/// Download a package from a registry
8790
Get(GetArgs),
8891
/// Publish a package to a registry
@@ -95,6 +98,53 @@ enum Commands {
9598
Wit(WitCommands),
9699
}
97100

101+
#[derive(Args, Debug)]
102+
struct ConfigArgs {
103+
/// The default registry domain to use. Overrides configuration file(s).
104+
#[arg(long = "default", value_name = "DEFAULT")]
105+
default: Option<Registry>,
106+
107+
#[command(flatten)]
108+
common: Common,
109+
}
110+
111+
impl ConfigArgs {
112+
pub async fn run(self) -> anyhow::Result<()> {
113+
// use config path provided, otherwise global config path
114+
let path = if let Some(path) = self.common.config {
115+
path
116+
} else {
117+
Config::global_config_path()
118+
.ok_or(anyhow::anyhow!("global config path not available"))?
119+
};
120+
121+
// read file or use default config (not empty config)
122+
let mut config = match tokio::fs::read_to_string(&path).await {
123+
Ok(contents) => Config::from_toml(&contents)?,
124+
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
125+
Err(err) => return Err(anyhow::anyhow!("error reading config file: {0}", err)),
126+
};
127+
128+
if let Some(default) = self.default {
129+
// set default registry
130+
config.set_default_registry(Some(default));
131+
132+
// write config file
133+
config.to_file(&path).await?;
134+
println!("Updated config file: {path}", path = path.display());
135+
}
136+
137+
// print config
138+
if let Some(registry) = config.default_registry() {
139+
println!("Default registry: {}", registry);
140+
} else {
141+
println!("Default registry is not set");
142+
}
143+
144+
Ok(())
145+
}
146+
}
147+
98148
#[derive(Args, Debug)]
99149
struct GetArgs {
100150
/// Output path. If this ends with a '/', a filename based on the package
@@ -312,6 +362,7 @@ async fn main() -> anyhow::Result<()> {
312362
let cli = Cli::parse();
313363

314364
match cli.command {
365+
Commands::Config(args) => args.run().await,
315366
Commands::Get(args) => args.run().await,
316367
Commands::Publish(args) => args.run().await,
317368
Commands::Oci(args) => args.run().await,

0 commit comments

Comments
 (0)