From 5e0b14a8bd0a782ddfad75325ff099b5be3d527c Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Fri, 1 Dec 2023 21:09:28 -0800 Subject: [PATCH] cli: split up `cmd_util()` into one function per subcommand This matches how most other commands with subcommands are handled. --- cli/src/commands/util.rs | 68 ++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/cli/src/commands/util.rs b/cli/src/commands/util.rs index 112606b4b..3a80de123 100644 --- a/cli/src/commands/util.rs +++ b/cli/src/commands/util.rs @@ -71,30 +71,50 @@ pub(crate) fn cmd_util( subcommand: &UtilCommand, ) -> Result<(), CommandError> { match subcommand { - UtilCommand::Completion(completion_args) => { - let mut app = command.app().clone(); - let mut buf = vec![]; - let shell = if completion_args.zsh { - clap_complete::Shell::Zsh - } else if completion_args.fish { - clap_complete::Shell::Fish - } else { - clap_complete::Shell::Bash - }; - clap_complete::generate(shell, &mut app, "jj", &mut buf); - ui.stdout_formatter().write_all(&buf)?; - } - UtilCommand::Mangen(_mangen_args) => { - let mut buf = vec![]; - let man = clap_mangen::Man::new(command.app().clone()); - man.render(&mut buf)?; - ui.stdout_formatter().write_all(&buf)?; - } - UtilCommand::ConfigSchema(_config_schema_args) => { - // TODO(#879): Consider generating entire schema dynamically vs. static file. - let buf = include_bytes!("../config-schema.json"); - ui.stdout_formatter().write_all(buf)?; - } + UtilCommand::Completion(args) => cmd_util_completion(ui, command, args), + UtilCommand::Mangen(args) => cmd_util_mangen(ui, command, args), + UtilCommand::ConfigSchema(args) => cmd_util_config_schema(ui, command, args), } +} + +fn cmd_util_completion( + ui: &mut Ui, + command: &CommandHelper, + args: &UtilCompletionArgs, +) -> Result<(), CommandError> { + let mut app = command.app().clone(); + let mut buf = vec![]; + let shell = if args.zsh { + clap_complete::Shell::Zsh + } else if args.fish { + clap_complete::Shell::Fish + } else { + clap_complete::Shell::Bash + }; + clap_complete::generate(shell, &mut app, "jj", &mut buf); + ui.stdout_formatter().write_all(&buf)?; + Ok(()) +} + +fn cmd_util_mangen( + ui: &mut Ui, + command: &CommandHelper, + _args: &UtilMangenArgs, +) -> Result<(), CommandError> { + let mut buf = vec![]; + let man = clap_mangen::Man::new(command.app().clone()); + man.render(&mut buf)?; + ui.stdout_formatter().write_all(&buf)?; + Ok(()) +} + +fn cmd_util_config_schema( + ui: &mut Ui, + _command: &CommandHelper, + _args: &UtilConfigSchemaArgs, +) -> Result<(), CommandError> { + // TODO(#879): Consider generating entire schema dynamically vs. static file. + let buf = include_bytes!("../config-schema.json"); + ui.stdout_formatter().write_all(buf)?; Ok(()) }