Default to buy-xmr if a subcommand is not specified

A new struct, CliExecutionParams, was created to represent the execution
path of the program and separate program execution from Cli argument
parsing.
This commit is contained in:
rishflab 2021-03-04 14:35:58 +11:00
parent 5cb25edea0
commit c4b14b21f3
2 changed files with 150 additions and 24 deletions

View File

@ -20,9 +20,8 @@ use std::future::Future;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use structopt::StructOpt;
use swap::bitcoin::{Amount, TxLock};
use swap::cli::command::{Arguments, Command};
use swap::cli::command::{CliExecutionParams, Command};
use swap::cli::config::{read_config, Config};
use swap::database::Database;
use swap::execution_params::GetExecutionParams;
@ -43,7 +42,7 @@ const MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME: &str = "swap-tool-blockchain-mon
#[tokio::main]
async fn main() -> Result<()> {
let args = Arguments::from_args();
let cli_execution_params = CliExecutionParams::from_args();
let is_terminal = atty::is(atty::Stream::Stderr);
let base_subscriber = |level| {
@ -54,7 +53,7 @@ async fn main() -> Result<()> {
.with_env_filter(format!("swap={}", level))
};
if args.debug {
if cli_execution_params.debug {
let subscriber = base_subscriber(Level::DEBUG)
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::with_format(
"%F %T".to_owned(),
@ -71,7 +70,7 @@ async fn main() -> Result<()> {
tracing::subscriber::set_global_default(subscriber)?;
}
let config = match args.config {
let config = match cli_execution_params.config {
Some(config_path) => read_config(config_path)??,
None => Config::testnet(),
};
@ -99,7 +98,7 @@ async fn main() -> Result<()> {
.run(monero_network, "stagenet.community.xmr.to")
.await?;
match args.cmd {
match cli_execution_params.command {
Command::BuyXmr {
receive_monero_address,
alice_peer_id,

View File

@ -3,13 +3,121 @@ use libp2p::core::Multiaddr;
use libp2p::PeerId;
use std::path::PathBuf;
use std::str::FromStr;
use structopt::clap::AppSettings;
use structopt::StructOpt;
use uuid::Uuid;
pub const DEFAULT_ALICE_MULTIADDR: &str = "/dns4/xmr-btc-asb.coblox.tech/tcp/9876";
pub const DEFAULT_ALICE_PEER_ID: &str = "12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
pub struct CliExecutionParams {
pub config: Option<PathBuf>,
pub debug: bool,
pub command: Command,
}
impl CliExecutionParams {
pub fn from_args() -> Self {
let matches = Arguments::clap()
.setting(AppSettings::SubcommandsNegateReqs)
.setting(AppSettings::ArgsNegateSubcommands)
.get_matches();
if matches.subcommand_name().is_none() {
let args = Arguments::from_clap(&matches);
CliExecutionParams {
config: args.config,
debug: args.debug,
command: Command::BuyXmr {
receive_monero_address: args.receive_monero_address,
alice_peer_id: args.alice_peer_id,
alice_addr: args.alice_addr,
},
}
} else {
let sub_command: SubCommand = SubCommand::from_clap(&matches);
match sub_command {
SubCommand::History { debug } => CliExecutionParams {
config: None,
debug,
command: Command::History,
},
SubCommand::Cancel {
swap_id,
force,
config,
debug,
} => CliExecutionParams {
config,
debug,
command: Command::Cancel { swap_id, force },
},
SubCommand::Refund {
swap_id,
force,
config,
debug,
} => CliExecutionParams {
config,
debug,
command: Command::Refund { swap_id, force },
},
SubCommand::Resume {
receive_monero_address,
swap_id,
alice_peer_id,
alice_addr,
config,
debug,
} => CliExecutionParams {
config,
debug,
command: Command::Resume {
receive_monero_address,
swap_id,
alice_peer_id,
alice_addr,
},
},
}
}
}
}
#[allow(clippy::large_enum_variant)]
pub enum Command {
BuyXmr {
receive_monero_address: monero::Address,
alice_peer_id: PeerId,
alice_addr: Multiaddr,
},
History,
Resume {
receive_monero_address: monero::Address,
swap_id: Uuid,
alice_peer_id: PeerId,
alice_addr: Multiaddr,
},
Cancel {
swap_id: Uuid,
force: bool,
},
Refund {
swap_id: Uuid,
force: bool,
},
}
#[derive(structopt::StructOpt, Debug)]
pub struct Arguments {
#[structopt(long = "receive-address")]
receive_monero_address: monero::Address,
#[structopt(long = "connect-peer-id", default_value = DEFAULT_ALICE_PEER_ID)]
alice_peer_id: PeerId,
#[structopt(long = "connect-addr", default_value = DEFAULT_ALICE_MULTIADDR)]
alice_addr: Multiaddr,
#[structopt(
long = "config",
help = "Provide a custom path to the configuration file. The configuration file must be a toml file.",
@ -21,26 +129,17 @@ pub struct Arguments {
pub debug: bool,
#[structopt(subcommand)]
pub cmd: Command,
pub sub_command: Option<SubCommand>,
}
#[allow(clippy::large_enum_variant)]
#[derive(structopt::StructOpt, Debug)]
#[structopt(name = "xmr_btc-swap", about = "XMR BTC atomic swap")]
pub enum Command {
BuyXmr {
#[structopt(long = "receive-address", parse(try_from_str = parse_monero_address))]
receive_monero_address: monero::Address,
#[structopt(long = "connect-peer-id", default_value = DEFAULT_ALICE_PEER_ID)]
alice_peer_id: PeerId,
#[structopt(
long = "connect-addr",
default_value = DEFAULT_ALICE_MULTIADDR
)]
alice_addr: Multiaddr,
pub enum SubCommand {
History {
#[structopt(long, help = "Activate debug logging.")]
debug: bool,
},
History,
Resume {
#[structopt(long = "receive-address", parse(try_from_str = parse_monero_address))]
receive_monero_address: monero::Address,
@ -53,11 +152,19 @@ pub enum Command {
#[structopt(long = "counterpart-peer-id", default_value = DEFAULT_ALICE_PEER_ID)]
alice_peer_id: PeerId,
#[structopt(
long = "counterpart-addr",
default_value = DEFAULT_ALICE_MULTIADDR
#[structopt(long = "counterpart-addr", default_value = DEFAULT_ALICE_MULTIADDR
)]
alice_addr: Multiaddr,
#[structopt(
long = "config",
help = "Provide a custom path to the configuration file. The configuration file must be a toml file.",
parse(from_os_str)
)]
config: Option<PathBuf>,
#[structopt(long, help = "Activate debug logging.")]
debug: bool,
},
Cancel {
#[structopt(long = "swap-id")]
@ -65,6 +172,16 @@ pub enum Command {
#[structopt(short, long)]
force: bool,
#[structopt(
long = "config",
help = "Provide a custom path to the configuration file. The configuration file must be a toml file.",
parse(from_os_str)
)]
config: Option<PathBuf>,
#[structopt(long, help = "Activate debug logging.")]
debug: bool,
},
Refund {
#[structopt(long = "swap-id")]
@ -72,6 +189,16 @@ pub enum Command {
#[structopt(short, long)]
force: bool,
#[structopt(
long = "config",
help = "Provide a custom path to the configuration file. The configuration file must be a toml file.",
parse(from_os_str)
)]
config: Option<PathBuf>,
#[structopt(long, help = "Activate debug logging.")]
debug: bool,
},
}