Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
feat(CLI): Remove subcommands. Make some flags optional.
Browse files Browse the repository at this point in the history
Remove abundance of !! from git hook.
  • Loading branch information
joshuef authored and bochaco committed Mar 7, 2019
1 parent 2e20e3a commit b237db8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .cargo-husky/hooks/pre-commit
Expand Up @@ -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

Expand Down
58 changes: 17 additions & 41 deletions 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<String>,
/// The invitation token for creating a new SAFE Network account
#[structopt(short = "i", long = "invite-token")]
invite: Option<String>,
/// 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<SubCommands>,
/// Get account's balance
#[structopt(short = "b", long = "balance")]
balance: bool,
Expand All @@ -42,27 +27,18 @@ pub struct CmdArgs {
apps: bool,
}

pub fn run() -> Result<(), Box<dyn Error>> {
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)?;
Expand Down
3 changes: 0 additions & 3 deletions tests/cli_integration.rs
Expand Up @@ -20,7 +20,6 @@ mod cli_integration {
&rand_string,
"--password",
&rand_string,
"create",
"--invite-token",
"aaa",
])
Expand All @@ -46,7 +45,6 @@ mod cli_integration {
&rand_string,
"--password",
&rand_string,
"create",
"--invite-token",
"aaa",
])
Expand All @@ -61,7 +59,6 @@ mod cli_integration {
&rand_string,
"--password",
&rand_string,
"auth",
"-r",
&UNAUTHED_REQ,
])
Expand Down

0 comments on commit b237db8

Please sign in to comment.