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

This style matches our other commands, and will probably be helpful as
the `jj config` command grows.
This commit is contained in:
Martin von Zweigbergk 2023-02-03 10:01:08 -08:00 committed by Martin von Zweigbergk
parent db817c8dcd
commit 60086100a1

View file

@ -1034,14 +1034,20 @@ fn cmd_config(
command: &CommandHelper, command: &CommandHelper,
subcommand: &ConfigSubcommand, subcommand: &ConfigSubcommand,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let settings = command.settings();
match subcommand { match subcommand {
ConfigSubcommand::List(ConfigListArgs { ConfigSubcommand::List(sub_args) => cmd_config_list(ui, command, sub_args),
name, ConfigSubcommand::Edit(sub_args) => cmd_config_edit(ui, command, sub_args),
include_defaults, }
}) => { }
fn cmd_config_list(
ui: &mut Ui,
command: &CommandHelper,
args: &ConfigListArgs,
) -> Result<(), CommandError> {
ui.request_pager(); ui.request_pager();
let name_path = name let name_path = args
.name
.as_ref() .as_ref()
.map_or(vec![], |name| name.split('.').collect_vec()); .map_or(vec![], |name| name.split('.').collect_vec());
let values = command.resolved_config_values(&name_path)?; let values = command.resolved_config_values(&name_path)?;
@ -1059,7 +1065,7 @@ fn cmd_config(
continue; continue;
} }
// Skip built-ins if not included. // Skip built-ins if not included.
if !*include_defaults && *source == ConfigSource::Default { if !args.include_defaults && *source == ConfigSource::Default {
continue; continue;
} }
writeln!(ui, "{}={}", path.join("."), serialize_config_value(value))?; writeln!(ui, "{}={}", path.join("."), serialize_config_value(value))?;
@ -1067,18 +1073,24 @@ fn cmd_config(
} }
if !wrote_values { if !wrote_values {
// Note to stderr explaining why output is empty. // Note to stderr explaining why output is empty.
if let Some(name) = name { if let Some(name) = &args.name {
writeln!(ui.warning(), "No matching config key for {name}")?; writeln!(ui.warning(), "No matching config key for {name}")?;
} else { } else {
writeln!(ui.warning(), "No config to list")?; writeln!(ui.warning(), "No config to list")?;
} }
} }
Ok(())
} }
ConfigSubcommand::Edit(ConfigEditArgs { config_args }) => {
let edit_path = if config_args.user { fn cmd_config_edit(
ui: &mut Ui,
command: &CommandHelper,
args: &ConfigEditArgs,
) -> Result<(), CommandError> {
let edit_path = if args.config_args.user {
// TODO(#531): Special-case for editors that can't handle viewing directories? // TODO(#531): Special-case for editors that can't handle viewing directories?
config_path()?.ok_or_else(|| user_error("No repo config path found to edit"))? config_path()?.ok_or_else(|| user_error("No repo config path found to edit"))?
} else if config_args.repo { } else if args.config_args.repo {
let workspace_command = command.workspace_helper(ui)?; let workspace_command = command.workspace_helper(ui)?;
let workspace_path = workspace_command.workspace_root(); let workspace_path = workspace_command.workspace_root();
WorkspaceLoader::init(workspace_path) WorkspaceLoader::init(workspace_path)
@ -1089,10 +1101,7 @@ fn cmd_config(
// Shouldn't be reachable unless clap ArgGroup is broken. // Shouldn't be reachable unless clap ArgGroup is broken.
panic!("No config_level provided"); panic!("No config_level provided");
}; };
run_ui_editor(settings, &edit_path)?; run_ui_editor(command.settings(), &edit_path)?;
}
}
Ok(()) Ok(())
} }