From b8a7176ecc84c9cdb61d989f7b746e3d487fd168 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Sat, 25 Nov 2023 18:34:07 -0500 Subject: [PATCH] fix(CLI): allow passing both network args and prefer `network` arg --- cmd/soroban-cli/src/commands/config/secret.rs | 8 ++++++++ .../src/commands/identity/generate.rs | 17 ++++++----------- cmd/soroban-cli/src/commands/network/mod.rs | 18 +++++++++--------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/cmd/soroban-cli/src/commands/config/secret.rs b/cmd/soroban-cli/src/commands/config/secret.rs index 7e6f08a902..4684e2a88b 100644 --- a/cmd/soroban-cli/src/commands/config/secret.rs +++ b/cmd/soroban-cli/src/commands/config/secret.rs @@ -92,6 +92,14 @@ impl FromStr for Secret { } } +impl From for Secret { + fn from(value: PrivateKey) -> Self { + Secret::SecretKey { + secret_key: value.to_string(), + } + } +} + impl Secret { pub fn private_key(&self, index: Option) -> Result { Ok(match self { diff --git a/cmd/soroban-cli/src/commands/identity/generate.rs b/cmd/soroban-cli/src/commands/identity/generate.rs index 20277f2586..0473eb28b7 100644 --- a/cmd/soroban-cli/src/commands/identity/generate.rs +++ b/cmd/soroban-cli/src/commands/identity/generate.rs @@ -56,21 +56,16 @@ impl Cmd { Secret::from_seed(self.seed.as_deref()) }?; let secret = if self.as_secret { - let secret = seed_phrase.private_key(self.hd_path)?; - Secret::SecretKey { - secret_key: secret.to_string(), - } + seed_phrase.private_key(self.hd_path)?.into() } else { seed_phrase }; self.config_locator.write_identity(&self.name, &secret)?; - if !self.network.is_no_network() { - let addr = secret.public_key(self.hd_path)?; - let network = self.network.get(&self.config_locator)?; - network.fund_address(&addr).await.unwrap_or_else(|_| { - tracing::warn!("Failed to fund address: {addr} on at {}", network.rpc_url); - }); - } + let addr = secret.public_key(self.hd_path)?; + let network = self.network.get(&self.config_locator)?; + network.fund_address(&addr).await.unwrap_or_else(|_| { + tracing::warn!("Failed to fund address: {addr} on at {}", network.rpc_url); + }); Ok(()) } } diff --git a/cmd/soroban-cli/src/commands/network/mod.rs b/cmd/soroban-cli/src/commands/network/mod.rs index a62536c862..22cba1904e 100644 --- a/cmd/soroban-cli/src/commands/network/mod.rs +++ b/cmd/soroban-cli/src/commands/network/mod.rs @@ -40,7 +40,7 @@ pub enum Error { #[error(transparent)] Config(#[from] locator::Error), - #[error("network arg or rpc url and network passphrase are required if using the network")] + #[error("network arg or rpc url and network passphrase are required if using the network")] Network, #[error(transparent)] Rpc(#[from] rpc::Error), @@ -74,6 +74,7 @@ pub struct Args { #[arg( long = "rpc-url", requires = "network_passphrase", + required_unless_present = "network", env = "SOROBAN_RPC_URL", help_heading = HEADING_RPC, )] @@ -82,6 +83,7 @@ pub struct Args { #[arg( long = "network-passphrase", requires = "rpc_url", + required_unless_present = "network", env = "SOROBAN_NETWORK_PASSPHRASE", help_heading = HEADING_RPC, )] @@ -89,8 +91,7 @@ pub struct Args { /// Name of network to use from config #[arg( long, - conflicts_with = "network_passphrase", - conflicts_with = "rpc_url", + required_unless_present = "rpc_url", env = "SOROBAN_NETWORK", help_heading = HEADING_RPC, )] @@ -100,8 +101,11 @@ pub struct Args { impl Args { pub fn get(&self, locator: &locator::Args) -> Result { if let Some(name) = self.network.as_deref() { - Ok(locator.read_network(name)?) - } else if let (Some(rpc_url), Some(network_passphrase)) = + if let Ok(network) = locator.read_network(name) { + return Ok(network); + } + } + if let (Some(rpc_url), Some(network_passphrase)) = (self.rpc_url.clone(), self.network_passphrase.clone()) { Ok(Network { @@ -112,10 +116,6 @@ impl Args { Err(Error::Network) } } - - pub fn is_no_network(&self) -> bool { - self.network.is_none() && self.network_passphrase.is_none() && self.rpc_url.is_none() - } } #[derive(Debug, clap::Args, Serialize, Deserialize, Clone)]