Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: Deduplicate args for operation
Lorenz Leutgeb committed 6 months ago
commit 0fa33ef489b199f909c1a88bafa533be1121bec3
parent e0f6911c9e68e20ef3dcce4969e9abe50334e03d
2 files changed +32 -47
modified crates/radicle-cli/src/commands/cob.rs
@@ -49,27 +49,24 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
        Create(args::Create {
            repo,
            type_name,
-
            message,
-
            embed_files,
-
            embed_hashes,
-
            actions,
+
            operation,
        }) => {
            let signer = &profile.signer()?;
            let repo = storage.repository_mut(repo)?;
-
            let reader = buf_reader(actions)?;
-
            let embeds = embeds(&repo, embed_files, embed_hashes)?;
+
            let reader = buf_reader(operation.actions)?;
+
            let embeds = embeds(&repo, operation.embed_files, operation.embed_hashes)?;

            let oid = match type_name {
                Patch => {
                    let store: Store<cob::patch::Patch, _> = Store::open(&repo)?;
                    let actions = read_jsonl_actions(reader)?;
-
                    let (oid, _) = store.create(&message, actions, embeds, signer)?;
+
                    let (oid, _) = store.create(&operation.message, actions, embeds, signer)?;
                    oid
                }
                Issue => {
                    let store: Store<cob::issue::Issue, _> = Store::open(&repo)?;
                    let actions = read_jsonl_actions(reader)?;
-
                    let (oid, _) = store.create(&message, actions, embeds, signer)?;
+
                    let (oid, _) = store.create(&operation.message, actions, embeds, signer)?;
                    oid
                }
                Identity => anyhow::bail!(
@@ -80,7 +77,7 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
                    let store: Store<cob::external::External, _> =
                        Store::open_for(&type_name, &repo)?;
                    let actions = read_jsonl_actions(reader)?;
-
                    let (oid, _) = store.create(&message, actions, embeds, signer)?;
+
                    let (oid, _) = store.create(&operation.message, actions, embeds, signer)?;
                    oid
                }
            };
@@ -173,24 +170,21 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
            repo,
            type_name,
            object,
-
            message,
-
            embed_files,
-
            embed_hashes,
-
            actions,
-
            ..
+
            operation,
+
            format: _,
        }) => {
            let signer = &profile.signer()?;
            let repo = storage.repository_mut(repo)?;
-
            let reader = buf_reader(actions)?;
+
            let reader = buf_reader(operation.actions)?;
            let oid = object.resolve::<radicle::git::Oid>(&repo.backend)?.into();
-
            let embeds = embeds(&repo, embed_files, embed_hashes)?;
+
            let embeds = embeds(&repo, operation.embed_files, operation.embed_hashes)?;

            let oid = match type_name {
                Patch => {
                    let actions: Vec<cob::patch::Action> = read_jsonl(reader)?;
                    let mut patches = profile.patches_mut(&repo)?;
                    let mut patch = patches.get_mut(&oid)?;
-
                    patch.transaction(&message, &*profile.signer()?, |tx| {
+
                    patch.transaction(&operation.message, &*profile.signer()?, |tx| {
                        tx.extend(actions)?;
                        tx.embed(embeds)?;
                        Ok(())
@@ -200,7 +194,7 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
                    let actions: Vec<cob::issue::Action> = read_jsonl(reader)?;
                    let mut issues = profile.issues_mut(&repo)?;
                    let mut issue = issues.get_mut(&oid)?;
-
                    issue.transaction(&message, &*profile.signer()?, |tx| {
+
                    issue.transaction(&operation.message, &*profile.signer()?, |tx| {
                        tx.extend(actions)?;
                        tx.embed(embeds)?;
                        Ok(())
@@ -215,7 +209,7 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
                    let actions: Vec<Action> = read_jsonl(reader)?;
                    let mut store: Store<External, _> = Store::open_for(&type_name, &repo)?;
                    let tx = cob::store::Transaction::new(type_name.clone(), actions, embeds);
-
                    let (_, oid) = tx.commit(&message, oid, &mut store, signer)?;
+
                    let (_, oid) = tx.commit(&operation.message, oid, &mut store, signer)?;
                    oid
                }
            };
modified crates/radicle-cli/src/commands/cob/args.rs
@@ -91,16 +91,8 @@ pub(super) enum Command {
}

#[derive(Parser, Debug)]
-
pub(super) struct Create {
-
    /// Repository ID of the repository to operate on
-
    #[arg(long, short, value_name = "RID")]
-
    pub(super) repo: RepoId,
-

-
    /// Typename of the object to create
-
    #[arg(long = "type", short, value_name = "TYPENAME")]
-
    pub(super) type_name: FilteredTypeName,
-

-
    /// Attach a message
+
pub(super) struct Operation {
+
    /// Message describing the operation
    #[arg(long, short)]
    pub(super) message: String,

@@ -112,13 +104,26 @@ pub(super) struct Create {
    #[arg(long = "embed-hash", value_names = ["NAME", "OID"], num_args = 2)]
    pub(super) embed_hashes: Vec<String>,

-
    /// A file that contains a sequence of actions for the COB, in JSON Lines
-
    /// format.
+
    /// A file that contains a sequence actions (in JSONL format) to apply.
    #[arg(value_name = "FILENAME")]
    pub(super) actions: PathBuf,
}

#[derive(Parser, Debug)]
+
pub(super) struct Create {
+
    /// Repository ID of the repository to operate on
+
    #[arg(long, short, value_name = "RID")]
+
    pub(super) repo: RepoId,
+

+
    /// Typename of the object to create
+
    #[arg(long = "type", short, value_name = "TYPENAME")]
+
    pub(super) type_name: FilteredTypeName,
+

+
    #[clap(flatten)]
+
    pub(super) operation: Operation,
+
}
+

+
#[derive(Parser, Debug)]
pub(super) struct Update {
    /// Repository ID of the repository to operate on
    #[arg(long, short)]
@@ -132,27 +137,13 @@ pub(super) struct Update {
    #[arg(long, short, value_name = "OID")]
    pub(super) object: Rev,

-
    /// Attach a message
-
    #[arg(long, short)]
-
    pub(super) message: String,
-

-
    /// Supply embed of given name via file at given path
-
    #[arg(long = "embed-file", value_names = ["NAME", "PATH"], num_args = 2)]
-
    pub(super) embed_files: Vec<String>,
-

-
    /// Supply embed of given name via object ID of blob
-
    #[arg(long = "embed-hash", value_names = ["NAME", "OID"], num_args = 2)]
-
    pub(super) embed_hashes: Vec<String>,
-

    // TODO(finto): `Format` is unused and is obsolete for this command
    /// Desired output format
    #[arg(long, default_value_t = Format::Json, value_parser = FormatParser)]
    pub(super) format: Format,

-
    /// A file that contains a sequence of actions for the COB, in JSON Lines
-
    /// format.
-
    #[arg(value_name = "FILENAME")]
-
    pub(super) actions: PathBuf,
+
    #[clap(flatten)]
+
    pub(super) operation: Operation,
}

/// A precursor to [`cob::Embed`] used for parsing