Skip to content

Commit

Permalink
config: do not parse TOML value expression by write_config_value_to_f…
Browse files Browse the repository at this point in the history
…ile()

It's a bit weird that a write() function parses user input, and some callers
doesn't want such flakiness.
  • Loading branch information
yuja committed Aug 9, 2024
1 parent 2053340 commit 9a4cb9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
8 changes: 6 additions & 2 deletions cli/src/commands/config/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use tracing::instrument;
use super::ConfigLevelArgs;
use crate::cli_util::{get_new_config_file_path, CommandHelper};
use crate::command_error::{user_error, CommandError};
use crate::config::{write_config_value_to_file, ConfigNamePathBuf};
use crate::config::{
parse_toml_value_or_bare_string, write_config_value_to_file, ConfigNamePathBuf,
};
use crate::ui::Ui;

/// Update config file to set the given option to a given value.
Expand All @@ -44,5 +46,7 @@ pub fn cmd_config_set(
path = config_path.display()
)));
}
write_config_value_to_file(&args.name, &args.value, &config_path)
// TODO(#531): Infer types based on schema (w/ --type arg to override).
let value = parse_toml_value_or_bare_string(&args.value);
write_config_value_to_file(&args.name, value, &config_path)
}
2 changes: 1 addition & 1 deletion cli/src/commands/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn cmd_git_clone(
let config_path = workspace_command.repo().repo_path().join("config.toml");
write_config_value_to_file(
&ConfigNamePathBuf::from_iter(["revset-aliases", "trunk()"]),
&format!("{default_branch}@{remote_name}"),
format!("{default_branch}@{remote_name}").into(),
&config_path,
)?;
writeln!(
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/git/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub fn maybe_set_repository_level_trunk_alias(
let config_path = repo.repo_path().join("config.toml");
write_config_value_to_file(
&ConfigNamePathBuf::from_iter(["revset-aliases", "trunk()"]),
&format!("{default_branch}@origin"),
format!("{default_branch}@origin").into(),
&config_path,
)?;
writeln!(
Expand Down
21 changes: 13 additions & 8 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ use tracing::instrument;

use crate::command_error::{user_error, user_error_with_message, CommandError};

/// Parses a TOML value expression. Interprets the given value as string if it
/// can't be parsed.
pub fn parse_toml_value_or_bare_string(value_str: &str) -> toml_edit::Value {
match value_str.parse() {
Ok(value) => value,
// TODO: might be better to reject meta characters. A typo in TOML value
// expression shouldn't be silently converted to string.
_ => value_str.into(),
}
}

pub fn to_toml_value(value: &config::Value) -> Result<toml_edit::Value, config::ConfigError> {
fn type_error<T: fmt::Display>(message: T) -> config::ConfigError {
config::ConfigError::Message(message.to_string())
Expand Down Expand Up @@ -566,7 +577,7 @@ fn read_config_path(config_path: &Path) -> Result<config::Config, config::Config

pub fn write_config_value_to_file(
key: &ConfigNamePathBuf,
value_str: &str,
value: toml_edit::Value,
path: &Path,
) -> Result<(), CommandError> {
// Read config
Expand All @@ -588,12 +599,6 @@ pub fn write_config_value_to_file(
})?;

// Apply config value
// Interpret value as string if it can't be parsed as a TOML value.
// TODO(#531): Infer types based on schema (w/ --type arg to override).
let item = match value_str.parse() {
Ok(value) => toml_edit::Item::Value(value),
_ => toml_edit::value(value_str),
};
let mut target_table = doc.as_table_mut();
let mut key_parts_iter = key.components();
let last_key_part = key_parts_iter.next_back().expect("key must not be empty");
Expand All @@ -618,7 +623,7 @@ pub fn write_config_value_to_file(
)));
}
}
target_table[last_key_part] = item;
target_table[last_key_part] = toml_edit::Item::Value(value);

// Write config back
std::fs::write(path, doc.to_string()).map_err(|err| {
Expand Down

0 comments on commit 9a4cb9e

Please sign in to comment.