radicle-cli 0.20.0

Radicle CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::fmt;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;

use thiserror::Error;

use clap::{Parser, Subcommand};

use radicle::cob;
use radicle::git;
use radicle::prelude::*;
use radicle::storage;

use crate::git::Rev;

#[derive(Parser, Debug)]
#[command(disable_version_flag = true)]
pub struct Args {
    #[command(subcommand)]
    pub(super) command: Command,
}

#[derive(Subcommand, Debug)]
pub(super) enum Command {
    /// Create a new COB of a given type given initial actions
    Create(#[clap(flatten)] Create),

    /// List all COBs of a given type
    List {
        /// Repository ID of the repository to operate on
        #[arg(long, short, value_name = "RID")]
        repo: RepoId,

        /// Typename of the object(s) to list
        #[arg(long = "type", short, value_name = "TYPENAME")]
        type_name: cob::TypeName,
    },

    /// Print a log of all raw operations on a COB
    Log {
        /// Tepository ID of the repository to operate on
        #[arg(long, short, value_name = "RID")]
        repo: RepoId,

        /// Typename of the object(s) to show
        #[arg(long = "type", short, value_name = "TYPENAME")]
        type_name: cob::TypeName,

        /// Object ID of the object to log
        #[arg(long, short, value_name = "OID")]
        object: Rev,

        /// Desired output format
        #[arg(long, default_value_t = Format::Pretty, value_parser = FormatParser)]
        format: Format,

        /// Object ID of the commit of the operation to start iterating at
        #[arg(long, value_name = "OID")]
        from: Option<Rev>,

        /// Object ID of the commit of the operation to stop iterating at
        #[arg(long, value_name = "OID")]
        until: Option<Rev>,
    },

    /// Migrate the COB database to the latest version
    Migrate,

    /// Print the state of COBs
    Show {
        /// Repository ID of the repository to operate on
        #[arg(long, short, value_name = "RID")]
        repo: RepoId,

        /// Typename of the object(s) to show
        #[arg(long = "type", short, value_name = "TYPENAME")]
        type_name: cob::TypeName,

        /// Object ID(s) of the objects to show
        #[arg(long = "object", short, value_name = "OID", action = clap::ArgAction::Append, required = true)]
        objects: Vec<Rev>,

        /// Desired output format
        #[arg(long, default_value_t = Format::Json, value_parser = FormatParser)]
        format: Format,
    },

    /// Add actions to a COB
    Update(#[clap(flatten)] Update),
}

#[derive(Parser, Debug)]
pub(super) struct Operation {
    /// Message describing the operation
    #[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>,

    /// 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)]
    pub(super) repo: RepoId,

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

    /// Object ID of the object to update
    #[arg(long, short, value_name = "OID")]
    pub(super) object: Rev,

    // 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,

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

/// A precursor to [`cob::Embed`] used for parsing
/// that can be initialized without relying on a [`git::Repository`].
#[derive(Clone, Debug)]
pub(super) struct Embed {
    name: String,
    content: EmbedContent,
}

impl Embed {
    pub(super) fn try_into_bytes(
        self,
        repo: &storage::git::Repository,
    ) -> anyhow::Result<cob::Embed<cob::Uri>> {
        Ok(match self.content {
            EmbedContent::Hash(hash) => cob::Embed {
                name: self.name,
                content: hash.resolve::<git::Oid>(&repo.backend)?.into(),
            },
            EmbedContent::Path(path) => {
                cob::Embed::store(self.name, &fs::read(path)?, &repo.backend)?
            }
        })
    }
}

#[derive(Clone, Debug)]
pub(super) enum EmbedContent {
    Path(PathBuf),
    Hash(Rev),
}

impl From<PathBuf> for EmbedContent {
    fn from(path: PathBuf) -> Self {
        EmbedContent::Path(path)
    }
}

impl From<Rev> for EmbedContent {
    fn from(rev: Rev) -> Self {
        EmbedContent::Hash(rev)
    }
}

/// Parses a slice of all embeds as name-path or name-oid pairs as aggregated by
/// `clap`.
/// E.g. `["image", "./image.png", "code", "d87dcfe8c2b3200e78b128d9b959cfdf7063fefe"]`
/// will result a `Vec` of two [`Embed`]s.
///
/// # Panics
///
/// If the length of `values` is not divisible by 2.
pub(super) fn parse_many_embeds<T>(values: &[String]) -> impl Iterator<Item = Embed> + use<'_, T>
where
    T: From<String>,
    EmbedContent: From<T>,
{
    // `clap` ensures we have 2 values per option occurrence,
    // so we can chunk the aggregated slice exactly.
    let chunks = values.chunks_exact(2);

    assert!(chunks.remainder().is_empty());

    chunks.map(|chunk| {
        // Slice accesses will not panic, guaranteed by `chunks_exact(2)`.
        #[allow(clippy::indexing_slicing)]
        Embed {
            name: chunk[0].to_string(),
            content: EmbedContent::from(T::from(chunk[1].clone())),
        }
    })
}

#[derive(Clone, Debug, PartialEq)]
pub(super) enum Format {
    Json,
    Pretty,
}

impl fmt::Display for Format {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Format::Json => f.write_str("json"),
            Format::Pretty => f.write_str("pretty"),
        }
    }
}

#[non_exhaustive]
#[derive(Debug, Error)]
#[error("invalid format value: {0:?}")]
pub struct FormatParseError(String);

impl FromStr for Format {
    type Err = FormatParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "json" => Ok(Self::Json),
            "pretty" => Ok(Self::Pretty),
            _ => Err(FormatParseError(s.to_string())),
        }
    }
}

