starmap-cli 0.3.0

The starmap command, which checks, explains, compares and runs an application's declared environment
Documentation
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! `starmap explain`, which describes each declared variable and the value
//! it has in this shell.

use std::ffi::OsString;
use std::fmt;

use crumbkit::schemars::{Schema, SchemaGenerator};
use crumbkit::serde::Serializer;
use crumbkit::{Batch, Document, Failure, Recovery, Row, Shown};
use schemars::JsonSchema;
use serde::Serialize;
use starmap::{
    Bindings, Bound, BuiltIns, Cache, Choices, Mode, Name, SecretRef, Source, Spec, Type, Unset,
    Value, Variable,
};

use crate::cli::{ExplainArgs, SpecFile};
use crate::codes::VARIABLE_UNKNOWN;
use crate::mode;
use crate::spec::{self, SpecFailure};

#[cfg(test)]
mod tests;

/// The document that `starmap explain` prints.
pub(crate) const EXPLAIN: Document<Batch<Explained, UnknownVariable>> = Document::new(
    "starmap",
    "explain",
    1,
    "Each variable that a spec declares, with its rules and the value it has in this shell.",
);

/// What a withheld value prints as, whatever its length, so the output
/// does not reveal how long the secret is.
const DOTS: &str = "••••••••";

/// One declared variable, with its rules and the value it has in this
/// shell.
#[derive(Debug, Serialize, JsonSchema)]
pub(crate) struct Explained {
    /// The variable's name.
    name: String,
    /// The type of the variable's value, such as port or integer 1..=100.
    #[serde(rename = "type")]
    kind: String,
    /// What happens when the variable has no value, in local mode and, when it differs, in production.
    unset: String,
    /// Where the variable's value comes from, with the kind of its bind.
    source: String,
    /// The variable's value in this shell, or its default; a withheld value prints as eight dots and has no text in JSON.
    value: Cell,
    /// Whether the value may be shown or is withheld.
    show: String,
    /// The variable's bind, such as a 1Password reference, when it has one.
    bind: Option<String>,
    /// `never` when the bind keeps the value out of any cache of resolved values; empty, and left out of `--json`, when a cache may keep it.
    #[serde(skip_serializing_if = "Option::is_none")]
    cache: Option<String>,
    /// The sentence that says what the variable is for.
    purpose: String,
    /// The sentence that says what the application does when the variable is unset; empty, and left out of `--json`, when the spec states none.
    #[serde(skip_serializing_if = "Option::is_none")]
    effect: Option<String>,
    /// The sentence that states the check the application's own code makes of the value, which starmap never runs; empty, and left out of `--json`, when the spec states none.
    #[serde(skip_serializing_if = "Option::is_none")]
    check: Option<String>,
    /// The name of the named enumeration that the variable's type is; left out of `--json` for any other type.
    #[serde(skip_serializing_if = "Option::is_none")]
    enumeration: Option<String>,
    /// Each value of a named enumeration with its description; empty, and left out of `--json`, for any other type.
    #[serde(skip_serializing_if = "Option::is_none")]
    values: Option<Values>,
    /// The name of the group of variables that are set together or not at all, which the variable belongs to; empty, and left out of `--json`, when it belongs to none.
    #[serde(skip_serializing_if = "Option::is_none")]
    group: Option<String>,
}

impl Row for Explained {
    const COLUMNS: &'static [&'static str] = &["name", "type", "unset", "source", "value"];
    const LONG_COLUMNS: &'static [&'static str] = &[
        "show", "bind", "cache", "purpose", "effect", "values", "check", "group",
    ];
    const UNCUT_COLUMNS: &'static [&'static str] = &["name"];
}

/// The value of a variable as explain shows it. Its text is the value, the
/// default with `(default)`, `unset`, or eight dots for a withheld value.
/// Its JSON is an object with the state and, for a shown value, the text.
#[derive(Debug)]
pub(crate) struct Cell(CellJson);

/// The JSON form of a [`Cell`].
#[derive(Debug, Serialize, JsonSchema)]
struct CellJson {
    /// Whether the value comes from this shell, is the default, is unset,
    /// or comes from this shell and does not fit the variable's type.
    state: State,
    /// The value's text. A withheld value never has one.
    #[serde(skip_serializing_if = "Option::is_none")]
    text: Option<String>,
}

