Skip to content

Commit

Permalink
cli: reuse ConfigLevelArgs for "config list --user/--repo"
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yuja committed May 12, 2024
1 parent 717b422 commit 728e9e0
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions cli/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ pub(crate) struct 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 {
ConfigSource::User
Some(ConfigSource::User)
} else if self.repo {
ConfigSource::Repo
Some(ConfigSource::Repo)
} else {
// Shouldn't be reachable unless clap ArgGroup is broken.
panic!("No config_level provided");
None
}
}
}
Expand Down Expand Up @@ -77,23 +80,19 @@ pub(crate) enum ConfigCommand {

/// List variables set in config file, along with their values.
#[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 {
/// An optional name of a specific config option to look up.
#[arg(value_parser = NonEmptyStringValueParser::new())]
pub name: Option<String>,
/// 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,
/// Allow printing overridden values.
#[arg(long)]
pub include_overridden: bool,
/// Target the user-level config
#[arg(long)]
user: bool,
/// Target the repo-level config
#[arg(long)]
repo: bool,
#[command(flatten)]
pub level: ConfigLevelArgs,
// TODO(#1047): Support --show-origin using LayeredConfigs.
/// Render each variable using the given template
///
Expand All @@ -108,19 +107,6 @@ pub(crate) struct ConfigListArgs {
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.
///
/// Unlike `jj config list`, the result of `jj config get` is printed without
Expand Down Expand Up @@ -239,7 +225,7 @@ pub(crate) fn cmd_config_list(
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 {
continue;
}
Expand Down Expand Up @@ -309,7 +295,7 @@ pub(crate) fn cmd_config_set(
command: &CommandHelper,
args: &ConfigSetArgs,
) -> 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() {
return Err(user_error(format!(
"Can't set config in path {path} (dirs not supported)",
Expand All @@ -325,7 +311,7 @@ pub(crate) fn cmd_config_edit(
command: &CommandHelper,
args: &ConfigEditArgs,
) -> 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)
}

Expand All @@ -335,7 +321,7 @@ pub(crate) fn cmd_config_path(
command: &CommandHelper,
args: &ConfigPathArgs,
) -> 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!(
ui.stdout(),
"{}",
Expand Down

0 comments on commit 728e9e0

Please sign in to comment.