#[derive(Clone, Debug)]
struct FormatParser;

impl clap::builder::TypedValueParser for FormatParser {
    type Value = Format;

    fn parse_ref(
        &self,
        cmd: &clap::Command,
        arg: Option<&clap::Arg>,
        value: &std::ffi::OsStr,
    ) -> Result<Self::Value, clap::Error> {
        use clap::error::ErrorKind;

        let format = <Format as std::str::FromStr>::from_str.parse_ref(cmd, arg, value)?;
        match cmd.get_name() {
            "show" | "update" if format == Format::Pretty => Err(clap::Error::raw(
                ErrorKind::ValueValidation,
                format!("output format `{format}` is not allowed in this command"),
            )
            .with_cmd(cmd)),
            _ => Ok(format),
        }
    }

    fn possible_values(
        &self,
    ) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
        use clap::builder::PossibleValue;
        Some(Box::new(
            [PossibleValue::new("json"), PossibleValue::new("pretty")].into_iter(),
        ))
    }
}

/// A thin wrapper around [`cob::TypeName`] used for parsing.
/// Well known COB type names are captured as variants,
/// with [`FilteredTypeName::Other`] as an escape hatch for type names
/// that are not well known.
#[derive(Clone, Debug)]
pub(super) enum FilteredTypeName {
    Issue,
    Patch,
    Identity,
    Other(cob::TypeName),
}

impl AsRef<cob::TypeName> for FilteredTypeName {
    fn as_ref(&self) -> &cob::TypeName {
        match self {
            FilteredTypeName::Issue => &cob::issue::TYPENAME,
            FilteredTypeName::Patch => &cob::patch::TYPENAME,
            FilteredTypeName::Identity => &cob::identity::TYPENAME,
            FilteredTypeName::Other(value) => value,
        }
    }
}

impl From<cob::TypeName> for FilteredTypeName {
    fn from(value: cob::TypeName) -> Self {
        if value == *cob::issue::TYPENAME {
            FilteredTypeName::Issue
        } else if value == *cob::patch::TYPENAME {
            FilteredTypeName::Patch
        } else if value == *cob::identity::TYPENAME {
            FilteredTypeName::Identity
        } else {
            FilteredTypeName::Other(value)
        }
    }
}

impl std::fmt::Display for FilteredTypeName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_ref().fmt(f)
    }
}

impl std::str::FromStr for FilteredTypeName {
    type Err = cob::TypeNameParse;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(s.parse::<cob::TypeName>()?))
    }
}

#[cfg(test)]
mod test {
    use super::Args;
    use clap::error::ErrorKind;
    use clap::Parser;

    const ARGS: &[&str] = &[
        "--repo",
        "rad:z3Tr6bC7ctEg2EHmLvknUr29mEDLH",
        "--type",
        "xyz.radicle.issue",
        "--object",
        "f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354",
    ];

    #[test]
    fn should_allow_log_json_format() {
        let args = Args::try_parse_from(
            ["cob", "log", "--format", "json"]
                .iter()
                .chain(ARGS.iter())
                .collect::<Vec<_>>(),
        );
        assert!(args.is_ok())
    }

    #[test]
    fn should_allow_log_pretty_format() {
        let args = Args::try_parse_from(
            ["cob", "log", "--format", "pretty"]
                .iter()
                .chain(ARGS.iter())
                .collect::<Vec<_>>(),
        );
        assert!(args.is_ok())
    }

    #[test]
    fn should_allow_show_json_format() {
        let args = Args::try_parse_from(
            ["cob", "show", "--format", "json"]
                .iter()
                .chain(ARGS.iter())
                .collect::<Vec<_>>(),
        );
        assert!(args.is_ok())
    }

    #[test]
    fn should_allow_update_json_format() {
        let args = Args::try_parse_from(
            [
                "cob",
                "update",
                "--format",
                "json",
                "--message",
                "",
                "/dev/null",
            ]
            .iter()
            .chain(ARGS.iter())
            .collect::<Vec<_>>(),
        );
        println!("{args:?}");
        assert!(args.is_ok())
    }

    #[test]
    fn should_not_allow_show_pretty_format() {
        let err = Args::try_parse_from(["cob", "show", "--format", "pretty"]).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::ValueValidation);
    }

    #[test]
    fn should_not_allow_update_pretty_format() {
        let err = Args::try_parse_from(["cob", "update", "--format", "pretty"]).unwrap_err();
        assert_eq!(err.kind(), ErrorKind::ValueValidation);
    }
}