Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/auth: use Clap
Merged levitte opened 6 months ago
4 files changed +45 -71 dfd35480 384c5064
modified crates/radicle-cli/examples/rad-auth-errors.md
@@ -1,17 +1,23 @@
Note that aliases must not be longer than 32 bytes, or you will get an error.
There are other rules as well:

-
``` (fail)
+
``` (stderr) (fail)
$ rad auth --alias "5fad63fe6b339fa92c588d926121bea6240773a7"
-
✗ Error: rad auth: alias cannot be greater than 32 bytes
+
error: invalid value '5fad63fe6b339fa92c588d926121bea6240773a7' for '--alias <STRING>': alias cannot be greater than 32 bytes
+

+
For more information, try '--help'.
```

-
``` (fail)
+
``` (stderr) (fail)
$ rad auth --alias "john doe"
-
✗ Error: rad auth: alias cannot contain whitespace or control characters
+
error: invalid value 'john doe' for '--alias <STRING>': alias cannot contain whitespace or control characters
+

+
For more information, try '--help'.
```

-
``` (fail)
+
``` (stderr) (fail)
$ rad auth --alias ""
-
✗ Error: rad auth: alias cannot be empty
+
error: invalid value '' for '--alias <STRING>': alias cannot be empty
+

+
For more information, try '--help'.
```
modified crates/radicle-cli/src/commands/auth.rs
@@ -1,9 +1,10 @@
#![allow(clippy::or_fun_call)]
-
use std::ffi::OsString;
use std::str::FromStr;

use anyhow::{anyhow, Context};

+
use clap::Parser;
+

use radicle::crypto::ssh;
use radicle::crypto::ssh::Passphrase;
use radicle::node::Alias;
@@ -11,73 +12,34 @@ use radicle::profile::env;
use radicle::{profile, Profile};

use crate::terminal as term;
-
use crate::terminal::args::{Args, Error, Help};
-

-
pub const HELP: Help = Help {
-
    name: "auth",
-
    description: "Manage identities and profiles",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad auth [<option>...]
-

-
    A passphrase may be given via the environment variable `RAD_PASSPHRASE` or
-
    via the standard input stream if `--stdin` is used. Using either of these
-
    methods disables the passphrase prompt.
-

-
Options

-
    --alias                 When initializing an identity, sets the node alias
-
    --stdin                 Read passphrase from stdin (default: false)
-
    --help                  Print help
-
"#,
-
};
-

-
#[derive(Debug)]
-
pub struct Options {
-
    pub stdin: bool,
+
pub(crate) const ABOUT: &str = "Manage identities and profiles";
+
const LONG_ABOUT: &str = r#"
+
A passphrase may be given via the environment variable `RAD_PASSPHRASE` or
+
via the standard input stream if `--stdin` is used. Using either of these
+
methods disables the passphrase prompt.
+
"#;
+

+
#[derive(Debug, Parser)]
+
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
+
pub struct Args {
+
    /// When initializing an identity, sets the node alias
+
    #[arg(long, value_name = "STRING")]
    pub alias: Option<Alias>,
-
}
-

-
impl Args for Options {
-
    fn from_args(args: Vec<OsString>) -> anyhow::Result<(Self, Vec<OsString>)> {
-
        use lexopt::prelude::*;
-

-
        let mut stdin = false;
-
        let mut alias = None;
-
        let mut parser = lexopt::Parser::from_args(args);

-
        while let Some(arg) = parser.next()? {
-
            match arg {
-
                Long("alias") => {
-
                    let val = parser.value()?;
-
                    let val = term::args::alias(&val)?;
-

-
                    alias = Some(val);
-
                }
-
                Long("stdin") => {
-
                    stdin = true;
-
                }
-
                Long("help") | Short('h') => {
-
                    return Err(Error::Help.into());
-
                }
-
                _ => anyhow::bail!(arg.unexpected()),
-
            }
-
        }
-

-
        Ok((Options { alias, stdin }, vec![]))
-
    }
+
    /// Read passphrase from stdin (default: false)
+
    #[arg(long)]
+
    pub stdin: bool,
}

-
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
+
pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    match ctx.profile() {
-
        Ok(profile) => authenticate(options, &profile),
-
        Err(_) => init(options),
+
        Ok(profile) => authenticate(args, &profile),
+
        Err(_) => init(args),
    }
}

-
pub fn init(options: Options) -> anyhow::Result<()> {
+
pub fn init(args: Args) -> anyhow::Result<()> {
    term::headline("Initializing your radicle 👾 identity");

    if let Ok(version) = radicle::git::version() {
@@ -92,7 +54,7 @@ pub fn init(options: Options) -> anyhow::Result<()> {
        anyhow::bail!("A Git installation is required for Radicle to run.");
    }

-
    let alias: Alias = if let Some(alias) = options.alias {
+
    let alias: Alias = if let Some(alias) = args.alias {
        alias
    } else {
        let user = env::var("USER").ok().and_then(|u| Alias::from_str(&u).ok());
@@ -105,7 +67,7 @@ pub fn init(options: Options) -> anyhow::Result<()> {
        user.ok_or_else(|| anyhow::anyhow!("An alias is required for Radicle to run."))?
    };
    let home = profile::home()?;
-
    let passphrase = if options.stdin {
+
    let passphrase = if args.stdin {
        Some(term::passphrase_stdin()?)
    } else {
        term::passphrase_confirm("Enter a passphrase:", env::RAD_PASSPHRASE)?
@@ -165,7 +127,7 @@ pub fn init(options: Options) -> anyhow::Result<()> {

/// Try loading the identity's key into SSH Agent, falling back to verifying `RAD_PASSPHRASE` for
/// use.
-
pub fn authenticate(options: Options, profile: &Profile) -> anyhow::Result<()> {
+
pub fn authenticate(args: Args, profile: &Profile) -> anyhow::Result<()> {
    if !profile.keystore.is_encrypted()? {
        term::success!("Authenticated as {}", term::format::tertiary(profile.id()));
        return Ok(());
@@ -186,7 +148,7 @@ pub fn authenticate(options: Options, profile: &Profile) -> anyhow::Result<()> {
            }
            let passphrase = if let Some(phrase) = profile::env::passphrase() {
                phrase
-
            } else if options.stdin {
+
            } else if args.stdin {
                term::passphrase_stdin()?
            } else if let Some(passphrase) =
                term::io::passphrase(term::io::PassphraseValidator::new(profile.keystore.clone()))?
modified crates/radicle-cli/src/commands/help.rs
@@ -37,7 +37,10 @@ impl CommandItem {
}

const COMMANDS: &[CommandItem] = &[
-
    CommandItem::Lexopt(crate::commands::auth::HELP),
+
    CommandItem::Clap {
+
        name: "auth",
+
        about: crate::commands::auth::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::block::HELP),
    CommandItem::Lexopt(crate::commands::checkout::HELP),
    CommandItem::Lexopt(crate::commands::clone::HELP),
modified crates/radicle-cli/src/main.rs
@@ -45,6 +45,7 @@ struct CliArgs {

#[derive(Subcommand, Debug)]
enum Commands {
+
    Auth(auth::Args),
    Clean(clean::Args),
    Issue(issue::Args),
    Path(path::Args),
@@ -175,7 +176,9 @@ fn run(command: Command) -> Result<(), Option<anyhow::Error>> {
pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>> {
    match exe {
        "auth" => {
-
            term::run_command_args::<auth::Options, _>(auth::HELP, auth::run, args.to_vec());
+
            if let Some(Commands::Auth(args)) = CliArgs::parse().command {
+
                term::run_command_fn(auth::run, args);
+
            }
        }
        "block" => {
            term::run_command_args::<block::Options, _>(block::HELP, block::run, args.to_vec());