/// Where a variable's value comes from in this shell.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
enum State {
    /// This shell sets the variable.
    Set,
    /// This shell leaves the variable unset, and its default applies.
    Default,
    /// The variable has no value.
    Unset,
    /// This shell sets the variable to a value that does not fit its type.
    Invalid,
}

impl Cell {
    /// Returns the cell of a value in `state`, whose text is `text`, or
    /// none when the variable is withheld.
    fn new(state: State, text: Option<String>) -> Self {
        Self(CellJson { state, text })
    }
}

impl fmt::Display for Cell {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let text = self.0.text.as_deref().unwrap_or(DOTS);
        match self.0.state {
            State::Set => formatter.write_str(text),
            State::Default => write!(formatter, "{text} (default)"),
            State::Unset => formatter.write_str("unset"),
            State::Invalid => write!(formatter, "{text} (invalid)"),
        }
    }
}

impl Shown for Cell {
    fn json<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.0.serialize(serializer)
    }

    fn schema(generator: &mut SchemaGenerator) -> Schema {
        CellJson::json_schema(generator)
    }
}

crumbkit::shown!(Cell);

/// The values of a named enumeration, each with its description. Its text
/// is `value: description` for each value, separated by `; `, because a
/// table cell holds one line. Its JSON is a list of objects.
#[derive(Debug)]
pub(crate) struct Values(Vec<Described>);

/// One value of a named enumeration with its description.
#[derive(Debug, Serialize, JsonSchema)]
struct Described {
    /// The value.
    value: String,
    /// The sentence that says what choosing the value means.
    description: String,
}

impl Values {
    /// Returns the values of a named enumeration type, or `None` for any
    /// other type.
    pub(crate) fn of(ty: &Type) -> Option<Self> {
        let Type::Enumeration(choices) = ty else {
            return None;
        };
        choices.name()?;
        Some(Self(described(choices)))
    }
}

/// Returns each choice of `choices` with its description.
fn described(choices: &Choices) -> Vec<Described> {
    choices
        .iter()
        .map(|choice| Described {
            value: choice.to_string(),
            description: choices.description(choice).unwrap_or_default().to_owned(),
        })
        .collect()
}

impl fmt::Display for Values {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (index, described) in self.0.iter().enumerate() {
            if index > 0 {
                formatter.write_str("; ")?;
            }
            write!(formatter, "{}: {}", described.value, described.description)?;
        }
        Ok(())
    }
}

impl Shown for Values {
    fn json<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.0.serialize(serializer)
    }

    fn schema(generator: &mut SchemaGenerator) -> Schema {
        Vec::<Described>::json_schema(generator)
    }
}

crumbkit::shown!(Values);

/// A name that `starmap explain` was given and the spec does not declare.
#[derive(Debug, Failure)]
#[failure(
    code = VARIABLE_UNKNOWN,
    recovery = list_again(file),
    field(name = name.as_str()),
    field(file = file.as_str()),
)]
pub(crate) struct UnknownVariable {
    /// The name.
    name: Name,
    /// The spec file, as the caller gave it.
    file: String,
}

impl fmt::Display for UnknownVariable {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{} does not declare {}", self.file, self.name)
    }
}

/// Returns the recovery of an unknown name, which explains every variable
/// of the same spec file.
fn list_again(file: &str) -> Recovery {
    Recovery::Run(SpecFile::call(&["explain"], file))
}

impl std::error::Error for UnknownVariable {}

