From 4dc41f1d474f6cbfce25ebf81102768e631aa5ca Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 2 May 2021 15:17:49 -0700 Subject: [PATCH] cli: add support for command aliases This commit adds support for defining command aliases. The aliases are read from the `[alias]` section and are expected to be TOML arrays with one element per argument. --- src/commands.rs | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index f21b78466..917b292f9 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -2185,12 +2185,52 @@ fn cmd_git( Ok(()) } +fn resolve_alias(ui: &mut Ui, args: Vec) -> Vec { + if args.len() >= 2 { + let command_name = args[1].clone(); + if let Ok(alias_definition) = ui + .settings() + .config() + .get_array(&format!("alias.{}", command_name)) + { + let mut resolved_args = vec![args[0].clone()]; + for arg in alias_definition { + match arg.into_str() { + Ok(string_arg) => resolved_args.push(string_arg), + Err(err) => { + ui.write_error(&format!( + "Warning: Ignoring bad alias definition: {:?}\n", + err + )) + .unwrap(); + return args; + } + } + } + resolved_args.extend_from_slice(&args[2..]); + return resolved_args; + } + } + args +} + pub fn dispatch(mut ui: Ui, args: I) -> i32 where I: IntoIterator, T: Into + Clone, { - let matches = get_app().get_matches_from(args); + let mut string_args: Vec = vec![]; + for arg in args { + let os_string_arg = arg.clone().into(); + if let Some(string_arg) = os_string_arg.to_str() { + string_args.push(string_arg.to_owned()); + } else { + ui.write_error("Error: Non-utf8 argument\n").unwrap(); + return 1; + } + } + let string_args = resolve_alias(&mut ui, string_args); + let matches = get_app().get_matches_from(string_args); let result = if let Some(sub_matches) = matches.subcommand_matches("init") { cmd_init(&mut ui, &matches, &sub_matches) } else if let Some(sub_matches) = matches.subcommand_matches("checkout") { @@ -2251,13 +2291,12 @@ where match result { Ok(()) => 0, Err(CommandError::UserError(message)) => { - ui.write_error(format!("Error: {}\n", message).as_str()) - .unwrap(); + ui.write_error(&format!("Error: {}\n", message)).unwrap(); 1 } Err(CommandError::BrokenPipe) => 2, Err(CommandError::InternalError(message)) => { - ui.write_error(format!("Internal error: {}\n", message).as_str()) + ui.write_error(&format!("Internal error: {}\n", message)) .unwrap(); 255 }