cargo/ops/
cargo_config.rs

1//! Implementation of `cargo config` subcommand.
2
3use crate::util::context::{ConfigKey, ConfigValue as CV, Definition, GlobalContext};
4use crate::util::errors::CargoResult;
5use crate::{drop_eprintln, drop_println};
6use anyhow::{Error, bail, format_err};
7use serde_json::json;
8use std::borrow::Cow;
9use std::fmt;
10use std::str::FromStr;
11
12pub enum ConfigFormat {
13    Toml,
14    Json,
15    JsonValue,
16}
17
18impl ConfigFormat {
19    /// For clap.
20    pub const POSSIBLE_VALUES: [&'static str; 3] = ["toml", "json", "json-value"];
21}
22
23impl FromStr for ConfigFormat {
24    type Err = Error;
25    fn from_str(s: &str) -> CargoResult<Self> {
26        match s {
27            "toml" => Ok(ConfigFormat::Toml),
28            "json" => Ok(ConfigFormat::Json),
29            "json-value" => Ok(ConfigFormat::JsonValue),
30            f => bail!("unknown config format `{}`", f),
31        }
32    }
33}
34
35impl fmt::Display for ConfigFormat {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match *self {
38            ConfigFormat::Toml => write!(f, "toml"),
39            ConfigFormat::Json => write!(f, "json"),
40            ConfigFormat::JsonValue => write!(f, "json-value"),
41        }
42    }
43}
44
45/// Options for `cargo config get`.
46pub struct GetOptions<'a> {
47    pub key: Option<&'a str>,
48    pub format: ConfigFormat,
49    pub show_origin: bool,
50    pub merged: bool,
51}
52
53pub fn get(gctx: &GlobalContext, opts: &GetOptions<'_>) -> CargoResult<()> {
54    if opts.show_origin && !matches!(opts.format, ConfigFormat::Toml) {
55        bail!(
56            "the `{}` format does not support --show-origin, try the `toml` format instead",
57            opts.format
58        );
59    }
60    let key = match opts.key {
61        Some(key) => ConfigKey::from_str(key),
62        None => ConfigKey::new(),
63    };
64    if opts.merged {
65        let cv = gctx
66            .get_cv_with_env(&key)?
67            .ok_or_else(|| format_err!("config value `{}` is not set", key))?;
68        match opts.format {
69            ConfigFormat::Toml => print_toml(gctx, opts, &key, &cv),
70            ConfigFormat::Json => print_json(gctx, &key, &cv, true),
71            ConfigFormat::JsonValue => print_json(gctx, &key, &cv, false),
72        }
73        if let Some(env) = maybe_env(gctx, &key, &cv) {
74            match opts.format {
75                ConfigFormat::Toml => print_toml_env(gctx, &env),
76                ConfigFormat::Json | ConfigFormat::JsonValue => print_json_env(gctx, &env),
77            }
78        }
79    } else {
80        match &opts.format {
81            ConfigFormat::Toml => print_toml_unmerged(gctx, opts, &key)?,
82            format => bail!(
83                "the `{}` format does not support --merged=no, try the `toml` format instead",
84                format
85            ),
86        }
87    }
88    Ok(())
89}
90
91/// Checks for environment variables that might be used.
92fn maybe_env<'gctx>(
93    gctx: &'gctx GlobalContext,
94    key: &ConfigKey,
95    cv: &CV,
96) -> Option<Vec<(&'gctx str, &'gctx str)>> {
97    // Only fetching a table is unable to load env values. Leaf entries should
98    // work properly.
99    match cv {
100        CV::Table(_map, _def) => {}
101        _ => return None,
102    }
103    let mut env: Vec<_> = gctx
104        .env()
105        .filter(|(env_key, _val)| env_key.starts_with(&format!("{}_", key.as_env_key())))
106        .collect();
107    env.sort_by_key(|x| x.0);
108    if env.is_empty() { None } else { Some(env) }
109}
110
111fn print_toml(gctx: &GlobalContext, opts: &GetOptions<'_>, key: &ConfigKey, cv: &CV) {
112    let origin = |def: &Definition| -> String {
113        if !opts.show_origin {
114            return "".to_string();
115        }
116        format!(" # {}", def)
117    };
118    match cv {
119        CV::Boolean(val, def) => drop_println!(gctx, "{} = {}{}", key, val, origin(def)),
120        CV::Integer(val, def) => drop_println!(gctx, "{} = {}{}", key, val, origin(def)),
121        CV::String(val, def) => drop_println!(
122            gctx,
123            "{} = {}{}",
124            key,
125            toml_edit::Value::from(val),
126            origin(def)
127        ),
128        CV::List(vals, _def) => {
129            if opts.show_origin {
130                drop_println!(gctx, "{} = [", key);
131                for cv in vals {
132                    let (val, def) = match cv {
133                        CV::String(s, def) => (s.as_str(), def),
134                        // This is actually unreachable until we start supporting list of different types.
135                        // It should be validated already during the deserialization.
136                        v => todo!("support {} type ", v.desc()),
137                    };
138                    drop_println!(
139                        gctx,
140                        "    {}, # {}",
141                        serde::Serialize::serialize(val, toml_edit::ser::ValueSerializer::new())
142                            .unwrap(),
143                        def
144                    );
145                }
146                drop_println!(gctx, "]");
147            } else {
148                let vals: toml_edit::Array = vals
149                    .iter()
150                    .map(|cv| match cv {
151                        CV::String(s, _) => toml_edit::Value::from(s.as_str()),
152                        // This is actually unreachable until we start supporting list of different types.
153                        // It should be validated already during the deserialization.
154                        v => todo!("support {} type ", v.desc()),
155                    })
156                    .collect();
157                drop_println!(gctx, "{} = {}", key, vals);
158            }
159        }
160        CV::Table(table, _def) => {
161            let mut key_vals: Vec<_> = table.iter().collect();
162            key_vals.sort_by(|a, b| a.0.cmp(b.0));
163            for (table_key, val) in key_vals {
164                let mut subkey = key.clone();
165                // push or push_sensitive shouldn't matter here, since this is
166                // not dealing with environment variables.
167                subkey.push(table_key);
168                print_toml(gctx, opts, &subkey, val);
169            }
170        }
171    }
172}
173
174fn print_toml_env(gctx: &GlobalContext, env: &[(&str, &str)]) {
175    drop_println!(
176        gctx,
177        "# The following environment variables may affect the loaded values."
178    );
179    for (env_key, env_value) in env {
180        let val = shell_escape::escape(Cow::Borrowed(env_value));
181        drop_println!(gctx, "# {}={}", env_key, val);
182    }
183}
184
185fn print_json_env(gctx: &GlobalContext, env: &[(&str, &str)]) {
186    drop_eprintln!(
187        gctx,
188        "note: The following environment variables may affect the loaded values."
189    );
190    for (env_key, env_value) in env {
191        let val = shell_escape::escape(Cow::Borrowed(env_value));
192        drop_eprintln!(gctx, "{}={}", env_key, val);
193    }
194}
195
196fn print_json(gctx: &GlobalContext, key: &ConfigKey, cv: &CV, include_key: bool) {
197    let json_value = if key.is_root() || !include_key {
198        cv_to_json(cv)
199    } else {
200        let mut parts: Vec<_> = key.parts().collect();
201        let last_part = parts.pop().unwrap();
202        let mut root_table = json!({});
203        // Create a JSON object with nested keys up to the value being displayed.
204        let mut table = &mut root_table;
205        for part in parts {
206            table[part] = json!({});
207            table = table.get_mut(part).unwrap();
208        }
209        table[last_part] = cv_to_json(cv);
210        root_table
211    };
212    drop_println!(gctx, "{}", serde_json::to_string(&json_value).unwrap());
213
214    // Helper for recursively converting a CV to JSON.
215    fn cv_to_json(cv: &CV) -> serde_json::Value {
216        match cv {
217            CV::Boolean(val, _def) => json!(val),
218            CV::Integer(val, _def) => json!(val),
219            CV::String(val, _def) => json!(val),
220            CV::List(vals, _def) => {
221                let jvals: Vec<_> = vals.iter().map(cv_to_json).collect();
222                json!(jvals)
223            }
224            CV::Table(map, _def) => {
225                let mut table = json!({});
226                for (key, val) in map {
227                    table[key] = cv_to_json(val);
228                }
229                table
230            }
231        }
232    }
233}
234
235fn print_toml_unmerged(
236    gctx: &GlobalContext,
237    opts: &GetOptions<'_>,
238    key: &ConfigKey,
239) -> CargoResult<()> {
240    let print_table = |cv: &CV| {
241        drop_println!(gctx, "# {}", cv.definition());
242        print_toml(gctx, opts, &ConfigKey::new(), cv);
243        drop_println!(gctx, "");
244    };
245    // This removes entries from the given CV so that all that remains is the
246    // given key. Returns false if no entries were found.
247    fn trim_cv(mut cv: &mut CV, key: &ConfigKey) -> CargoResult<bool> {
248        for (i, part) in key.parts().enumerate() {
249            match cv {
250                CV::Table(map, _def) => {
251                    map.retain(|key, _value| key == part);
252                    match map.get_mut(part) {
253                        Some(val) => cv = val,
254                        None => return Ok(false),
255                    }
256                }
257                _ => {
258                    let mut key_so_far = ConfigKey::new();
259                    for part in key.parts().take(i) {
260                        key_so_far.push(part);
261                    }
262                    bail!(
263                        "expected table for configuration key `{}`, \
264                         but found {} in {}",
265                        key_so_far,
266                        cv.desc(),
267                        cv.definition()
268                    )
269                }
270            }
271        }
272        Ok(match cv {
273            CV::Table(map, _def) => !map.is_empty(),
274            _ => true,
275        })
276    }
277
278    let mut cli_args = gctx.cli_args_as_table()?;
279    if trim_cv(&mut cli_args, key)? {
280        print_table(&cli_args);
281    }
282
283    // This slurps up some extra env vars that aren't technically part of the
284    // "config" (or are special-cased). I'm personally fine with just keeping
285    // them here, though it might be confusing. The vars I'm aware of:
286    //
287    // * CARGO
288    // * CARGO_HOME
289    // * CARGO_NAME
290    // * CARGO_EMAIL
291    // * CARGO_INCREMENTAL
292    // * CARGO_TARGET_DIR
293    // * CARGO_CACHE_RUSTC_INFO
294    //
295    // All of these except CARGO, CARGO_HOME, and CARGO_CACHE_RUSTC_INFO are
296    // actually part of the config, but they are special-cased in the code.
297    //
298    // TODO: It might be a good idea to teach the Config loader to support
299    // environment variable aliases so that these special cases are less
300    // special, and will just naturally get loaded as part of the config.
301    let mut env: Vec<_> = gctx
302        .env()
303        .filter(|(env_key, _val)| env_key.starts_with(key.as_env_key()))
304        .collect();
305    if !env.is_empty() {
306        env.sort_by_key(|x| x.0);
307        drop_println!(gctx, "# Environment variables");
308        for (key, value) in env {
309            // Displaying this in "shell" syntax instead of TOML, since that
310            // somehow makes more sense to me.
311            let val = shell_escape::escape(Cow::Borrowed(value));
312            drop_println!(gctx, "# {}={}", key, val);
313        }
314        drop_println!(gctx, "");
315    }
316
317    let unmerged = gctx.load_values_unmerged()?;
318    for mut cv in unmerged {
319        if trim_cv(&mut cv, key)? {
320            print_table(&cv);
321        }
322    }
323    Ok(())
324}