Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli/checkout: use Clap
Merged levitte opened 6 months ago
3 files changed +33 -72 63486688 06e22434
modified crates/radicle-cli/src/commands/checkout.rs
@@ -1,10 +1,11 @@
#![allow(clippy::box_default)]
-
use std::ffi::OsString;
use std::path::PathBuf;

use anyhow::anyhow;
use anyhow::Context as _;

+
use clap::Parser;
+

use radicle::git;
use radicle::node::AliasStore;
use radicle::prelude::*;
@@ -12,80 +13,38 @@ use radicle::storage::git::transport;

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

-
pub const HELP: Help = Help {
-
    name: "checkout",
-
    description: "Checkout a repository into the local directory",
-
    version: env!("RADICLE_VERSION"),
-
    usage: r#"
-
Usage
-

-
    rad checkout <rid> [--remote <did>] [<option>...]
-

-
    Creates a working copy from a repository in local storage.
-

-
Options
-

-
    --remote <did>  Remote peer to checkout
-
    --no-confirm    Don't ask for confirmation during checkout
-
    --help          Print help
-
"#,
-
};

-
pub struct Options {
-
    pub id: RepoId,
+
pub(crate) const ABOUT: &str = "Checkout a repository into the local directory";
+
const LONG_ABOUT: &str = r#"
+
Creates a working copy from a repository in local storage.
+
"#;
+

+
#[derive(Debug, Parser)]
+
#[command(about = ABOUT, long_about = LONG_ABOUT, disable_version_flag = true)]
+
pub struct Args {
+
    /// Repository identity
+
    #[arg(value_name = "RID")]
+
    pub repo: RepoId,
+
    /// The DID of the remote peer to checkout
+
    #[arg(long)]
    pub remote: Option<Did>,
+
    /// Don't ask for confirmation during checkout
+
    #[arg(long)]
+
    pub no_confirm: bool,
}

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

-
        let mut parser = lexopt::Parser::from_args(args);
-
        let mut id = None;
-
        let mut remote = None;
-

-
        while let Some(arg) = parser.next()? {
-
            match arg {
-
                Long("no-confirm") => {
-
                    // Ignored for now.
-
                }
-
                Long("help") | Short('h') => return Err(Error::Help.into()),
-
                Long("remote") => {
-
                    let val = parser.value().unwrap();
-
                    remote = Some(term::args::did(&val)?);
-
                }
-
                Value(val) if id.is_none() => {
-
                    id = Some(term::args::rid(&val)?);
-
                }
-
                _ => anyhow::bail!(arg.unexpected()),
-
            }
-
        }
-

-
        Ok((
-
            Options {
-
                id: id.ok_or_else(|| anyhow!("a repository to checkout must be provided"))?,
-
                remote,
-
            },
-
            vec![],
-
        ))
-
    }
-
}
-

-
pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
+
pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
-
    execute(options, &profile)?;
+
    execute(args, &profile)?;

    Ok(())
}

-
fn execute(options: Options, profile: &Profile) -> anyhow::Result<PathBuf> {
-
    let id = options.id;
+
fn execute(args: Args, profile: &Profile) -> anyhow::Result<PathBuf> {
    let storage = &profile.storage;
-
    let remote = options.remote.unwrap_or(profile.did());
+
    let remote = args.remote.unwrap_or(profile.did());
    let doc = storage
-
        .repository(id)?
+
        .repository(args.repo)?
        .identity_doc()
        .context("repository could not be found in local storage")?;
    let payload = doc.project()?;
@@ -98,7 +57,7 @@ fn execute(options: Options, profile: &Profile) -> anyhow::Result<PathBuf> {
    }

    let mut spinner = term::spinner("Performing checkout...");
-
    let repo = match radicle::rad::checkout(options.id, &remote, path.clone(), &storage, false) {
+
    let repo = match radicle::rad::checkout(args.repo, &remote, path.clone(), &storage, false) {
        Ok(repo) => repo,
        Err(err) => {
            spinner.failed();
@@ -124,7 +83,7 @@ fn execute(options: Options, profile: &Profile) -> anyhow::Result<PathBuf> {
    // Setup remote tracking branches for project delegates.
    setup_remotes(
        project::SetupRemote {
-
            rid: id,
+
            rid: args.repo,
            tracking: Some(payload.default_branch().clone()),
            repo: &repo,
            fetch: true,
modified crates/radicle-cli/src/commands/help.rs
@@ -39,7 +39,10 @@ impl CommandItem {
const COMMANDS: &[CommandItem] = &[
    CommandItem::Lexopt(crate::commands::auth::HELP),
    CommandItem::Lexopt(crate::commands::block::HELP),
-
    CommandItem::Lexopt(crate::commands::checkout::HELP),
+
    CommandItem::Clap {
+
        name: "checkout",
+
        about: crate::commands::checkout::ABOUT,
+
    },
    CommandItem::Lexopt(crate::commands::clone::HELP),
    CommandItem::Lexopt(crate::commands::config::HELP),
    CommandItem::Lexopt(crate::commands::fork::HELP),
modified crates/radicle-cli/src/main.rs
@@ -45,6 +45,7 @@ struct CliArgs {

#[derive(Subcommand, Debug)]
enum Commands {
+
    Checkout(checkout::Args),
    Clean(clean::Args),
    Issue(issue::Args),
    Path(path::Args),
@@ -181,11 +182,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
            term::run_command_args::<block::Options, _>(block::HELP, block::run, args.to_vec());
        }
        "checkout" => {
-
            term::run_command_args::<checkout::Options, _>(
-
                checkout::HELP,
-
                checkout::run,
-
                args.to_vec(),
-
            );
+
            if let Some(Commands::Checkout(args)) = CliArgs::parse().command {
+
                term::run_command_fn(checkout::run, args);
+
            }
        }
        "clone" => {
            term::run_command_args::<clone::Options, _>(clone::HELP, clone::run, args.to_vec());