Skip to content

Commit

Permalink
Print environment note for json format, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Mar 29, 2021
1 parent 69b8570 commit 23d4a68
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/cargo/ops/cargo_config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Implementation of `cargo config` subcommand.
use crate::drop_println;
use crate::util::config::{Config, ConfigKey, ConfigValue as CV, Definition};
use crate::util::errors::CargoResult;
use crate::{drop_eprintln, drop_println};
use anyhow::{bail, format_err, Error};
use serde_json::json;
use std::borrow::Cow;
Expand Down Expand Up @@ -68,15 +68,16 @@ pub fn get(config: &Config, opts: &GetOptions<'_>) -> CargoResult<()> {
.get_cv_with_env(&key)?
.ok_or_else(|| format_err!("config value `{}` is not set", key))?;
match opts.format {
ConfigFormat::Toml => {
print_toml(config, opts, &key, &cv);
if let Some(env) = maybe_env(config, &key, &cv) {
print_toml_env(config, &env);
}
}
ConfigFormat::Toml => print_toml(config, opts, &key, &cv),
ConfigFormat::Json => print_json(config, &key, &cv, true),
ConfigFormat::JsonValue => print_json(config, &key, &cv, false),
}
if let Some(env) = maybe_env(config, &key, &cv) {
match opts.format {
ConfigFormat::Toml => print_toml_env(config, &env),
ConfigFormat::Json | ConfigFormat::JsonValue => print_json_env(config, &env),
}
}
} else {
match &opts.format {
ConfigFormat::Toml => print_toml_unmerged(config, opts, &key)?,
Expand Down Expand Up @@ -168,6 +169,17 @@ fn print_toml_env(config: &Config, env: &[(&String, &String)]) {
}
}

fn print_json_env(config: &Config, env: &[(&String, &String)]) {
drop_eprintln!(
config,
"note: The following environment variables may affect the loaded values."
);
for (env_key, env_value) in env {
let val = shell_escape::escape(Cow::Borrowed(env_value));
drop_eprintln!(config, "{}={}", env_key, val);
}
}

fn print_json(config: &Config, key: &ConfigKey, cv: &CV, include_key: bool) {
let json_value = if key.is_root() || !include_key {
cv_to_json(cv)
Expand Down
16 changes: 14 additions & 2 deletions tests/testsuite/cargo_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,27 @@ fn get_json() {
.env("CARGO_ALIAS_BAR", "cat dog")
.env("CARGO_BUILD_JOBS", "100")
.with_json(all_json)
.with_stderr("")
.with_stderr(
"\
note: The following environment variables may affect the loaded values.
CARGO_ALIAS_BAR=[..]cat dog[..]
CARGO_BUILD_JOBS=100
CARGO_HOME=[ROOT]/home/.cargo
",
)
.run();

// json-value is the same for the entire root table
cargo_process("config get --format=json-value -Zunstable-options")
.cwd(&sub_folder.parent().unwrap())
.masquerade_as_nightly_cargo()
.with_json(all_json)
.with_stderr("")
.with_stderr(
"\
note: The following environment variables may affect the loaded values.
CARGO_HOME=[ROOT]/home/.cargo
",
)
.run();

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

0 comments on commit 23d4a68

Please sign in to comment.