From 40467ce13d4a6f4c12712569ab7fbfba66a84415 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 26 Nov 2024 11:50:25 -0500 Subject: [PATCH] add optional command_name config option --- crates/icepick/src/cli/mod.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/icepick/src/cli/mod.rs b/crates/icepick/src/cli/mod.rs index a42a061..99dbb14 100644 --- a/crates/icepick/src/cli/mod.rs +++ b/crates/icepick/src/cli/mod.rs @@ -17,6 +17,7 @@ pub fn get_command(bin_name: &str) -> (&str, Vec<&str>) { #[derive(Serialize, Deserialize, Debug)] struct ModuleConfig { name: String, + command_name: Option, derivation_prefix: String, } @@ -42,9 +43,9 @@ pub fn do_cli_thing() { let mut icepick_command = command!(); // NOTE: this needs to be .cloned(), since commands is leaked to be 'static // and coin_bin otherwise wouldn't live long enough - for coin_bin in config.modules.iter().map(|m| &m.name).cloned() { - // try to run the "help" operation on any bins. - let bin = format!("icepick-{coin_bin}"); + for module in &config.modules { + let module_name = &module.name; + let bin = module.command_name.clone().unwrap_or_else(|| format!("icepick-{module_name}")); let (command, args) = get_command(&bin); let mut child = Command::new(command) .args(args) @@ -62,12 +63,12 @@ pub fn do_cli_thing() { let output = child.wait_with_output().unwrap().stdout; let operations: Vec = serde_json::from_slice::>(&output) .expect("successful deserialization of operation"); - commands.push((coin_bin, operations)); + commands.push((module_name.clone(), bin, operations)); } let commands = commands.leak(); for command in commands.iter() { let mut subcommand = clap::Command::new(command.0.as_str()); - for op in &command.1 { + for op in &command.2 { let mut op_command = clap::Command::new(&op.name).about(&op.description); for arg in &op.arguments { let mut op_arg = clap::Arg::new(&arg.name).help(arg.description.as_str()); @@ -97,8 +98,8 @@ pub fn do_cli_thing() { if let Some((subcommand, matches)) = matches.subcommand() { if let Some(operation) = commands .iter() - .find(|(name, _)| *name == module) - .and_then(|(_, operations)| operations.iter().find(|o| o.name == subcommand)) + .find(|(name, ..)| *name == module) + .and_then(|(.., operations)| operations.iter().find(|o| o.name == subcommand)) { let mut args = std::collections::HashMap::<&String, Option<&String>>::with_capacity( operation.arguments.len(), @@ -112,8 +113,14 @@ pub fn do_cli_thing() { "derived-keys": [], "blob": blob, }); - let bin = format!("icepick-{module}"); - let (command, args) = get_command(&bin); + let bin = commands.iter().find_map(|(fmodule, fcommand, _)| { + if fmodule == module { + Some(fcommand) + } else { + None + } + }).expect("previously found module should exist in new search"); + let (command, args) = get_command(bin); let mut child = Command::new(command) .args(args) .stdin(Stdio::piped())