Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: add rad fork command
Fintan Halpenny committed 3 years ago
commit ea03e8a38bb9e7837f5a54129ed094df82a6d6be
parent 92a14b671f5c4ed99618dd38f8bf60bb6987660a
4 files changed +85 -0
modified radicle-cli/src/commands.rs
@@ -14,6 +14,8 @@ pub mod rad_delegate;
pub mod rad_edit;
#[path = "commands/fetch.rs"]
pub mod rad_fetch;
+
#[path = "commands/fork.rs"]
+
pub mod rad_fork;
#[path = "commands/help.rs"]
pub mod rad_help;
#[path = "commands/id.rs"]
added radicle-cli/src/commands/fork.rs
@@ -0,0 +1,74 @@
+
use std::ffi::OsString;
+
use std::path::Path;
+

+
use anyhow::Context as _;
+

+
use radicle::prelude::Id;
+
use radicle::rad;
+

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

+
pub const HELP: Help = Help {
+
    name: "ls",
+
    description: "Create a fork of a project",
+
    version: env!("CARGO_PKG_VERSION"),
+
    usage: r#"
+
Usage
+

+
    rad fork [<rid>] [<option>...]
+

+
Options
+

+
    --help          Print help
+
"#,
+
};
+

+
pub struct Options {
+
    rid: Option<Id>,
+
}
+

+
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 rid = None;
+

+
        if let Some(arg) = parser.next()? {
+
            match arg {
+
                Long("help") => {
+
                    return Err(Error::Help.into());
+
                }
+
                Value(val) if rid.is_none() => {
+
                    rid = Some(args::rid(&val)?);
+
                }
+
                _ => return Err(anyhow::anyhow!(arg.unexpected())),
+
            }
+
        }
+

+
        Ok((Options { rid }, vec![]))
+
    }
+
}
+

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

+
    let rid = match options.rid {
+
        Some(rid) => rid,
+
        None => {
+
            let (_, rid) = radicle::rad::repo(Path::new("."))
+
                .context("Current directory is not a radicle project")?;
+

+
            rid
+
        }
+
    };
+

+
    rad::fork(rid, &signer, &storage)?;
+
    term::success!("Forked project {rid} for {}", profile.id());
+

+
    Ok(())
+
}
modified radicle-cli/src/commands/help.rs
@@ -19,6 +19,7 @@ const COMMANDS: &[Help] = &[
    rad_clone::HELP,
    rad_edit::HELP,
    rad_fetch::HELP,
+
    rad_fork::HELP,
    rad_help::HELP,
    rad_id::HELP,
    rad_init::HELP,
modified radicle-cli/src/main.rs
@@ -171,6 +171,14 @@ fn run_other(exe: &str, args: &[OsString]) -> Result<(), Option<anyhow::Error>>
                args.to_vec(),
            );
        }
+
        "fork" => {
+
            term::run_command_args::<rad_fork::Options, _>(
+
                rad_fork::HELP,
+
                "Fork",
+
                rad_fork::run,
+
                args.to_vec(),
+
            );
+
        }
        "help" => {
            term::run_command_args::<rad_help::Options, _>(
                rad_help::HELP,