Revert to "get" / "set" for settable config keys

This commit is contained in:
Felix Schütt
2022-12-21 09:02:07 +01:00
parent b89d128377
commit 139c3147d6
3 changed files with 124 additions and 101 deletions

View File

@@ -13,7 +13,7 @@ pub struct Config {
flags: Flags, flags: Flags,
/// Subcommand for `wasmer config get | set` /// Subcommand for `wasmer config get | set`
#[clap(subcommand)] #[clap(subcommand)]
set: Option<Set>, set: Option<GetOrSet>,
} }
/// Normal configuration /// Normal configuration
@@ -47,26 +47,6 @@ pub struct Flags {
#[clap(long, conflicts_with = "pkg-config")] #[clap(long, conflicts_with = "pkg-config")]
config_path: bool, config_path: bool,
/// Print the registry URL of the currently active registry
#[clap(long, conflicts_with = "pkg-config")]
registry_url: bool,
/// Print the token for the currently active registry or nothing if not logged in
#[clap(long, conflicts_with = "pkg-config")]
registry_token: bool,
/// Print whether telemetry is currently enabled
#[clap(long, conflicts_with = "pkg-config")]
telemetry_enabled: bool,
/// Print whether update notifications are enabled
#[clap(long, conflicts_with = "pkg-config")]
update_notifications_enabled: bool,
/// Print the proxy URL
#[clap(long, conflicts_with = "pkg-config")]
proxy_url: bool,
/// Outputs the necessary details for compiling /// Outputs the necessary details for compiling
/// and linking a program to Wasmer, using the `pkg-config` format. /// and linking a program to Wasmer, using the `pkg-config` format.
#[clap(long)] #[clap(long)]
@@ -75,28 +55,51 @@ pub struct Flags {
/// Subcommand for `wasmer config set` /// Subcommand for `wasmer config set`
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Parser)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Parser)]
pub enum Set { pub enum GetOrSet {
/// `wasmer config get $KEY`
#[clap(subcommand)]
Get(RetrievableConfigField),
/// `wasmer config set $KEY $VALUE` /// `wasmer config set $KEY $VALUE`
#[clap(subcommand)] #[clap(subcommand)]
Set(StorableConfigField), Set(StorableConfigField),
} }
/// Subcommand for `wasmer config get`
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Parser)]
pub enum RetrievableConfigField {
/// Print the registry URL of the currently active registry
#[clap(name = "registry.url")]
RegistryUrl,
/// Print the token for the currently active registry or nothing if not logged in
#[clap(name = "registry.token")]
RegistryToken,
/// Print whether telemetry is currently enabled
#[clap(name = "telemetry.enabled")]
TelemetryEnabled,
/// Print whether update notifications are enabled
#[clap(name = "update-notifications.enabled")]
UpdateNotificationsEnabled,
/// Print the proxy URL
#[clap(name = "proxy.url")]
ProxyUrl,
}
/// Setting that can be stored in the wasmer config /// Setting that can be stored in the wasmer config
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Parser)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Parser)]
pub enum StorableConfigField { pub enum StorableConfigField {
/// Print the registry URL of the currently active registry /// Set the registry URL of the currently active registry
#[clap(name = "registry.url")] #[clap(name = "registry.url")]
RegistryUrl(SetRegistryUrl), RegistryUrl(SetRegistryUrl),
/// Print the token for the currently active registry or nothing if not logged in /// Set the token for the currently active registry or nothing if not logged in
#[clap(name = "registry.token")] #[clap(name = "registry.token")]
RegistryToken(SetRegistryToken), RegistryToken(SetRegistryToken),
/// Print whether telemetry is currently enabled /// Set whether telemetry is currently enabled
#[clap(name = "telemetry.enabled")] #[clap(name = "telemetry.enabled")]
TelemetryEnabled(SetTelemetryEnabled), TelemetryEnabled(SetTelemetryEnabled),
/// Print whether update notifications are enabled /// Set whether update notifications are enabled
#[clap(name = "update-notifications.enabled")] #[clap(name = "update-notifications.enabled")]
UpdateNotificationsEnabled(SetUpdateNotificationsEnabled), UpdateNotificationsEnabled(SetUpdateNotificationsEnabled),
/// Print the proxy URL /// Set the active proxy URL
#[clap(name = "proxy.url")] #[clap(name = "proxy.url")]
ProxyUrl(SetProxyUrl), ProxyUrl(SetProxyUrl),
} }
@@ -149,7 +152,7 @@ pub struct SetTelemetryEnabled {
pub struct SetProxyUrl { pub struct SetProxyUrl {
/// Set if a proxy URL should be used (empty = unset proxy) /// Set if a proxy URL should be used (empty = unset proxy)
#[clap(name = "URL")] #[clap(name = "URL")]
pub url: Option<String>, pub url: String,
} }
impl Config { impl Config {
@@ -159,7 +162,7 @@ impl Config {
.context("failed to retrieve the wasmer config".to_string()) .context("failed to retrieve the wasmer config".to_string())
} }
fn inner_execute(&self) -> Result<()> { fn inner_execute(&self) -> Result<()> {
if let Some(Set::Set(s)) = self.set.as_ref() { if let Some(s) = self.set.as_ref() {
return s.execute(); return s.execute();
} }
@@ -225,41 +228,11 @@ impl Config {
println!("{}", path.display()); println!("{}", path.display());
} }
let config = WasmerConfig::from_file()
.map_err(|e| anyhow::anyhow!("could not find config file: {e}"))?;
if flags.registry_url {
println!("{}", config.registry.get_current_registry());
}
if flags.registry_token {
if let Some(s) = config
.registry
.get_login_token_for_registry(&config.registry.get_current_registry())
{
println!("{s}");
}
}
if flags.telemetry_enabled {
println!("{:?}", config.telemetry_enabled);
}
if flags.update_notifications_enabled {
println!("{:?}", config.update_notifications_enabled);
}
if flags.proxy_url {
if let Some(s) = config.proxy.url.as_ref() {
println!("{s}");
}
}
Ok(()) Ok(())
} }
} }
impl StorableConfigField { impl GetOrSet {
fn execute(&self) -> Result<()> { fn execute(&self) -> Result<()> {
let config_file = WasmerConfig::get_file_location() let config_file = WasmerConfig::get_file_location()
.map_err(|e| anyhow::anyhow!("could not find config file {e}"))?; .map_err(|e| anyhow::anyhow!("could not find config file {e}"))?;
@@ -270,10 +243,44 @@ impl StorableConfigField {
) )
})?; })?;
match self { match self {
GetOrSet::Get(g) => {
let config = WasmerConfig::from_file()
.map_err(|e| anyhow::anyhow!("could not find config file: {e}"))?;
match g {
RetrievableConfigField::RegistryUrl => {
println!("{}", config.registry.get_current_registry());
}
RetrievableConfigField::RegistryToken => {
if let Some(s) = config
.registry
.get_login_token_for_registry(&config.registry.get_current_registry())
{
println!("{s}");
}
}
RetrievableConfigField::TelemetryEnabled => {
println!("{:?}", config.telemetry_enabled);
}
RetrievableConfigField::UpdateNotificationsEnabled => {
println!("{:?}", config.update_notifications_enabled);
}
RetrievableConfigField::ProxyUrl => {
if let Some(s) = config.proxy.url.as_ref() {
println!("{s}");
} else {
println!("none");
}
}
}
}
GetOrSet::Set(s) => {
match s {
StorableConfigField::RegistryUrl(s) => { StorableConfigField::RegistryUrl(s) => {
config.registry.set_current_registry(&s.url); config.registry.set_current_registry(&s.url);
let current_registry = config.registry.get_current_registry(); let current_registry = config.registry.get_current_registry();
if let Some(u) = wasmer_registry::utils::get_username().ok().and_then(|o| o) { if let Some(u) = wasmer_registry::utils::get_username().ok().and_then(|o| o)
{
println!( println!(
"Successfully logged into registry {current_registry:?} as user {u:?}" "Successfully logged into registry {current_registry:?} as user {u:?}"
); );
@@ -290,7 +297,11 @@ impl StorableConfigField {
config.telemetry_enabled = t.enabled.0; config.telemetry_enabled = t.enabled.0;
} }
StorableConfigField::ProxyUrl(p) => { StorableConfigField::ProxyUrl(p) => {
config.proxy.url = p.url.clone(); if p.url == "none" || p.url.is_empty() {
config.proxy.url = None;
} else {
config.proxy.url = Some(p.url.clone());
}
} }
StorableConfigField::UpdateNotificationsEnabled(u) => { StorableConfigField::UpdateNotificationsEnabled(u) => {
config.update_notifications_enabled = u.enabled.0; config.update_notifications_enabled = u.enabled.0;
@@ -299,7 +310,8 @@ impl StorableConfigField {
config config
.save(config_file) .save(config_file)
.with_context(|| anyhow::anyhow!("could not save config file"))?; .with_context(|| anyhow::anyhow!("could not save config file"))?;
}
}
Ok(()) Ok(())
} }
} }

View File

@@ -4,7 +4,6 @@ use std::path::{Path, PathBuf};
#[derive(Deserialize, Default, Serialize, Debug, PartialEq, Eq)] #[derive(Deserialize, Default, Serialize, Debug, PartialEq, Eq)]
pub struct WasmerConfig { pub struct WasmerConfig {
/// Whether or not telemetry is enabled. /// Whether or not telemetry is enabled.
#[serde(default)] #[serde(default)]
pub telemetry_enabled: bool, pub telemetry_enabled: bool,

View File

@@ -154,8 +154,6 @@ fn config_works() -> anyhow::Result<()> {
assert_eq!(lines, args); assert_eq!(lines, args);
// ---- config get
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--config-path") .arg("--config-path")
@@ -167,9 +165,12 @@ fn config_works() -> anyhow::Result<()> {
format!("{}\n", config_path.display()) format!("{}\n", config_path.display())
); );
// ---- config get
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--registry-token") .arg("get")
.arg("registry.token")
.output()?; .output()?;
let original_token = String::from_utf8_lossy(&output.stdout); let original_token = String::from_utf8_lossy(&output.stdout);
@@ -185,7 +186,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--registry-token") .arg("get")
.arg("registry.token")
.output()?; .output()?;
assert_eq!( assert_eq!(
@@ -204,7 +206,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--registry-token") .arg("get")
.arg("registry.token")
.output()?; .output()?;
assert_eq!( assert_eq!(
@@ -214,7 +217,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--registry-url") .arg("get")
.arg("registry.url")
.output()?; .output()?;
let original_url = String::from_utf8_lossy(&output.stdout); let original_url = String::from_utf8_lossy(&output.stdout);
@@ -232,7 +236,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--registry-url") .arg("get")
.arg("registry.url")
.output()?; .output()?;
let output_str = String::from_utf8_lossy(&output.stdout); let output_str = String::from_utf8_lossy(&output.stdout);
@@ -253,7 +258,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--registry-url") .arg("get")
.arg("registry.url")
.output()?; .output()?;
let output_str = String::from_utf8_lossy(&output.stdout); let output_str = String::from_utf8_lossy(&output.stdout);
@@ -261,7 +267,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--telemetry-enabled") .arg("get")
.arg("telemetry.enabled")
.output()?; .output()?;
let original_output = String::from_utf8_lossy(&output.stdout); let original_output = String::from_utf8_lossy(&output.stdout);
@@ -277,7 +284,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--telemetry-enabled") .arg("get")
.arg("telemetry.enabled")
.output()?; .output()?;
assert_eq!( assert_eq!(
@@ -296,7 +304,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--telemetry-enabled") .arg("get")
.arg("telemetry.enabled")
.output()?; .output()?;
assert_eq!( assert_eq!(
@@ -306,7 +315,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--update-notifications-enabled") .arg("get")
.arg("update-notifications.enabled")
.output()?; .output()?;
let original_output = String::from_utf8_lossy(&output.stdout); let original_output = String::from_utf8_lossy(&output.stdout);
@@ -322,7 +332,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--update-notifications-enabled") .arg("get")
.arg("update-notifications.enabled")
.output()?; .output()?;
assert_eq!( assert_eq!(
@@ -341,7 +352,8 @@ fn config_works() -> anyhow::Result<()> {
let output = Command::new(get_wasmer_path()) let output = Command::new(get_wasmer_path())
.arg("config") .arg("config")
.arg("--update-notifications-enabled") .arg("get")
.arg("update-notifications.enabled")
.output()?; .output()?;
assert_eq!( assert_eq!(