From 07b9439c27dfbeed0c635869c3b5fb21ef4e4b90 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 19 Jun 2022 11:14:35 -0400 Subject: [PATCH] switch to a better setup for subcommands --- src/cli.rs | 95 ++++++++++++++++++++++------------------------------- src/main.rs | 30 ++++++++--------- 2 files changed, 54 insertions(+), 71 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index be06ae2..ad6fecc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -22,39 +22,16 @@ pub(crate) struct Args { #[derive(Debug, Clone, Parser)] pub(crate) enum Subcommands { - Debug { - #[clap(long)] - demo_conf_menu: bool - }, + Debug(ArgsDebug), // Completions { // TODO: Add completions // }, - #[clap(about = "Interactive interface for trade confirmations")] - Trade { - #[clap(short, long, help = "Accept all open trade confirmations. Does not open interactive interface.")] - accept_all: bool, - #[clap(short, long, help = "If submitting a confirmation response fails, exit immediately.")] - fail_fast: bool, - }, - #[clap(about = "Set up a new account with steamguard-cli")] - Setup { - #[clap(short, long, from_global, help = "Steam username, case-sensitive.")] - username: Option, - }, - #[clap(about = "Import an account with steamguard already set up")] - Import { - #[clap(long, help = "Paths to one or more maFiles, eg. \"./gaben.maFile\"")] - files: Vec, - }, - #[clap(about = "Remove the authenticator from an account.")] - Remove { - #[clap(short, long, from_global, help = "Steam username, case-sensitive.")] - username: String, - }, - #[clap(about = "Encrypt all maFiles")] - Encrypt, - #[clap(about = "Decrypt all maFiles")] - Decrypt, + Setup(ArgsSetup), + Import(ArgsImport), + Trade(ArgsTrade), + Remove(ArgsRemove), + Encrypt(ArgsEncrypt), + Decrypt(ArgsDecrypt), } #[derive(Debug, Clone, Copy)] @@ -93,42 +70,48 @@ impl FromStr for Verbosity { } } + +#[derive(Debug, Clone, Parser)] +#[clap(about="Debug stuff, not useful for most users.")] +pub(crate) struct ArgsDebug { + #[clap(long)] + pub demo_conf_menu: bool +} + +#[derive(Debug, Clone, Parser)] +#[clap(about = "Set up a new account with steamguard-cli")] pub(crate) struct ArgsSetup { + #[clap(short, long, from_global, help = "Steam username, case-sensitive.")] pub username: Option, } -impl From for ArgsSetup { - fn from(sub: Subcommands) -> Self { - match sub { - Subcommands::Setup { username } => Self { username }, - _ => panic!("ArgsSetup::from() called with non-Setup subcommand"), - } - } -} - +#[derive(Debug, Clone, Parser)] +#[clap(about = "Import an account with steamguard already set up")] pub(crate) struct ArgsImport { + #[clap(long, help = "Paths to one or more maFiles, eg. \"./gaben.maFile\"")] pub files: Vec, } -impl From for ArgsImport { - fn from(sub: Subcommands) -> Self { - match sub { - Subcommands::Import { files } => Self { files }, - _ => panic!("ArgsImport::from() called with non-Import subcommand"), - } - } -} - +#[derive(Debug, Clone, Parser)] +#[clap(about = "Interactive interface for trade confirmations")] pub(crate) struct ArgsTrade { + #[clap(short, long, help = "Accept all open trade confirmations. Does not open interactive interface.")] pub accept_all: bool, + #[clap(short, long, help = "If submitting a confirmation response fails, exit immediately.")] pub fail_fast: bool, } -impl From for ArgsTrade { - fn from(sub: Subcommands) -> Self { - match sub { - Subcommands::Trade { accept_all, fail_fast } => Self { accept_all, fail_fast }, - _ => panic!("ArgsTrade::from() called with non-Trade subcommand"), - } - } -} \ No newline at end of file +#[derive(Debug, Clone, Parser)] +#[clap(about = "Remove the authenticator from an account.")] +pub(crate) struct ArgsRemove { + #[clap(short, long, from_global, help = "Steam username, case-sensitive.")] + username: String, +} + +#[derive(Debug, Clone, Parser)] +#[clap(about = "Encrypt all maFiles")] +pub(crate) struct ArgsEncrypt; + +#[derive(Debug, Clone, Parser)] +#[clap(about = "Decrypt all maFiles")] +pub(crate) struct ArgsDecrypt; diff --git a/src/main.rs b/src/main.rs index 6225ee3..88ee22b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use steamguard::{ SteamGuardAccount, UserLogin, }; -use crate::accountmanager::ManifestAccountLoadError; +use crate::{accountmanager::ManifestAccountLoadError, cli::Subcommands}; #[macro_use] extern crate lazy_static; @@ -157,8 +157,8 @@ fn run() -> anyhow::Result<()> { .unwrap(); match new_args.sub { - Some(cli::Subcommands::Debug{demo_conf_menu}) => { - if demo_conf_menu { + Some(cli::Subcommands::Debug(args)) => { + if args.demo_conf_menu { demos::demo_confirmation_menu(); } return Ok(()); @@ -233,13 +233,13 @@ fn run() -> anyhow::Result<()> { } } - match &new_args.sub { - Some(cli::Subcommands::Setup{ username }) => { - return do_subcmd_setup(new_args.sub.unwrap().into(), &mut manifest); + match new_args.sub { + Some(cli::Subcommands::Setup(args)) => { + return do_subcmd_setup(args, &mut manifest); }, - Some(cli::Subcommands::Import { files }) => {todo!()}, - Some(cli::Subcommands::Encrypt {}) => {todo!()}, - Some(cli::Subcommands::Decrypt {}) => {todo!()}, + Some(cli::Subcommands::Import(args)) => {todo!()}, + Some(cli::Subcommands::Encrypt(args)) => {todo!()}, + Some(cli::Subcommands::Decrypt(args)) => {todo!()}, _ => {}, } @@ -321,7 +321,7 @@ fn run() -> anyhow::Result<()> { ); match new_args.sub.as_ref() { - Some(cli::Subcommands::Trade{ accept_all, fail_fast }) => { + Some(cli::Subcommands::Trade(args)) => { for a in selected_accounts.iter_mut() { let mut account = a.lock().unwrap(); @@ -341,14 +341,14 @@ fn run() -> anyhow::Result<()> { } let mut any_failed = false; - if *accept_all { + if args.accept_all { info!("accepting all confirmations"); for conf in &confirmations { let result = account.accept_confirmation(conf); if result.is_err() { warn!("accept confirmation result: {:?}", result); any_failed = true; - if *fail_fast { + if args.fail_fast { return result; } } else { @@ -363,7 +363,7 @@ fn run() -> anyhow::Result<()> { if result.is_err() { warn!("accept confirmation result: {:?}", result); any_failed = true; - if *fail_fast { + if args.fail_fast { return result; } } else { @@ -376,7 +376,7 @@ fn run() -> anyhow::Result<()> { if result.is_err() { warn!("deny confirmation result: {:?}", result); any_failed = true; - if *fail_fast { + if args.fail_fast { return result; } } else { @@ -398,7 +398,7 @@ fn run() -> anyhow::Result<()> { manifest.save()?; }, - Some(cli::Subcommands::Remove { username }) => { + Some(cli::Subcommands::Remove(args)) => { println!( "This will remove the mobile authenticator from {} accounts: {}", selected_accounts.len(),