1use 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 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
45pub 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
91fn maybe_env<'gctx>(
93 gctx: &'gctx GlobalContext,
94 key: &ConfigKey,
95 cv: &CV,
96) -> Option<Vec<(&'gctx str, &'gctx str)>> {
97 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 (val, def) in vals {
132 drop_println!(
133 gctx,
134 " {}, # {}",
135 serde::Serialize::serialize(val, toml_edit::ser::ValueSerializer::new())
136 .unwrap(),
137 def
138 );
139 }
140 drop_println!(gctx, "]");
141 } else {
142 let vals: toml_edit::Array = vals.iter().map(|x| &x.0).collect();
143 drop_println!(gctx, "{} = {}", key, vals);
144 }
145 }
146 CV::Table(table, _def) => {
147 let mut key_vals: Vec<_> = table.iter().collect();
148 key_vals.sort_by(|a, b| a.0.cmp(b.0));
149 for (table_key, val) in key_vals {
150 let mut subkey = key.clone();
151 subkey.push(table_key);
154 print_toml(gctx, opts, &subkey, val);
155 }
156 }
157 }
158}
159
160fn print_toml_env(gctx: &GlobalContext, env: &[(&str, &str)]) {
161 drop_println!(
162 gctx,
163 "# The following environment variables may affect the loaded values."
164 );
165 for (env_key, env_value) in env {
166 let val = shell_escape::escape(Cow::Borrowed(env_value));
167 drop_println!(gctx, "# {}={}", env_key, val);
168 }
169}
170
171fn print_json_env(gctx: &GlobalContext, env: &[(&str, &str)]) {
172 drop_eprintln!(
173 gctx,
174 "note: The following environment variables may affect the loaded values."
175 );
176 for (env_key, env_value) in env {
177 let val = shell_escape::escape(Cow::Borrowed(env_value));
178 drop_eprintln!(gctx, "{}={}", env_key, val);
179 }
180}
181
182fn print_json(gctx: &GlobalContext, key: &ConfigKey, cv: &CV, include_key: bool) {
183 let json_value = if key.is_root() || !include_key {
184 cv_to_json(cv)
185 } else {
186 let mut parts: Vec<_> = key.parts().collect();
187 let last_part = parts.pop().unwrap();
188 let mut root_table = json!({});
189 let mut table = &mut root_table;
191 for part in parts {
192 table[part] = json!({});
193 table = table.get_mut(part).unwrap();
194 }
195 table[last_part] = cv_to_json(cv);
196 root_table
197 };
198 drop_println!(gctx, "{}", serde_json::to_string(&json_value).unwrap());
199
200 fn cv_to_json(cv: &CV) -> serde_json::Value {
202 match cv {
203 CV::Boolean(val, _def) => json!(val),
204 CV::Integer(val, _def) => json!(val),
205 CV::String(val, _def) => json!(val),
206 CV::List(vals, _def) => {
207 let jvals: Vec<_> = vals.iter().map(|(val, _def)| json!(val)).collect();
208 json!(jvals)
209 }
210 CV::Table(map, _def) => {
211 let mut table = json!({});
212 for (key, val) in map {
213 table[key] = cv_to_json(val);
214 }
215 table
216 }
217 }
218 }
219}
220
221fn print_toml_unmerged(
222 gctx: &GlobalContext,
223 opts: &GetOptions<'_>,
224 key: &ConfigKey,
225) -> CargoResult<()> {
226 let print_table = |cv: &CV| {
227 drop_println!(gctx, "# {}", cv.definition());
228 print_toml(gctx, opts, &ConfigKey::new(), cv);
229 drop_println!(gctx, "");
230 };
231 fn trim_cv(mut cv: &mut CV, key: &ConfigKey) -> CargoResult<bool> {
234 for (i, part) in key.parts().enumerate() {
235 match cv {
236 CV::Table(map, _def) => {
237 map.retain(|key, _value| key == part);
238 match map.get_mut(part) {
239 Some(val) => cv = val,
240 None => return Ok(false),
241 }
242 }
243 _ => {
244 let mut key_so_far = ConfigKey::new();
245 for part in key.parts().take(i) {
246 key_so_far.push(part);
247 }
248 bail!(
249 "expected table for configuration key `{}`, \
250 but found {} in {}",
251 key_so_far,
252 cv.desc(),
253 cv.definition()
254 )
255 }
256 }
257 }
258 Ok(match cv {
259 CV::Table(map, _def) => !map.is_empty(),
260 _ => true,
261 })
262 }
263
264 let mut cli_args = gctx.cli_args_as_table()?;
265 if trim_cv(&mut cli_args, key)? {
266 print_table(&cli_args);
267 }
268
269 let mut env: Vec<_> = gctx
288 .env()
289 .filter(|(env_key, _val)| env_key.starts_with(key.as_env_key()))
290 .collect();
291 if !env.is_empty() {
292 env.sort_by_key(|x| x.0);
293 drop_println!(gctx, "# Environment variables");
294 for (key, value) in env {
295 let val = shell_escape::escape(Cow::Borrowed(value));
298 drop_println!(gctx, "# {}={}", key, val);
299 }
300 drop_println!(gctx, "");
301 }
302
303 let unmerged = gctx.load_values_unmerged()?;
304 for mut cv in unmerged {
305 if trim_cv(&mut cv, key)? {
306 print_table(&cv);
307 }
308 }
309 Ok(())
310}