ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: reuse ConfigLevelArgs for "config list --user/--repo"

This is a bit tricky, but we can reconfigure group attributes by using
mut_group().

https://docs.rs/clap/latest/clap/struct.Command.html#method.mut_group
This commit is contained in:
Yuya Nishihara 2024-05-12 22:10:34 +09:00
parent 717b42245c
commit 728e9e0772

View file

@ -42,14 +42,17 @@ pub(crate) struct ConfigLevelArgs {
} }
impl ConfigLevelArgs { impl ConfigLevelArgs {
fn get_source_kind(&self) -> ConfigSource { fn expect_source_kind(&self) -> ConfigSource {
self.get_source_kind().expect("No config_level provided")
}
fn get_source_kind(&self) -> Option<ConfigSource> {
if self.user { if self.user {
ConfigSource::User Some(ConfigSource::User)
} else if self.repo { } else if self.repo {
ConfigSource::Repo Some(ConfigSource::Repo)
} else { } else {
// Shouldn't be reachable unless clap ArgGroup is broken. None
panic!("No config_level provided");
} }
} }
} }
@ -77,23 +80,19 @@ pub(crate) enum ConfigCommand {
/// List variables set in config file, along with their values. /// List variables set in config file, along with their values.
#[derive(clap::Args, Clone, Debug)] #[derive(clap::Args, Clone, Debug)]
#[command(group(clap::ArgGroup::new("specific").args(&["repo", "user"])))] #[command(mut_group("config_level", |g| g.required(false)))]
pub(crate) struct ConfigListArgs { pub(crate) struct ConfigListArgs {
/// An optional name of a specific config option to look up. /// An optional name of a specific config option to look up.
#[arg(value_parser = NonEmptyStringValueParser::new())] #[arg(value_parser = NonEmptyStringValueParser::new())]
pub name: Option<String>, pub name: Option<String>,
/// Whether to explicitly include built-in default values in the list. /// Whether to explicitly include built-in default values in the list.
#[arg(long, conflicts_with = "specific")] #[arg(long, conflicts_with = "config_level")]
pub include_defaults: bool, pub include_defaults: bool,
/// Allow printing overridden values. /// Allow printing overridden values.
#[arg(long)] #[arg(long)]
pub include_overridden: bool, pub include_overridden: bool,
/// Target the user-level config #[command(flatten)]
#[arg(long)] pub level: ConfigLevelArgs,
user: bool,
/// Target the repo-level config
#[arg(long)]
repo: bool,
// TODO(#1047): Support --show-origin using LayeredConfigs. // TODO(#1047): Support --show-origin using LayeredConfigs.
/// Render each variable using the given template /// Render each variable using the given template
/// ///
@ -108,19 +107,6 @@ pub(crate) struct ConfigListArgs {
template: Option<String>, template: Option<String>,
} }
impl ConfigListArgs {
fn get_source_kind(&self) -> Option<ConfigSource> {
if self.user {
Some(ConfigSource::User)
} else if self.repo {
Some(ConfigSource::Repo)
} else {
//List all variables
None
}
}
}
/// Get the value of a given config option. /// Get the value of a given config option.
/// ///
/// Unlike `jj config list`, the result of `jj config get` is printed without /// Unlike `jj config list`, the result of `jj config get` is printed without
@ -239,7 +225,7 @@ pub(crate) fn cmd_config_list(
continue; continue;
} }
if let Some(target_source) = args.get_source_kind() { if let Some(target_source) = args.level.get_source_kind() {
if target_source != annotated.source { if target_source != annotated.source {
continue; continue;
} }
@ -309,7 +295,7 @@ pub(crate) fn cmd_config_set(
command: &CommandHelper, command: &CommandHelper,
args: &ConfigSetArgs, args: &ConfigSetArgs,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(&args.level.get_source_kind(), command)?; let config_path = get_new_config_file_path(&args.level.expect_source_kind(), command)?;
if config_path.is_dir() { if config_path.is_dir() {
return Err(user_error(format!( return Err(user_error(format!(
"Can't set config in path {path} (dirs not supported)", "Can't set config in path {path} (dirs not supported)",
@ -325,7 +311,7 @@ pub(crate) fn cmd_config_edit(
command: &CommandHelper, command: &CommandHelper,
args: &ConfigEditArgs, args: &ConfigEditArgs,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(&args.level.get_source_kind(), command)?; let config_path = get_new_config_file_path(&args.level.expect_source_kind(), command)?;
run_ui_editor(command.settings(), &config_path) run_ui_editor(command.settings(), &config_path)
} }
@ -335,7 +321,7 @@ pub(crate) fn cmd_config_path(
command: &CommandHelper, command: &CommandHelper,
args: &ConfigPathArgs, args: &ConfigPathArgs,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(&args.level.get_source_kind(), command)?; let config_path = get_new_config_file_path(&args.level.expect_source_kind(), command)?;
writeln!( writeln!(
ui.stdout(), ui.stdout(),
"{}", "{}",