Skip to content

Commit 23d4a68

Browse files
committed
Print environment note for json format, too.
1 parent 69b8570 commit 23d4a68

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/cargo/ops/cargo_config.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Implementation of `cargo config` subcommand.
22
3-
use crate::drop_println;
43
use crate::util::config::{Config, ConfigKey, ConfigValue as CV, Definition};
54
use crate::util::errors::CargoResult;
5+
use crate::{drop_eprintln, drop_println};
66
use anyhow::{bail, format_err, Error};
77
use serde_json::json;
88
use std::borrow::Cow;
@@ -68,15 +68,16 @@ pub fn get(config: &Config, opts: &GetOptions<'_>) -> CargoResult<()> {
6868
.get_cv_with_env(&key)?
6969
.ok_or_else(|| format_err!("config value `{}` is not set", key))?;
7070
match opts.format {
71-
ConfigFormat::Toml => {
72-
print_toml(config, opts, &key, &cv);
73-
if let Some(env) = maybe_env(config, &key, &cv) {
74-
print_toml_env(config, &env);
75-
}
76-
}
71+
ConfigFormat::Toml => print_toml(config, opts, &key, &cv),
7772
ConfigFormat::Json => print_json(config, &key, &cv, true),
7873
ConfigFormat::JsonValue => print_json(config, &key, &cv, false),
7974
}
75+
if let Some(env) = maybe_env(config, &key, &cv) {
76+
match opts.format {
77+
ConfigFormat::Toml => print_toml_env(config, &env),
78+
ConfigFormat::Json | ConfigFormat::JsonValue => print_json_env(config, &env),
79+
}
80+
}
8081
} else {
8182
match &opts.format {
8283
ConfigFormat::Toml => print_toml_unmerged(config, opts, &key)?,
@@ -168,6 +169,17 @@ fn print_toml_env(config: &Config, env: &[(&String, &String)]) {
168169
}
169170
}
170171

172+
fn print_json_env(config: &Config, env: &[(&String, &String)]) {
173+
drop_eprintln!(
174+
config,
175+
"note: The following environment variables may affect the loaded values."
176+
);
177+
for (env_key, env_value) in env {
178+
let val = shell_escape::escape(Cow::Borrowed(env_value));
179+
drop_eprintln!(config, "{}={}", env_key, val);
180+
}
181+
}
182+
171183
fn print_json(config: &Config, key: &ConfigKey, cv: &CV, include_key: bool) {
172184
let json_value = if key.is_root() || !include_key {
173185
cv_to_json(cv)

tests/testsuite/cargo_config.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,27 @@ fn get_json() {
200200
.env("CARGO_ALIAS_BAR", "cat dog")
201201
.env("CARGO_BUILD_JOBS", "100")
202202
.with_json(all_json)
203-
.with_stderr("")
203+
.with_stderr(
204+
"\
205+
note: The following environment variables may affect the loaded values.
206+
CARGO_ALIAS_BAR=[..]cat dog[..]
207+
CARGO_BUILD_JOBS=100
208+
CARGO_HOME=[ROOT]/home/.cargo
209+
",
210+
)
204211
.run();
205212

206213
// json-value is the same for the entire root table
207214
cargo_process("config get --format=json-value -Zunstable-options")
208215
.cwd(&sub_folder.parent().unwrap())
209216
.masquerade_as_nightly_cargo()
210217
.with_json(all_json)
211-
.with_stderr("")
218+
.with_stderr(
219+
"\
220+
note: The following environment variables may affect the loaded values.
221+
CARGO_HOME=[ROOT]/home/.cargo
222+
",
223+
)
212224
.run();
213225

214226
cargo_process("config get --format=json build.jobs -Zunstable-options")

0 commit comments

Comments
 (0)