aboutsummaryrefslogtreecommitdiffstats
path: root/src/config/file.rs
blob: bd6a3d5df530fe5dc5a5d0bd6ff8bca139209d81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! Configuration files: TOML (`.toml`) for people, JSON (`.json`) for generated
//! configuration, with the same keys: the long option names. Nested tables join their
//! keys with `-`, so `[mycelium] key-file = …` is `mycelium-key-file`.
//!
//! `include = [paths]` loads other files first (relative to this one), in the order
//! listed; an entry `{ path = "…", optional = true }` is skipped where missing. This
//! file's own values win over theirs, and later includes over earlier ones. A cycle is
//! an error.
//!
//! `unset = [options]` sets options back to their defaults, overriding includes and
//! earlier files; in JSON, `null` does the same for one option.

use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Map as JsonMap, Value as JsonValue};
use tracing::debug;

use super::import;
use super::{Entry, Layer, Overlay, Provenance, UNSET, overlay, spec, unset_entry};

const INCLUDE: &str = "include";
/// Gone: optional includes are entries of `include`, so the order is the list's.
const INCLUDE_OPTIONAL: &str = "include-optional";

/// The settings `path` makes, its includes resolved.
pub(super) fn load(path: &Path) -> Result<Layer> {
    load_inner(path, &mut Vec::new())
}

fn load_inner(path: &Path, loading: &mut Vec<PathBuf>) -> Result<Layer> {
    let canonical = path
        .canonicalize()
        .with_context(|| format!("configuration file {}", path.display()))?;
    if loading.contains(&canonical) {
        bail!("{} includes itself (through {:?})", path.display(), loading);
    }
    let dir = canonical
        .parent()
        .map(Path::to_owned)
        .unwrap_or_else(|| PathBuf::from("/"));

    let mut root = parse(&canonical)?;
    if root.contains_key(INCLUDE_OPTIONAL) {
        bail!(
            "{}: include-optional is gone; list the file in include as {{ path = \"…\", optional = true }}, where it goes in the order",
            canonical.display()
        );
    }
    let includes = take_includes(&mut root, &canonical)?;
    let unsets = take_paths(&mut root, UNSET, &canonical)?;

    loading.push(canonical.clone());
    let mut layer = Layer::new();
    for (include, required) in includes {
        let include = dir.join(include);
        if !required && !include.exists() {
            debug!(file = %include.display(), "Optional configuration file is missing; skipped");
            continue;
        }
        let included = load_inner(&include, loading)
            .with_context(|| format!("included from {}", canonical.display()))?;
        for (name, entry) in included {
            overlay(&mut layer, name, entry, Overlay::IncludeOverInclude);
        }
    }
    loading.pop();

    for name in unsets {
        if spec(&name).is_none() {
            bail!(
                "unknown option {name:?} in unset, in {}",
                canonical.display()
            );
        }
        let entry = unset_entry(Provenance::File(canonical.clone()));
        overlay(&mut layer, name, entry, Overlay::FileOverFile);
    }

    let mut own = Vec::new();
    flatten(&root, "", &dir, &mut own).with_context(|| format!("in {}", canonical.display()))?;
    for (name, value) in own {
        let spec = spec(&name)
            .ok_or_else(|| anyhow!("unknown option {name:?} in {}", canonical.display()))?;
        let (value, sources) = super::resolve_file_value(spec, value, &dir)
            .with_context(|| format!("option {name} in {}", canonical.display()))?;
        let entry = Entry {
            origin: sources.origin(
                Provenance::File(canonical.clone()),
                value.is_null(),
                spec.kind,
            ),
            value,
        };
        overlay(&mut layer, name, entry, Overlay::FileOverFile);
    }
    Ok(layer)
}

fn parse(path: &Path) -> Result<JsonMap<String, JsonValue>> {
    let text =
        std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
    let value: JsonValue = match path.extension().and_then(|e| e.to_str()) {
        Some("toml") => {
            let value: toml::Value =
                toml::from_str(&text).with_context(|| format!("parsing {}", path.display()))?;
            serde_json::to_value(value).with_context(|| format!("converting {}", path.display()))?
        }
        Some("json") => {
            serde_json::from_str(&text).with_context(|| format!("parsing {}", path.display()))?
        }
        _ => bail!(
            "{}: unknown configuration format (use .toml or .json)",
            path.display()
        ),
    };
    match value {
        JsonValue::Object(map) => Ok(map),
        _ => bail!("{}: the top level must be a table", path.display()),
    }
}

/// The files `include` names, in its order, and whether each is required: a path, or
/// `{ path = "…", optional = true }`.
fn take_includes(
    root: &mut JsonMap<String, JsonValue>,
    path: &Path,
) -> Result<Vec<(String, bool)>> {
    let items = match root.remove(INCLUDE) {
        None => return Ok(Vec::new()),
        Some(JsonValue::Array(items)) => items,
        Some(single) => vec![single],
    };
    items
        .into_iter()
        .map(|item| match item {
            JsonValue::String(file) => Ok((file, true)),
            JsonValue::Object(mut entry) => {
                let file = match entry.remove("path") {
                    Some(JsonValue::String(file)) => file,
                    _ => bail!("{}: an include entry needs a path", path.display()),
                };
                let optional = match entry.remove("optional") {
                    None => false,
                    Some(JsonValue::Bool(optional)) => optional,
                    Some(other) => bail!(
                        "{}: include's optional is true or false, not {other}",
                        path.display()
                    ),
                };
                if let Some(key) = entry.keys().next() {
                    bail!(
                        "{}: unknown key {key:?} in an include entry",
                        path.display()
                    );
                }
                Ok((file, !optional))
            }
            other => Err(anyhow!(
                "{}: include entries are paths or {{ path, optional }}, not {other}",
                path.display()
            )),
        })
        .collect()
}