/// Reads and parses the spec that `--spec` names, and explains each
/// variable that `names` lists, or every declared variable without names,
/// with its value in `vars`, the process environment. The note names the
/// mode and says that explain resolves no bind. Without names, the note
/// also lists the built-in variables that `starmap run --only-declared`
/// passes.
///
/// # Errors
///
/// Returns [`SpecFailure`] when the spec cannot be read or parsed. A name
/// that the spec does not declare is a failure of that name alone.
pub(crate) fn explain(
    args: &ExplainArgs,
    vars: Vec<(OsString, OsString)>,
) -> Result<Batch<Explained, UnknownVariable>, SpecFailure> {
    let spec = spec::open(&args.file.spec)?.spec;
    let file = args.file.spec.display().to_string();
    let bindings = starmap::capture(&spec, vars);
    let names: Vec<Name> = if args.names.is_empty() {
        spec.variables().map(|(name, _)| name.clone()).collect()
    } else {
        args.names.clone()
    };
    let (_, note) = mode::decide(&spec, &bindings);
    let mut note = format!("{note} Values come from this shell, and explain resolves no bind.");
    if args.names.is_empty() {
        note.push(' ');
        note.push_str(&built_ins_sentence(BuiltIns::current()));
    }
    let mut batch = Batch::new(&["explain"], "names", &names).note(note);
    for name in names {
        batch.push(row(&spec, &file, &bindings, name));
    }
    Ok(batch)
}

/// Returns the sentence that lists `built_ins`, the variables that
/// `starmap run --only-declared` passes to the program without a
/// declaration.
fn built_ins_sentence(built_ins: &BuiltIns) -> String {
    let names: Vec<&str> = built_ins.names().collect();
    format!(
        "starmap run --only-declared also passes these built-in variables from the shell: {}.",
        names.join(", ")
    )
}

/// Returns the row of the variable `name`, or the failure of a name that
/// `spec`, read from `file`, does not declare.
fn row(
    spec: &Spec,
    file: &str,
    bindings: &Bindings,
    name: Name,
) -> Result<Explained, UnknownVariable> {
    let Some(variable) = spec.variable(&name) else {
        return Err(UnknownVariable {
            name,
            file: file.to_owned(),
        });
    };
    Ok(Explained {
        kind: type_text(variable.value_type()),
        unset: unset_text(variable),
        source: source_text(variable.source()),
        value: cell(variable, bindings.get(&name)),
        show: variable.show().to_string(),
        bind: bind_text(variable.source()),
        cache: cache_text(variable.source()).map(str::to_owned),
        purpose: variable.purpose().to_owned(),
        effect: variable.effect().map(str::to_owned),
        check: variable.check().map(str::to_owned),
        enumeration: enumeration_name(variable.value_type()),
        values: Values::of(variable.value_type()),
        group: spec.group_of(&name).map(|group| group.name().to_string()),
        name: name.as_str().to_owned(),
    })
}

/// Returns the cell of `variable`'s value, which `bound` gives when this
/// shell sets it. A value that counts as unset, such as a relative path
/// where the type takes an absolute one, falls back to the default.
/// A shell value that is not Unicode is shown with each invalid sequence
/// replaced by U+FFFD.
fn cell(variable: &Variable, bound: Option<&Bound>) -> Cell {
    match bound {
        Some(Bound::Plain(raw)) => match variable.value_type().parse(raw) {
            Ok(Some(_)) => return Cell::new(State::Set, Some(raw.to_string_lossy().into_owned())),
            Ok(None) => {}
            Err(_) => {
                return Cell::new(State::Invalid, Some(raw.to_string_lossy().into_owned()));
            }
        },
        // The cell never holds a withheld value's text, whichever state it
        // shows.
        Some(Bound::Secret(secret)) => {
            let state = match variable.value_type().check_secret(secret) {
                Ok(()) => State::Set,
                Err(_) => State::Invalid,
            };
            return Cell::new(state, None);
        }
        Some(_) => return Cell::new(State::Invalid, None),
        None => {}
    }
    match variable.unset().in_mode(Mode::Local) {
        Unset::Default(value) => Cell::new(State::Default, Some(value_text(value))),
        Unset::Required | Unset::Optional => Cell::new(State::Unset, None),
    }
}

/// Returns a value's text, as an environment would hold it.
pub(crate) fn value_text(value: &Value) -> String {
    match value {
        Value::Path(path) => path.display().to_string(),
        Value::Port(port) => port.to_string(),
        Value::Url(url) => url.to_string(),
        Value::SocketAddress(address) => address.to_string(),
        Value::Boolean(boolean) => boolean.to_string(),
        Value::Integer(number) => number.to_string(),
        Value::Choice(choice) => choice.to_string(),
        Value::String(string) => string.clone(),
        Value::Bytes { .. } => value.to_string(),
        // A kind of value that a later release of starmap adds.
        _ => "a value".to_owned(),
    }
}

