add optional command_name config option

This commit is contained in:
Ryan Heywood 2024-11-26 11:50:25 -05:00
parent 9f9deb107e
commit 40467ce13d
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 16 additions and 9 deletions

View File

@ -17,6 +17,7 @@ pub fn get_command(bin_name: &str) -> (&str, Vec<&str>) {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct ModuleConfig { struct ModuleConfig {
name: String, name: String,
command_name: Option<String>,
derivation_prefix: String, derivation_prefix: String,
} }
@ -42,9 +43,9 @@ pub fn do_cli_thing() {
let mut icepick_command = command!(); let mut icepick_command = command!();
// NOTE: this needs to be .cloned(), since commands is leaked to be 'static // NOTE: this needs to be .cloned(), since commands is leaked to be 'static
// and coin_bin otherwise wouldn't live long enough // and coin_bin otherwise wouldn't live long enough
for coin_bin in config.modules.iter().map(|m| &m.name).cloned() { for module in &config.modules {
// try to run the "help" operation on any bins. let module_name = &module.name;
let bin = format!("icepick-{coin_bin}"); let bin = module.command_name.clone().unwrap_or_else(|| format!("icepick-{module_name}"));
let (command, args) = get_command(&bin); let (command, args) = get_command(&bin);
let mut child = Command::new(command) let mut child = Command::new(command)
.args(args) .args(args)
@ -62,12 +63,12 @@ pub fn do_cli_thing() {
let output = child.wait_with_output().unwrap().stdout; let output = child.wait_with_output().unwrap().stdout;
let operations: Vec<Operation> = serde_json::from_slice::<Vec<Operation>>(&output) let operations: Vec<Operation> = serde_json::from_slice::<Vec<Operation>>(&output)
.expect("successful deserialization of operation"); .expect("successful deserialization of operation");
commands.push((coin_bin, operations)); commands.push((module_name.clone(), bin, operations));
} }
let commands = commands.leak(); let commands = commands.leak();
for command in commands.iter() { for command in commands.iter() {
let mut subcommand = clap::Command::new(command.0.as_str()); 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); let mut op_command = clap::Command::new(&op.name).about(&op.description);
for arg in &op.arguments { for arg in &op.arguments {
let mut op_arg = clap::Arg::new(&arg.name).help(arg.description.as_str()); 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((subcommand, matches)) = matches.subcommand() {
if let Some(operation) = commands if let Some(operation) = commands
.iter() .iter()
.find(|(name, _)| *name == module) .find(|(name, ..)| *name == module)
.and_then(|(_, operations)| operations.iter().find(|o| o.name == subcommand)) .and_then(|(.., operations)| operations.iter().find(|o| o.name == subcommand))
{ {
let mut args = std::collections::HashMap::<&String, Option<&String>>::with_capacity( let mut args = std::collections::HashMap::<&String, Option<&String>>::with_capacity(
operation.arguments.len(), operation.arguments.len(),
@ -112,8 +113,14 @@ pub fn do_cli_thing() {
"derived-keys": [], "derived-keys": [],
"blob": blob, "blob": blob,
}); });
let bin = format!("icepick-{module}"); let bin = commands.iter().find_map(|(fmodule, fcommand, _)| {
let (command, args) = get_command(&bin); 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) let mut child = Command::new(command)
.args(args) .args(args)
.stdin(Stdio::piped()) .stdin(Stdio::piped())