fn take_paths(
    root: &mut JsonMap<String, JsonValue>,
    key: &str,
    path: &Path,
) -> Result<Vec<String>> {
    match root.remove(key) {
        None => Ok(Vec::new()),
        Some(JsonValue::String(single)) => Ok(vec![single]),
        Some(JsonValue::Array(items)) => items
            .into_iter()
            .map(|item| match item {
                JsonValue::String(item) => Ok(item),
                other => Err(anyhow!(
                    "{}: {key} entries must be paths, not {other}",
                    path.display()
                )),
            })
            .collect(),
        Some(other) => bail!(
            "{}: {key} must be a list of paths, not {other}",
            path.display()
        ),
    }
}

/// Option names and values, with nested tables joined by `-`. Importer tables are
/// values, not nesting.
fn flatten(
    table: &JsonMap<String, JsonValue>,
    prefix: &str,
    dir: &Path,
    out: &mut Vec<(String, JsonValue)>,
) -> Result<()> {
    for (key, value) in table {
        let name = if prefix.is_empty() {
            key.clone()
        } else {
            format!("{prefix}-{key}")
        };
        match value {
            JsonValue::Object(nested) if import::parse_table(nested, dir)?.is_none() => {
                flatten(nested, &name, dir, out)?
            }
            _ => out.push((name, value.clone())),
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::files::scratch_dir;
    use serde_json::json;

    fn write(dir: &Path, name: &str, text: &str) -> PathBuf {
        let path = dir.join(name);
        std::fs::write(&path, text).unwrap();
        path
    }

    #[test]
    fn includes_tables_and_importers() {
        let dir = scratch_dir("config-includes");
        write(&dir, "key-path", "/run/secrets/swarm-key\n");
        write(
            &dir,
            "public.toml",
            "neighbours = 8\nalias = \"public\"\n[mycelium]\npeers = [\"tls://a:9651\"]\n",
        );
        write(
            &dir,
            "secrets.json",
            r#"{ "swarm-key-file": { "file": "key-path" } }"#,
        );
        let main = write(
            &dir,
            "main.toml",
            "include = [\"public.toml\", \"secrets.json\", { path = \"missing.toml\", optional = true }]\nalias = \"main\"\n",
        );
        let layer = load(&main).unwrap();
        assert_eq!(layer["neighbours"].value, json!(8));
        assert_eq!(layer["alias"].value, json!("main"));
        assert_eq!(layer["mycelium-peers"].value, json!(["tls://a:9651"]));
        assert_eq!(
            layer["swarm-key-file"].value,
            json!("/run/secrets/swarm-key")
        );
        assert!(layer["swarm-key-file"].origin.is_imported());
        assert!(layer["alias"].origin.to_string().ends_with("main.toml"));
    }

    #[test]
    fn unset() {
        let dir = scratch_dir("config-unset");
        write(
            &dir,
            "base.toml",
            "alias = \"base\"\nneighbours = 8\nkeepalive = \"30s\"\n",
        );
        let toml = write(
            &dir,
            "local.toml",
            "include = \"base.toml\"\nunset = [\"alias\"]\n",
        );
        let layer = load(&toml).unwrap();
        assert!(layer["alias"].value.is_null() && layer["alias"].origin.unset);
        assert_eq!(layer["neighbours"].value, json!(8));
        let json = write(
            &dir,
            "local.json",
            r#"{ "include": "base.toml", "neighbours": null }"#,
        );
        let layer = load(&json).unwrap();
        assert!(layer["neighbours"].origin.unset);
        assert_eq!(layer["keepalive"].value, json!("30s"));
        let unknown = write(&dir, "bad.toml", "unset = [\"nope\"]\n");
        assert!(load(&unknown).is_err());
    }

    #[test]
    fn errors() {
        let dir = scratch_dir("config-errors");
        let unknown = write(&dir, "unknown.toml", "nieghbours = 3\n");
        assert!(load(&unknown).is_err());
        let cycle = write(&dir, "cycle.toml", "include = [\"cycle.toml\"]\n");
        assert!(load(&cycle).is_err());
        let missing = write(&dir, "missing.toml", "include = [\"nope.toml\"]\n");
        assert!(load(&missing).is_err());
        let format = write(&dir, "config.yaml", "alias: x\n");
        assert!(load(&format).is_err());
        let gone = write(&dir, "gone.toml", "include-optional = [\"x.toml\"]\n");
        assert!(load(&gone).is_err());
    }

    /// Includes are loaded in the order listed, optional ones too: the later wins.
    #[test]
    fn includes_in_order() {
        let dir = scratch_dir("config-include-order");
        write(&dir, "local.toml", "alias = \"local\"\n");
        write(&dir, "secrets.toml", "alias = \"secrets\"\n");
        let local_last = write(
            &dir,
            "a.toml",
            "include = [\"secrets.toml\", { path = \"local.toml\", optional = true }]\n",
        );
        assert_eq!(load(&local_last).unwrap()["alias"].value, json!("local"));
        let secrets_last = write(
            &dir,
            "b.toml",
            "include = [{ path = \"local.toml\", optional = true }, \"secrets.toml\"]\n",
        );
        assert_eq!(
            load(&secrets_last).unwrap()["alias"].value,
            json!("secrets")
        );
    }
}