cli/checkout: use Clap
This commit is contained in:
parent
28824a3127
commit
06e22434e1
|
|
@ -1,5 +1,6 @@
|
||||||
#![allow(clippy::box_default)]
|
#![allow(clippy::box_default)]
|
||||||
use std::ffi::OsString;
|
mod args;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
|
@ -12,80 +13,22 @@ use radicle::storage::git::transport;
|
||||||
|
|
||||||
use crate::project;
|
use crate::project;
|
||||||
use crate::terminal as term;
|
use crate::terminal as term;
|
||||||
use crate::terminal::args::{Args, Error, Help};
|
|
||||||
|
|
||||||
pub const HELP: Help = Help {
|
pub use args::Args;
|
||||||
name: "checkout",
|
pub(crate) use args::ABOUT;
|
||||||
description: "Checkout a repository into the local directory",
|
|
||||||
version: env!("RADICLE_VERSION"),
|
|
||||||
usage: r#"
|
|
||||||
Usage
|
|
||||||
|
|
||||||
rad checkout <rid> [--remote <did>] [<option>...]
|
pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
|
||||||
|
|
||||||
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 remote: Option<Did>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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<()> {
|
|
||||||
let profile = ctx.profile()?;
|
let profile = ctx.profile()?;
|
||||||
execute(options, &profile)?;
|
execute(args, &profile)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(options: Options, profile: &Profile) -> anyhow::Result<PathBuf> {
|
fn execute(args: Args, profile: &Profile) -> anyhow::Result<PathBuf> {
|
||||||
let id = options.id;
|
|
||||||
let storage = &profile.storage;
|
let storage = &profile.storage;
|
||||||
let remote = options.remote.unwrap_or(profile.did());
|
let remote = args.remote.unwrap_or(profile.did());
|
||||||
let doc = storage
|
let doc = storage
|
||||||
.repository(id)?
|
.repository(args.repo)?
|
||||||
.identity_doc()
|
.identity_doc()
|
||||||
.context("repository could not be found in local storage")?;
|
.context("repository could not be found in local storage")?;
|
||||||
let payload = doc.project()?;
|
let payload = doc.project()?;
|
||||||
|
|
@ -98,7 +41,7 @@ fn execute(options: Options, profile: &Profile) -> anyhow::Result<PathBuf> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut spinner = term::spinner("Performing checkout...");
|
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,
|
Ok(repo) => repo,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
spinner.failed();
|
spinner.failed();
|
||||||
|
|
@ -124,7 +67,7 @@ fn execute(options: Options, profile: &Profile) -> anyhow::Result<PathBuf> {
|
||||||
// Setup remote tracking branches for project delegates.
|
// Setup remote tracking branches for project delegates.
|
||||||
setup_remotes(
|
setup_remotes(
|
||||||
project::SetupRemote {
|
project::SetupRemote {
|
||||||
rid: id,
|
rid: args.repo,
|
||||||
tracking: Some(payload.default_branch().clone()),
|
tracking: Some(payload.default_branch().clone()),
|
||||||
repo: &repo,
|
repo: &repo,
|
||||||
fetch: true,
|
fetch: true,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
use clap::Parser;
|
||||||
|
use radicle::prelude::{Did, 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 ID of the repository to checkout
|
||||||
|
#[arg(value_name = "RID")]
|
||||||
|
pub(super) repo: RepoId,
|
||||||
|
|
||||||
|
/// The DID of the remote peer to checkout
|
||||||
|
#[arg(long, value_name = "DID")]
|
||||||
|
pub(super) remote: Option<Did>,
|
||||||
|
|
||||||
|
/// Don't ask for confirmation during checkout
|
||||||
|
// TODO(erikli): This is obsolete and should be removed
|
||||||
|
#[arg(long)]
|
||||||
|
no_confirm: bool,
|
||||||
|
}
|
||||||
|
|
@ -45,7 +45,10 @@ const COMMANDS: &[CommandItem] = &[
|
||||||
name: "block",
|
name: "block",
|
||||||
about: crate::commands::block::ABOUT,
|
about: crate::commands::block::ABOUT,
|
||||||
},
|
},
|
||||||
CommandItem::Lexopt(crate::commands::checkout::HELP),
|
CommandItem::Clap {
|
||||||
|
name: "checkout",
|
||||||
|
about: crate::commands::checkout::ABOUT,
|
||||||
|
},
|
||||||
CommandItem::Clap {
|
CommandItem::Clap {
|
||||||
name: "clone",
|
name: "clone",
|
||||||
about: crate::commands::clone::ABOUT,
|
about: crate::commands::clone::ABOUT,
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ struct CliArgs {
|
||||||
enum Commands {
|
enum Commands {
|
||||||
Auth(auth::Args),
|
Auth(auth::Args),
|
||||||
Block(block::Args),
|
Block(block::Args),
|
||||||
|
Checkout(checkout::Args),
|
||||||
Clean(clean::Args),
|
Clean(clean::Args),
|
||||||
Clone(clone::Args),
|
Clone(clone::Args),
|
||||||
Debug(debug::Args),
|
Debug(debug::Args),
|
||||||
|
|
@ -209,11 +210,9 @@ pub(crate) fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyho
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"checkout" => {
|
"checkout" => {
|
||||||
term::run_command_args::<checkout::Options, _>(
|
if let Some(Commands::Checkout(args)) = CliArgs::parse().command {
|
||||||
checkout::HELP,
|
term::run_command_fn(checkout::run, args);
|
||||||
checkout::run,
|
}
|
||||||
args.to_vec(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
"clone" => {
|
"clone" => {
|
||||||
if let Some(Commands::Clone(args)) = CliArgs::parse().command {
|
if let Some(Commands::Clone(args)) = CliArgs::parse().command {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue