Radish alpha
r
rad:z39mP9rQAaGmERfUMPULfPUi473tY
Radicle terminal user interface
Radicle
Git
bin(patch): Call `rad` or return JSON
Erik Kundt committed 1 year ago
commit 89ce751afa5779c91cb5c778a9f1d51c0366af1d
parent 5bd2f69
1 file changed +30 -7
modified bin/commands/patch.rs
@@ -76,6 +76,7 @@ pub enum OperationName {
pub struct ListOptions {
    mode: common::Mode,
    filter: patch::Filter,
+
    json: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
@@ -116,6 +117,7 @@ impl Args for Options {
        let mut parser = lexopt::Parser::from_args(args.clone());
        let mut op = OperationName::List;
        let mut forward = None;
+
        let mut json = false;
        let mut help = false;
        let mut repo = None;
        let mut list_opts = ListOptions::default();
@@ -127,6 +129,9 @@ impl Args for Options {
                Long("no-forward") => {
                    forward = Some(false);
                }
+
                Long("json") => {
+
                    json = true;
+
                }
                Long("help") | Short('h') => {
                    help = true;
                    // Only enable forwarding if it was not already disabled explicitly
@@ -210,9 +215,11 @@ impl Args for Options {
            return Err(Error::Help.into());
        }

+
        // Configure list options
        if list_opts.mode == common::Mode::Id {
            list_opts.filter = Filter::default().with_status(None)
        }
+
        list_opts.json = json;

        // Map local commands. Forward help and ignore `no-forward`.
        let op = match op {
@@ -247,15 +254,31 @@ pub async fn run(options: Options, ctx: impl terminal::Context) -> anyhow::Resul
            let rid = options.repo.unwrap_or(rid);

            // Run TUI with patch list interface
-
            let selection = interface::list(opts, profile, rid).await?;
-
            let selection = selection
-
                .map(|o| serde_json::to_string(&o).unwrap_or_default())
-
                .unwrap_or_default();
+
            let selection = interface::list(opts.clone(), profile, rid).await?;
+

+
            if opts.json {
+
                let selection = selection
+
                    .map(|o| serde_json::to_string(&o).unwrap_or_default())
+
                    .unwrap_or_default();
+

+
                log::info!("About to print to `stderr`: {}", selection);
+
                log::info!("Exiting patch list interface..");
+

+
                eprint!("{selection}");
+
            } else if let Some(selection) = selection {
+
                let mut args = vec![];

-
            log::info!("About to print to `stderr`: {}", selection);
-
            log::info!("Exiting patch list interface..");
+
                if let Some(operation) = selection.operation {
+
                    args.push(operation.to_string());
+
                }
+
                if let Some(id) = selection.ids.first() {
+
                    args.push(format!("{id}"));
+
                }

-
            eprint!("{selection}");
+
                let args = args.into_iter().map(OsString::from).collect::<Vec<_>>();
+

+
                let _ = crate::terminal::run_rad("patch", &args);
+
            }
        }
        Operation::Review { ref opts } => {
            log::info!("Starting patch review interface in project {rid}..");