config: load system host/user name by CLI and insert as env-base layer

Since the default now falls back to "", we can simply override the default by
CLI. We don't have to touch the environment in jj-lib.
This commit is contained in:
Yuya Nishihara 2025-01-03 00:07:26 +09:00
parent 5842e58db6
commit fcac7ed39c
6 changed files with 29 additions and 24 deletions

1
Cargo.lock generated
View file

@ -2056,7 +2056,6 @@ dependencies = [
"tracing",
"version_check",
"watchman_client",
"whoami",
"winreg",
"zstd",
]

View file

@ -438,9 +438,23 @@ pub fn config_from_environment(default_layers: impl IntoIterator<Item = ConfigLa
RawConfig(config)
}
const OP_HOSTNAME: &str = "operation.hostname";
const OP_USERNAME: &str = "operation.username";
/// Environment variables that should be overridden by config values
fn env_base_layer() -> ConfigLayer {
let mut layer = ConfigLayer::empty(ConfigSource::EnvBase);
// TODO: warn if hostname/username is empty after loading config files?
if let Ok(value) = whoami::fallible::hostname()
.inspect_err(|err| tracing::warn!(?err, "failed to get hostname"))
{
layer.set_value(OP_HOSTNAME, value).unwrap();
}
if let Ok(value) = whoami::fallible::username()
.inspect_err(|err| tracing::warn!(?err, "failed to get username"))
{
layer.set_value(OP_USERNAME, value).unwrap();
}
if !env::var("NO_COLOR").unwrap_or_default().is_empty() {
// "User-level configuration files and per-instance command-line arguments
// should override $NO_COLOR." https://no-color.org/
@ -496,10 +510,10 @@ fn env_overrides_layer() -> ConfigLayer {
layer.set_value("debug.operation-timestamp", value).unwrap();
}
if let Ok(value) = env::var("JJ_OP_HOSTNAME") {
layer.set_value("operation.hostname", value).unwrap();
layer.set_value(OP_HOSTNAME, value).unwrap();
}
if let Ok(value) = env::var("JJ_OP_USERNAME") {
layer.set_value("operation.username", value).unwrap();
layer.set_value(OP_USERNAME, value).unwrap();
}
if let Ok(value) = env::var("JJ_EDITOR") {
layer.set_value("ui.editor", value).unwrap();

View file

@ -834,9 +834,15 @@ fn test_default_config() {
maskable_re.is_match(&hostname) && maskable_re.is_match(&username)
};
let (stdout, stderr) = jj_cmd_ok(test_env.env_root(), &["config", "list"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @"Warning: No config to list");
let (stdout, stderr) = jj_cmd_ok(
test_env.env_root(),
&["config", "list", r#"-Tname ++ "\n""#],
);
insta::assert_snapshot!(stdout, @r"
operation.hostname
operation.username
");
insta::assert_snapshot!(stderr, @"");
let repo_path = test_env.env_root().join("repo");
let (stdout, stderr) = jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);

View file

@ -74,7 +74,6 @@ tokio = { workspace = true, optional = true }
toml_edit = { workspace = true }
tracing = { workspace = true }
watchman_client = { workspace = true, optional = true }
whoami = { workspace = true }
zstd = { workspace = true }
[target.'cfg(unix)'.dependencies]

View file

@ -14,8 +14,8 @@ abandon-unreachable-commits = true
# auto-local-bookmark = false
[operation]
# hostname = <host>
# username = <user>
hostname = ""
username = ""
[signing]
backend = "none"

View file

@ -152,21 +152,8 @@ impl UserSettings {
let operation_timestamp = config
.get_value_with("debug.operation-timestamp", to_timestamp)
.optional()?;
// Instead of handling environment data here, it might be better to load
// them by CLI and insert as a config layer.
// TODO: warn empty hostname/username by CLI?
let operation_hostname = config
.get("operation.hostname")
.optional()?
.map_or_else(whoami::fallible::hostname, Ok)
.inspect_err(|err| tracing::warn!(?err, "operation.hostname couldn't be set"))
.unwrap_or_default();
let operation_username = config
.get("operation.username")
.optional()?
.map_or_else(whoami::fallible::username, Ok)
.inspect_err(|err| tracing::warn!(?err, "operation.username couldn't be set"))
.unwrap_or_default();
let operation_hostname = config.get("operation.hostname")?;
let operation_username = config.get("operation.username")?;
let data = UserSettingsData {
user_name,
user_email,