Skip to content

Commit

Permalink
cli: set global args to config table without re-parsing as TOML
Browse files Browse the repository at this point in the history
This should be safer than constructing a parsable TOML form.
  • Loading branch information
yuja committed Dec 13, 2024
1 parent e1ab247 commit c29b5a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 15 additions & 6 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3276,19 +3276,28 @@ fn handle_early_args(
)
.ignore_errors(true)
.try_get_matches_from(args)?;
let mut args: EarlyArgs = EarlyArgs::from_arg_matches(&early_matches).unwrap();
let args = EarlyArgs::from_arg_matches(&early_matches).unwrap();

let old_layers_len = config.layers().len();
config.extend_layers(parse_config_args(&args.config_toml)?);

// Command arguments overrides any other configuration including the
// variables loaded from --config* arguments.
let mut layer = ConfigLayer::empty(ConfigSource::CommandArg);
if let Some(choice) = args.color {
args.config_toml.push(format!(r#"ui.color="{choice}""#));
layer.set_value("ui.color", choice.to_string()).unwrap();
}
if args.quiet.unwrap_or_default() {
args.config_toml.push(r#"ui.quiet=true"#.to_string());
layer.set_value("ui.quiet", true).unwrap();
}
if args.no_pager.unwrap_or_default() {
args.config_toml.push(r#"ui.paginate="never""#.to_owned());
layer.set_value("ui.paginate", "never").unwrap();
}
if !layer.is_empty() {
config.add_layer(layer);
}
if !args.config_toml.is_empty() {
config.extend_layers(parse_config_args(&args.config_toml)?);

if config.layers().len() != old_layers_len {
ui.reset(config)?;
}
Ok(())
Expand Down
5 changes: 5 additions & 0 deletions lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ impl ConfigLayer {
.try_collect()
}

/// Returns true if the table has no configuration variables.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}

// Add .get_value(name) if needed. look_up_*() are low-level API.

/// Looks up sub non-inline table by the `name` path. Returns `Some(table)`
Expand Down

0 comments on commit c29b5a2

Please sign in to comment.