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,65 +1034,74 @@ 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, }
}) => { }
ui.request_pager();
let name_path = name fn cmd_config_list(
.as_ref() ui: &mut Ui,
.map_or(vec![], |name| name.split('.').collect_vec()); command: &CommandHelper,
let values = command.resolved_config_values(&name_path)?; args: &ConfigListArgs,
let mut wrote_values = false; ) -> Result<(), CommandError> {
for AnnotatedValue { ui.request_pager();
path, let name_path = args
value, .name
source, .as_ref()
is_overridden, .map_or(vec![], |name| name.split('.').collect_vec());
} in &values let values = command.resolved_config_values(&name_path)?;
{ let mut wrote_values = false;
// Remove overridden values. for AnnotatedValue {
// TODO(#1047): Allow printing overridden values via `--include-overridden`. path,
if *is_overridden { value,
continue; source,
} is_overridden,
// Skip built-ins if not included. } in &values
if !*include_defaults && *source == ConfigSource::Default { {
continue; // Remove overridden values.
} // TODO(#1047): Allow printing overridden values via `--include-overridden`.
writeln!(ui, "{}={}", path.join("."), serialize_config_value(value))?; if *is_overridden {
wrote_values = true; continue;
}
if !wrote_values {
// Note to stderr explaining why output is empty.
if let Some(name) = name {
writeln!(ui.warning(), "No matching config key for {name}")?;
} else {
writeln!(ui.warning(), "No config to list")?;
}
}
} }
ConfigSubcommand::Edit(ConfigEditArgs { config_args }) => { // Skip built-ins if not included.
let edit_path = if config_args.user { if !args.include_defaults && *source == ConfigSource::Default {
// TODO(#531): Special-case for editors that can't handle viewing directories? continue;
config_path()?.ok_or_else(|| user_error("No repo config path found to edit"))? }
} else if config_args.repo { writeln!(ui, "{}={}", path.join("."), serialize_config_value(value))?;
let workspace_command = command.workspace_helper(ui)?; wrote_values = true;
let workspace_path = workspace_command.workspace_root(); }
WorkspaceLoader::init(workspace_path) if !wrote_values {
.unwrap() // Note to stderr explaining why output is empty.
.repo_path() if let Some(name) = &args.name {
.join("config.toml") writeln!(ui.warning(), "No matching config key for {name}")?;
} else { } else {
// Shouldn't be reachable unless clap ArgGroup is broken. writeln!(ui.warning(), "No config to list")?;
panic!("No config_level provided");
};
run_ui_editor(settings, &edit_path)?;
} }
} }
Ok(())
}
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?
config_path()?.ok_or_else(|| user_error("No repo config path found to edit"))?
} else if args.config_args.repo {
let workspace_command = command.workspace_helper(ui)?;
let workspace_path = workspace_command.workspace_root();
WorkspaceLoader::init(workspace_path)
.unwrap()
.repo_path()
.join("config.toml")
} else {
// Shouldn't be reachable unless clap ArgGroup is broken.
panic!("No config_level provided");
};
run_ui_editor(command.settings(), &edit_path)?;
Ok(()) Ok(())
} }