Skip to content

Commit

Permalink
cli: split up cmd_util() into one function per subcommand
Browse files Browse the repository at this point in the history
This matches how most other commands with subcommands are handled.
  • Loading branch information
martinvonz committed Dec 2, 2023
1 parent 5f4e36a commit d7f395f
Showing 1 changed file with 56 additions and 29 deletions.
85 changes: 56 additions & 29 deletions cli/src/commands/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,62 @@ 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::Gc(_gc_args) => {
let workspace_command = command.workspace_helper(ui)?;
let backend = workspace_command.repo().store().backend();
backend.gc().map_err(|err| user_error(err.to_string()))?;
}
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::Gc(args) => cmd_util_gc(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_gc(
ui: &mut Ui,
command: &CommandHelper,
_args: &UtilGcArgs,
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let backend = workspace_command.repo().store().backend();
backend.gc().map_err(|err| user_error(err.to_string()))?;
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(())
}

0 comments on commit d7f395f

Please sign in to comment.