From 89ce751afa5779c91cb5c778a9f1d51c0366af1d Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Tue, 28 Jan 2025 14:52:47 +0100 Subject: [PATCH] bin(patch): Call `rad` or return JSON --- bin/commands/patch.rs | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/bin/commands/patch.rs b/bin/commands/patch.rs index 8565766a..325439b9 100644 --- a/bin/commands/patch.rs +++ b/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?; - log::info!("About to print to `stderr`: {}", selection); - log::info!("Exiting patch list interface.."); + if opts.json { + let selection = selection + .map(|o| serde_json::to_string(&o).unwrap_or_default()) + .unwrap_or_default(); - eprint!("{selection}"); + 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![]; + + if let Some(operation) = selection.operation { + args.push(operation.to_string()); + } + if let Some(id) = selection.ids.first() { + args.push(format!("{id}")); + } + + let args = args.into_iter().map(OsString::from).collect::>(); + + let _ = crate::terminal::run_rad("patch", &args); + } } Operation::Review { ref opts } => { log::info!("Starting patch review interface in project {rid}..");