From b237db88a50ff2795d3670565ef94d24a496da4e Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Wed, 6 Mar 2019 10:05:32 +0100 Subject: [PATCH] feat(CLI): Remove subcommands. Make some flags optional. Remove abundance of !! from git hook. --- .cargo-husky/hooks/pre-commit | 2 +- src/cli.rs | 58 ++++++++++------------------------- tests/cli_integration.rs | 3 -- 3 files changed, 18 insertions(+), 45 deletions(-) diff --git a/.cargo-husky/hooks/pre-commit b/.cargo-husky/hooks/pre-commit index f778ab3..84447df 100755 --- a/.cargo-husky/hooks/pre-commit +++ b/.cargo-husky/hooks/pre-commit @@ -14,7 +14,7 @@ cargo clippy -- -D warnings echo 'Precommit: cargo fmt' cargo fmt -echo 'Precommit: !!!!!updating formatted files' +echo 'Precommit: updating formatted files' # Stage updated files git add -u diff --git a/src/cli.rs b/src/cli.rs index 7f8925e..57de2a5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,39 +1,24 @@ use log::info; -use std::error::Error; use safe_auth::{acc_info, authed_apps, authorise_app, create_acc, log_in}; use structopt::StructOpt; #[derive(StructOpt, Debug)] -pub enum SubCommands { - #[structopt(name = "create")] - /// Create a new SAFE Network account with the credentials provided - Invite { - /// The invitation token for creating a new SAFE Network account - #[structopt(short = "i", long = "invite-token")] - invite: String, - }, - #[structopt(name = "auth")] - /// Authorise an application by providing the authorisation request URI or string - Auth { - /// The authorisation request URI or string - #[structopt(short = "r", long = "req")] - req: String, - }, -} - -#[derive(StructOpt, Debug)] +/// Manage SAFE Network authorisations and accounts. pub struct CmdArgs { + /// The authorisation request URI or string + #[structopt(short = "r", long = "req")] + req: Option, + /// The invitation token for creating a new SAFE Network account + #[structopt(short = "i", long = "invite-token")] + invite: Option, /// The secret phrase of the SAFE account #[structopt(short = "s", long = "secret")] secret: String, /// The SAFE account's password #[structopt(short = "p", long = "password")] password: String, - /// subcommands - #[structopt(subcommand)] - cmd: Option, /// Get account's balance #[structopt(short = "b", long = "balance")] balance: bool, @@ -42,27 +27,18 @@ pub struct CmdArgs { apps: bool, } -pub fn run() -> Result<(), Box> { +pub fn run() -> Result<(), String> { let args = CmdArgs::from_args(); - let authenticator = match args.cmd { - None => log_in(&args.secret, &args.password).and_then(|auth| { - info!("Logged-in successfully!"); - Ok(auth) - })?, - Some(SubCommands::Invite { invite }) => create_acc(&invite, &args.secret, &args.password) - .and_then(|auth| { - info!("Account created successfully!"); - Ok(auth) - })?, - Some(SubCommands::Auth { req }) => { - log_in(&args.secret, &args.password).and_then(|auth| { - let resp = authorise_app(&auth, &req)?; - info!("Auth response generated: {:?}", resp); - Ok(auth) - })? - } - }; + if let Option::Some(invite) = &args.invite { + create_acc(&invite, &args.secret, &args.password)?; + } + + let authenticator = log_in(&args.secret, &args.password)?; + + if let Option::Some(req) = &args.req { + authorise_app(&authenticator, &req)?; + } if args.balance { let (mutations_done, mutations_available) = acc_info(&authenticator)?; diff --git a/tests/cli_integration.rs b/tests/cli_integration.rs index aa330c9..08f27f0 100644 --- a/tests/cli_integration.rs +++ b/tests/cli_integration.rs @@ -20,7 +20,6 @@ mod cli_integration { &rand_string, "--password", &rand_string, - "create", "--invite-token", "aaa", ]) @@ -46,7 +45,6 @@ mod cli_integration { &rand_string, "--password", &rand_string, - "create", "--invite-token", "aaa", ]) @@ -61,7 +59,6 @@ mod cli_integration { &rand_string, "--password", &rand_string, - "auth", "-r", &UNAUTHED_REQ, ])