Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: Bring back the target enum
Lorenz Leutgeb committed 6 months ago
commit 3d471c69717881387ce70fd1f8e89a4dde163b9c
parent 0b73011b768a0fbdc0e92242d5b3eb73c787c2f7
2 files changed +157 -129
modified crates/radicle-cli/src/commands/inspect.rs
@@ -3,7 +3,7 @@
mod args;

pub use args::Args;
-
pub(crate) use args::TargetArgs;
+
pub(crate) use args::Target;
pub(crate) use args::ABOUT;

use std::collections::HashMap;
@@ -41,22 +41,9 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
            .context("Current directory is not a Radicle repository")?,
    };

-
    let target = match args.target {
-
        None => TargetArgs {
-
            rid: true,
-
            payload: false,
-
            refs: false,
-
            sigrefs: false,
-
            identity: false,
-
            visibility: false,
-
            delegates: false,
-
            policy: false,
-
            history: false,
-
        },
-
        Some(target) => target,
-
    };
+
    let target = args.target.into();

-
    if target.rid {
+
    if matches!(target, Target::RepoId) {
        term::info!("{}", term::format::highlight(rid.urn()));
        return Ok(());
    }
@@ -64,124 +51,135 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
    let profile = ctx.profile()?;
    let storage = &profile.storage;

-
    if target.refs {
-
        let (repo, _) = repo(rid, storage)?;
-
        refs(&repo)?;
-
    } else if target.payload {
-
        let (_, doc) = repo(rid, storage)?;
-
        json::to_pretty(&doc.payload(), Path::new("radicle.json"))?.print();
-
    } else if target.identity {
-
        let (_, doc) = repo(rid, storage)?;
-
        json::to_pretty(&*doc, Path::new("radicle.json"))?.print();
-
    } else if target.sigrefs {
-
        let (repo, _) = repo(rid, storage)?;
-
        for remote in repo.remote_ids()? {
-
            let remote = remote?;
-
            let refs = RefsAt::new(&repo, remote)?;
-

-
            println!(
-
                "{:<48} {}",
-
                term::format::tertiary(remote.to_human()),
-
                term::format::secondary(refs.at)
-
            );
+
    match target {
+
        Target::Refs => {
+
            let (repo, _) = repo(rid, storage)?;
+
            refs(&repo)?;
+
        }
+
        Target::Payload => {
+
            let (_, doc) = repo(rid, storage)?;
+
            json::to_pretty(&doc.payload(), Path::new("radicle.json"))?.print();
+
        }
+
        Target::Identity => {
+
            let (_, doc) = repo(rid, storage)?;
+
            json::to_pretty(&*doc, Path::new("radicle.json"))?.print();
        }
-
    } else if target.policy {
-
        let policies = profile.policies()?;
-
        let seed = policies.seed_policy(&rid)?;
-
        match seed.policy {
-
            SeedingPolicy::Allow { scope } => {
+
        Target::Sigrefs => {
+
            let (repo, _) = repo(rid, storage)?;
+
            for remote in repo.remote_ids()? {
+
                let remote = remote?;
+
                let refs = RefsAt::new(&repo, remote)?;
+

                println!(
-
                    "Repository {} is {} with scope {}",
-
                    term::format::tertiary(&rid),
-
                    term::format::positive("being seeded"),
-
                    term::format::dim(format!("`{scope}`"))
+
                    "{:<48} {}",
+
                    term::format::tertiary(remote.to_human()),
+
                    term::format::secondary(refs.at)
                );
            }
-
            SeedingPolicy::Block => {
-
                println!(
-
                    "Repository {} is {}",
-
                    term::format::tertiary(&rid),
-
                    term::format::negative("not being seeded"),
-
                );
+
        }
+
        Target::Policy => {
+
            let policies = profile.policies()?;
+
            let seed = policies.seed_policy(&rid)?;
+
            match seed.policy {
+
                SeedingPolicy::Allow { scope } => {
+
                    println!(
+
                        "Repository {} is {} with scope {}",
+
                        term::format::tertiary(&rid),
+
                        term::format::positive("being seeded"),
+
                        term::format::dim(format!("`{scope}`"))
+
                    );
+
                }
+
                SeedingPolicy::Block => {
+
                    println!(
+
                        "Repository {} is {}",
+
                        term::format::tertiary(&rid),
+
                        term::format::negative("not being seeded"),
+
                    );
+
                }
            }
        }
-
    } else if target.delegates {
-
        let (_, doc) = repo(rid, storage)?;
-
        let aliases = profile.aliases();
-
        for did in doc.delegates().iter() {
-
            if let Some(alias) = aliases.alias(did) {
+
        Target::Delegates => {
+
            let (_, doc) = repo(rid, storage)?;
+
            let aliases = profile.aliases();
+
            for did in doc.delegates().iter() {
+
                if let Some(alias) = aliases.alias(did) {
+
                    println!(
+
                        "{} {}",
+
                        term::format::tertiary(&did),
+
                        term::format::parens(term::format::dim(alias))
+
                    );
+
                } else {
+
                    println!("{}", term::format::tertiary(&did));
+
                }
+
            }
+
        }
+
        Target::Visibility => {
+
            let (_, doc) = repo(rid, storage)?;
+
            println!("{}", term::format::visibility(doc.visibility()));
+
        }
+
        Target::History => {
+
            let (repo, _) = repo(rid, storage)?;
+
            let identity = Identity::load(&repo)?;
+
            let head = repo.identity_head()?;
+
            let history = repo.revwalk(head)?;
+

+
            for oid in history {
+
                let oid = oid?.into();
+
                let tip = repo.commit(oid)?;
+

+
                let Some(revision) = identity.revision(&tip.id().into()) else {
+
                    continue;
+
                };
+
                if !revision.is_accepted() {
+
                    continue;
+
                }
+
                let doc = &revision.doc;
+
                let timezone = if tip.time().sign() == '+' {
+
                    #[allow(deprecated)]
+
                    FixedOffset::east(tip.time().offset_minutes() * 60)
+
                } else {
+
                    #[allow(deprecated)]
+
                    FixedOffset::west(tip.time().offset_minutes() * 60)
+
                };
+
                let time = DateTime::<Utc>::from(
+
                    std::time::UNIX_EPOCH
+
                        + std::time::Duration::from_secs(tip.time().seconds() as u64),
+
                )
+
                .with_timezone(&timezone)
+
                .to_rfc2822();
+

                println!(
                    "{} {}",
-
                    term::format::tertiary(&did),
-
                    term::format::parens(term::format::dim(alias))
+
                    term::format::yellow("commit"),
+
                    term::format::yellow(oid),
                );
-
            } else {
-
                println!("{}", term::format::tertiary(&did));
-
            }
-
        }
-
    } else if target.visibility {
-
        let (_, doc) = repo(rid, storage)?;
-
        println!("{}", term::format::visibility(doc.visibility()));
-
    } else if target.history {
-
        let (repo, _) = repo(rid, storage)?;
-
        let identity = Identity::load(&repo)?;
-
        let head = repo.identity_head()?;
-
        let history = repo.revwalk(head)?;
-

-
        for oid in history {
-
            let oid = oid?.into();
-
            let tip = repo.commit(oid)?;
-

-
            let Some(revision) = identity.revision(&tip.id().into()) else {
-
                continue;
-
            };
-
            if !revision.is_accepted() {
-
                continue;
-
            }
-
            let doc = &revision.doc;
-
            let timezone = if tip.time().sign() == '+' {
-
                #[allow(deprecated)]
-
                FixedOffset::east(tip.time().offset_minutes() * 60)
-
            } else {
-
                #[allow(deprecated)]
-
                FixedOffset::west(tip.time().offset_minutes() * 60)
-
            };
-
            let time = DateTime::<Utc>::from(
-
                std::time::UNIX_EPOCH + std::time::Duration::from_secs(tip.time().seconds() as u64),
-
            )
-
            .with_timezone(&timezone)
-
            .to_rfc2822();
-

-
            println!(
-
                "{} {}",
-
                term::format::yellow("commit"),
-
                term::format::yellow(oid),
-
            );
-
            if let Ok(parent) = tip.parent_id(0) {
-
                println!("parent {parent}");
-
            }
-
            println!("blob   {}", revision.blob);
-
            println!("date   {time}");
-
            println!();
-

-
            if let Some(msg) = tip.message() {
-
                for line in msg.lines() {
-
                    if line.is_empty() {
-
                        println!();
-
                    } else {
-
                        term::indented(term::format::dim(line));
+
                if let Ok(parent) = tip.parent_id(0) {
+
                    println!("parent {parent}");
+
                }
+
                println!("blob   {}", revision.blob);
+
                println!("date   {time}");
+
                println!();
+

+
                if let Some(msg) = tip.message() {
+
                    for line in msg.lines() {
+
                        if line.is_empty() {
+
                            println!();
+
                        } else {
+
                            term::indented(term::format::dim(line));
+
                        }
                    }
+
                    term::blank();
+
                }
+
                for line in json::to_pretty(&doc, Path::new("radicle.json"))? {
+
                    println!(" {line}");
                }
-
                term::blank();
-
            }
-
            for line in json::to_pretty(&doc, Path::new("radicle.json"))? {
-
                println!(" {line}");
-
            }

-
            println!();
+
                println!();
+
            }
+
        }
+
        Target::RepoId => {
+
            // Handled above.
        }
-
    } else if target.rid {
-
        // Handled above.
    }

    Ok(())
modified crates/radicle-cli/src/commands/inspect/args.rs
@@ -45,14 +45,44 @@ pub(crate) struct TargetArgs {
    pub(crate) visibility: bool,
}

+
pub(super) enum Target {
+
    Delegates,
+
    History,
+
    Identity,
+
    Payload,
+
    Policy,
+
    Refs,
+
    RepoId,
+
    Sigrefs,
+
    Visibility,
+
}

-
    /// Inspect the repository's seeding policy
-
    #[arg(long)]
-
    pub(crate) policy: bool,
-

-
    /// Show the history of the repository identity document
-
    #[arg(long)]
-
    pub(crate) history: bool,
+
impl From<TargetArgs> for Target {
+
    fn from(args: TargetArgs) -> Self {
+
        match (
+
            args.delegates,
+
            args.history,
+
            args.identity,
+
            args.payload,
+
            args.policy,
+
            args.refs,
+
            args.rid,
+
            args.sigrefs,
+
            args.visibility,
+
        ) {
+
            (true, false, false, false, false, false, false, false, false) => Target::Delegates,
+
            (false, true, false, false, false, false, false, false, false) => Target::History,
+
            (false, false, true, false, false, false, false, false, false) => Target::Identity,
+
            (false, false, false, true, false, false, false, false, false) => Target::Payload,
+
            (false, false, false, false, true, false, false, false, false) => Target::Policy,
+
            (false, false, false, false, false, true, false, false, false) => Target::Refs,
+
            (false, false, false, false, false, false, true, false, false)
+
            | (false, false, false, false, false, false, false, false, false) => Target::RepoId,
+
            (false, false, false, false, false, false, false, true, false) => Target::Sigrefs,
+
            (false, false, false, false, false, false, false, false, true) => Target::Visibility,
+
            _ => unreachable!(),
+
        }
+
    }
}

#[derive(Debug, Parser)]
@@ -63,5 +93,5 @@ pub struct Args {
    pub(crate) repo: Option<String>,

    #[clap(flatten)]
-
    pub(crate) target: Option<TargetArgs>,
+
    pub(crate) target: TargetArgs,
}