|
1 | 1 | //! Implementation of `cargo config` subcommand. |
2 | 2 |
|
3 | | -use crate::drop_println; |
4 | 3 | use crate::util::config::{Config, ConfigKey, ConfigValue as CV, Definition}; |
5 | 4 | use crate::util::errors::CargoResult; |
| 5 | +use crate::{drop_eprintln, drop_println}; |
6 | 6 | use anyhow::{bail, format_err, Error}; |
7 | 7 | use serde_json::json; |
8 | 8 | use std::borrow::Cow; |
@@ -68,15 +68,16 @@ pub fn get(config: &Config, opts: &GetOptions<'_>) -> CargoResult<()> { |
68 | 68 | .get_cv_with_env(&key)? |
69 | 69 | .ok_or_else(|| format_err!("config value `{}` is not set", key))?; |
70 | 70 | 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), |
77 | 72 | ConfigFormat::Json => print_json(config, &key, &cv, true), |
78 | 73 | ConfigFormat::JsonValue => print_json(config, &key, &cv, false), |
79 | 74 | } |
| 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 | + } |
80 | 81 | } else { |
81 | 82 | match &opts.format { |
82 | 83 | ConfigFormat::Toml => print_toml_unmerged(config, opts, &key)?, |
@@ -168,6 +169,17 @@ fn print_toml_env(config: &Config, env: &[(&String, &String)]) { |
168 | 169 | } |
169 | 170 | } |
170 | 171 |
|
| 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 | + |
171 | 183 | fn print_json(config: &Config, key: &ConfigKey, cv: &CV, include_key: bool) { |
172 | 184 | let json_value = if key.is_root() || !include_key { |
173 | 185 | cv_to_json(cv) |
|
0 commit comments