Skip to content

Commit

Permalink
fix: remove clap from enforcing network args and update error message (
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored Nov 5, 2024
1 parent e44b89f commit c873f9f
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions cmd/soroban-cli/src/config/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@ pub mod passphrase;
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(
r#"Access to the network is required
`--network` or `--rpc-url` and `--network-passphrase` are required if using the network.
Alternatively you can use their corresponding environment variables:
STELLAR_NETWORK, STELLAR_RPC_URL and STELLAR_NETWORK_PASSPHRASE"#
)]
Network,
#[error(
"rpc-url is used but network passphrase is missing, use `--network-passphrase` or `STELLAR_NETWORK_PASSPHRASE`"
)]
MissingNetworkPassphrase,
#[error(
"network passphrase is used but rpc-url is missing, use `--rpc-url` or `STELLAR_RPC_URL`"
)]
MissingRpcUrl,
#[error("cannot use both `--rpc-url` and `--network`")]
CannotUseBothRpcAndNetwork,
#[error(transparent)]
Rpc(#[from] rpc::Error),
#[error(transparent)]
Expand All @@ -48,8 +63,6 @@ pub struct Args {
/// RPC server endpoint
#[arg(
long = "rpc-url",
requires = "network_passphrase",
required_unless_present = "network",
env = "STELLAR_RPC_URL",
help_heading = HEADING_RPC,
)]
Expand All @@ -68,17 +81,13 @@ pub struct Args {
/// Network passphrase to sign the transaction sent to the rpc server
#[arg(
long = "network-passphrase",
requires = "rpc_url",
required_unless_present = "network",
env = "STELLAR_NETWORK_PASSPHRASE",
help_heading = HEADING_RPC,
)]
pub network_passphrase: Option<String>,
/// Name of network to use from config
#[arg(
long,
required_unless_present = "rpc_url",
required_unless_present = "network_passphrase",
env = "STELLAR_NETWORK",
help_heading = HEADING_RPC,
)]
Expand All @@ -87,21 +96,20 @@ pub struct Args {

impl Args {
pub fn get(&self, locator: &locator::Args) -> Result<Network, Error> {
if let Some(name) = self.network.as_deref() {
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 {
match (
self.network.as_deref(),
self.rpc_url.clone(),
self.network_passphrase.clone(),
) {
(None, None, None) => Err(Error::Network),
(_, Some(_), None) => Err(Error::MissingNetworkPassphrase),
(_, None, Some(_)) => Err(Error::MissingRpcUrl),
(Some(network), None, None) => Ok(locator.read_network(network)?),
(_, Some(rpc_url), Some(network_passphrase)) => Ok(Network {
rpc_url,
rpc_headers: self.rpc_headers.clone(),
network_passphrase,
})
} else {
Err(Error::Network)
}),
}
}
}
Expand Down

0 comments on commit c873f9f

Please sign in to comment.