cli: split up cmd_util() into one function per subcommand

This matches how most other commands with subcommands are handled.
This commit is contained in:
Martin von Zweigbergk 2023-12-01 21:09:28 -08:00 committed by Martin von Zweigbergk
parent 35f718f212
commit 5e0b14a8bd

View file

@ -71,30 +71,50 @@ pub(crate) fn cmd_util(
subcommand: &UtilCommand, subcommand: &UtilCommand,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
match subcommand { match subcommand {
UtilCommand::Completion(completion_args) => { 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 app = command.app().clone();
let mut buf = vec![]; let mut buf = vec![];
let shell = if completion_args.zsh { let shell = if args.zsh {
clap_complete::Shell::Zsh clap_complete::Shell::Zsh
} else if completion_args.fish { } else if args.fish {
clap_complete::Shell::Fish clap_complete::Shell::Fish
} else { } else {
clap_complete::Shell::Bash clap_complete::Shell::Bash
}; };
clap_complete::generate(shell, &mut app, "jj", &mut buf); clap_complete::generate(shell, &mut app, "jj", &mut buf);
ui.stdout_formatter().write_all(&buf)?; ui.stdout_formatter().write_all(&buf)?;
} Ok(())
UtilCommand::Mangen(_mangen_args) => { }
fn cmd_util_mangen(
ui: &mut Ui,
command: &CommandHelper,
_args: &UtilMangenArgs,
) -> Result<(), CommandError> {
let mut buf = vec![]; let mut buf = vec![];
let man = clap_mangen::Man::new(command.app().clone()); let man = clap_mangen::Man::new(command.app().clone());
man.render(&mut buf)?; man.render(&mut buf)?;
ui.stdout_formatter().write_all(&buf)?; ui.stdout_formatter().write_all(&buf)?;
} Ok(())
UtilCommand::ConfigSchema(_config_schema_args) => { }
fn cmd_util_config_schema(
ui: &mut Ui,
_command: &CommandHelper,
_args: &UtilConfigSchemaArgs,
) -> Result<(), CommandError> {
// TODO(#879): Consider generating entire schema dynamically vs. static file. // TODO(#879): Consider generating entire schema dynamically vs. static file.
let buf = include_bytes!("../config-schema.json"); let buf = include_bytes!("../config-schema.json");
ui.stdout_formatter().write_all(buf)?; ui.stdout_formatter().write_all(buf)?;
}
}
Ok(()) Ok(())
} }