/// Returns the text of a type, such as `integer 1..=100`.
pub(crate) fn type_text(ty: &Type) -> String {
    match ty {
        Type::Path { absolute: false } => "path".to_owned(),
        Type::Path { absolute: true } => "absolute path".to_owned(),
        Type::Port => "port".to_owned(),
        Type::Url { schemes: None } => "url".to_owned(),
        Type::Url {
            schemes: Some(schemes),
        } => format!("url ({schemes})"),
        Type::SocketAddress => "ip socket address".to_owned(),
        Type::Boolean => "boolean".to_owned(),
        Type::Integer(range) => format!("integer {}..={}", range.min(), range.max()),
        Type::Enumeration(choices) => match choices.name() {
            Some(name) => format!("{name}: one of {choices}"),
            None => format!("one of {choices}"),
        },
        Type::String { pattern: None } => "string".to_owned(),
        Type::String {
            pattern: Some(pattern),
        } => format!("string matching {pattern}"),
        Type::Bytes(rule) => rule.to_string(),
        // A type that a later release of starmap adds.
        _ => "another type".to_owned(),
    }
}

/// Returns the name of a named enumeration type, or `None` for any other
/// type.
fn enumeration_name(ty: &Type) -> Option<String> {
    match ty {
        Type::Enumeration(choices) => choices.name().map(ToString::to_string),
        _ => None,
    }
}

/// Returns a variable's unset rule, such as `default dev` or `optional;
/// production required`.
pub(crate) fn unset_text(variable: &Variable) -> String {
    let word = |unset: &Unset| match unset {
        Unset::Required => "required".to_owned(),
        Unset::Optional => "optional".to_owned(),
        Unset::Default(value) => format!("default {}", value_text(value)),
    };
    let local = variable.unset().in_mode(Mode::Local);
    let production = variable.unset().in_mode(Mode::Production);
    if local == production {
        word(local)
    } else {
        format!("{}; production {}", word(local), word(production))
    }
}

/// Returns a variable's source with the kind of its bind, such as `secret,
/// 1Password` or `provided multiverse/port`.
pub(crate) fn source_text(source: &Source) -> String {
    match source {
        Source::Setting { bind: None } => "setting".to_owned(),
        Source::Setting { bind: Some(_) } => "setting, path bind".to_owned(),
        Source::Secret { bind: None } => "secret".to_owned(),
        Source::Secret { bind: Some(bind) } => match bind.reference() {
            SecretRef::OnePassword(_) => "secret, 1Password".to_owned(),
            // A reference that a later release of starmap adds.
            _ => "secret".to_owned(),
        },
        Source::Generated { .. } => "generated".to_owned(),
        Source::Inherited => "inherited".to_owned(),
        Source::Provided(reference) => format!("provided {}", reference.kind()),
        // A source or reference that a later release of starmap adds.
        _ => "another source".to_owned(),
    }
}

/// Returns `never` for a secret whose bind keeps the value out of any
/// cache of resolved values, and `None` for every other variable.
pub(crate) fn cache_text(source: &Source) -> Option<&'static str> {
    match source {
        Source::Secret { bind: Some(bind) } if bind.cache() == Cache::Never => Some("never"),
        _ => None,
    }
}

/// Returns a variable's bind, such as `op://Personal/shelf mirror/token`,
/// or `None` for a variable without one.
pub(crate) fn bind_text(source: &Source) -> Option<String> {
    match source {
        Source::Setting {
            bind: Some(template),
        } => Some(template.as_str().to_owned()),
        Source::Secret { bind: Some(bind) } => match bind.reference() {
            SecretRef::OnePassword(reference) => Some(reference.to_string()),
            // A reference that a later release of starmap adds.
            _ => None,
        },
        Source::Generated { bytes, encoding } => {
            Some(format!("{} random bytes as {encoding}", bytes.get()))
        }
        Source::Provided(reference) => {
            let mut text = reference.kind().to_string();
            for (key, value) in reference.qualifiers() {
                text.push_str(&format!(" {key}={value}"));
            }
            Some(text)
        }
        _ => None,
    }
}