starmap-cli 0.2.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! `starmap run`, which resolves and checks every declared variable and
//! starts a program with exactly the declared environment applied.
//!
//! The values of the caller come from `.env` beside the spec, or the file
//! that a pointer names, the shell and `--set`, and `.env` and `--set` never give a secret. The doc of
//! `Verb::Run` in cli.rs, which `starmap run --help` prints, states the
//! order in which a value is taken from them, a setting's path bind and
//! the default. starmap-resolve then resolves every variable under
//! one deadline, with the 1Password CLI and the Keychain as resolvers and
//! no provider. A required secret without a bind that nothing supplies is
//! asked for at a terminal, held for this run, and saved to the Keychain
//! when the person agrees. The program starts through crumbkit-child,
//! which passes on its exit code.

use std::ffi::OsString;
use std::fmt;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::{Duration, Instant};

use crumbkit::{Failure, Recovery, Run, Sink};
use starmap::{
    Bindings, Bound, Environment, Mode, Name, NoPlace, Placeholder, Problem, ProviderKind, Source,
    Spec, Verdict,
};
use starmap_resolve::{
    AppName, Context, Keychain, Lookup, OnePassword, Places, ResolveProblem, Resolver, SecretError,
    Slot, WorkspaceName, resolve,
};

use crate::cli::{Assignment, RunArgs, SpecFile};
use crate::codes::{
    BIND_NO_PLACE, BIND_NO_PROVIDER, SECRET_UNBOUND, SECRET_UNREADABLE, VARIABLE_INVALID,
    VARIABLE_MISSING, VARIABLE_SECRET, VARIABLE_UNKNOWN,
};
use crate::dotenv::{self, DotEnvFailure};
use crate::spec::{self, SpecFailure};
use crate::{files, mode};

mod ask;
mod child;
#[cfg(test)]
mod tests;

pub(crate) use ask::{Ask, Terminal};

/// How long resolution may take, from its start to the last secret store's
/// answer (BASE-34). A 1Password or Keychain prompt that waits for the
/// person counts against it. The question that asks a person for a secret
/// comes after resolution and has no deadline.
const RESOLVE_DEADLINE: Duration = Duration::from_secs(60);

/// How long saving one typed secret to the Keychain may take.
const SAVE_DEADLINE: Duration = Duration::from_secs(30);

/// Why `--fields` is refused.
///
/// BASE-37: crumbkit-child 0.2.0 keeps its own copy of this text private,
/// so this one repeats it word for word, and the two can drift apart. A
/// crumbkit-child release that exports the text replaces this copy.
const TEXT_ONLY: &str = "this command hands the terminal to another program";

/// Runs `args.program` with the environment that the spec declares, and
/// returns its exit code. `vars` is the process environment, and `notes`
/// receives each note before the program starts. `output` says whether the
/// caller passed `--no-input`. 1Password and the Keychain resolve the
/// secrets.
pub(crate) fn run(
    args: &RunArgs,
    vars: Vec<(OsString, OsString)>,
    output: Run,
    notes: &mut dyn Write,
) -> ExitCode {
    let one_password = OnePassword::new(PathBuf::from("op"));
    let keychain = Keychain::new();
    run_with(args, vars, output, notes, &[&one_password, &keychain])
}

/// Runs `args.program` as [`run`] does, with `resolvers` resolving the
/// secrets.
fn run_with(
    args: &RunArgs,
    vars: Vec<(OsString, OsString)>,
    mut output: Run,
    notes: &mut dyn Write,
    resolvers: &[&dyn Resolver],
) -> ExitCode {
    let mut sink = Sink::standard();
    if let Some(code) = output.refuse_fields_for_text(&mut sink, TEXT_ONLY) {
        return code;
    }
    let mut command = child::command(&args.program);
    let gathered = match gather(args, vars) {
        Ok(gathered) => gathered,
        Err(refused) => return refuse(&mut sink, &mut output, &refused, notes),
    };
    if output.is_json() {
        print_notes(notes, &gathered.notes);
        // crumbkit-child refuses --json before it starts anything, because
        // the program takes the terminal. Refusing here, before
        // resolution, means no secret store is asked and no Keychain item
        // is created for a run that cannot start.
        return crumbkit_child::hand_over(output, command);
    }
    let interactive = !output.no_input() && ask::terminals();
    let settled = {
        let mut terminal = Terminal::new(&mut output, interactive);
        settle(gathered, resolvers, &mut terminal)
    };
    match settled {
        Ok(ready) => {
            print_notes(notes, &ready.notes);
            ready.environment.apply(&mut command);
            // BASE-37: hand_over ignores SIGINT and SIGQUIT, which the
            // terminal sends to the program too, and forwards no signal.
            // A SIGTERM or SIGHUP sent to starmap alone, as a process
            // manager sends it, ends starmap and leaves the program
            // running. Signal forwarding in crumbkit-child replaces this.
            crumbkit_child::hand_over(output, command)
        }
        Err(refused) => refuse(&mut sink, &mut output, &refused, notes),
    }
}

/// Prints each failure of `refused` and then its notes, and returns the
/// exit code of its first failure.
fn refuse<O: Write, E: Write>(
    sink: &mut Sink<O, E>,
    output: &mut Run,
    refused: &Refused,
    notes: &mut dyn Write,
) -> ExitCode {
    for failure in &refused.failures {
        output.print_failure(sink, failure);
    }
    print_notes(notes, &refused.notes);
    let code = refused
        .failures
        .first()
        .map_or(1, |failure| output.failure_exit(failure.code()));
    crumbkit::events::ended(code, None)
}

/// Writes each of `lines` to `notes`. A note that cannot be written is
/// dropped, because the run's outcome does not depend on it.
fn print_notes(notes: &mut dyn Write, lines: &[String]) {
    for line in lines {
        let _ = writeln!(notes, "{line}");
    }
}

/// A checked environment that the program starts with, and the notes to
/// print before it starts.
#[derive(Debug)]
pub(crate) struct Ready {
    /// The checked values of every declared variable.
    pub(crate) environment: Environment,
    /// The notes, in the order they print.
    pub(crate) notes: Vec<String>,
}

/// The failures that stop the program from starting, with the notes to
/// print after them.
#[derive(Debug)]
pub(crate) struct Refused {
    /// Each failure, at least one.
    pub(crate) failures: Vec<RunFailure>,
    /// The notes, in the order they print.
    pub(crate) notes: Vec<String>,
}

impl Refused {
    /// Returns the refusal of `failures`, with a closing note that says
    /// nothing started.
    fn new(failures: Vec<RunFailure>, mut notes: Vec<String>) -> Self {
        let count = failures.len();
        let problems = if count == 1 { "problem" } else { "problems" };
        notes.push(format!(
            "starmap started nothing, because of {count} {problems}."
        ));
        Self { failures, notes }
    }
}

/// Everything a run knows before it asks a secret store: the spec, the
/// caller's values, the mode and the places.
#[derive(Debug)]
pub(crate) struct Gathered {
    spec: Spec,
    /// The spec file, as the caller gave it, which a recovery passes to
    /// `starmap explain`.
    file: String,
    caller: Bindings,
    mode: Mode,
    places: Places,
    /// The workspace that a typed secret is saved under.
    workspace: WorkspaceName,
    /// The app that a typed secret is saved under.
    app: AppName,
    notes: Vec<String>,
}

/// Reads the spec and `.env`, takes the caller's values and decides the
/// mode. It asks no secret store.
///
/// # Errors
///
/// Returns every failure of the spec, `.env` or `--set`.
pub(crate) fn gather(args: &RunArgs, vars: Vec<(OsString, OsString)>) -> Result<Gathered, Refused> {
    let mut notes = Vec::new();
    let path = &args.file.spec;
    let spec::Opened { spec, located } =
        spec::open(path).map_err(|failure| Refused::new(vec![failure.into()], Vec::new()))?;
    let checkout = checkout(path)?;
    let dotenv_file = located.dotenv;
    let dotenv = dotenv::read(&dotenv_file, &spec).map_err(|failures| {
        Refused::new(failures.into_iter().map(Into::into).collect(), Vec::new())
    })?;
    if !dotenv.undeclared.is_empty() {
        notes.push(format!(
            "{} sets {}, which {} does not declare, so the program does not receive {}.",
            dotenv_file.display(),
            dotenv.undeclared.join(", "),
            located.file.display(),
            if dotenv.undeclared.len() == 1 {
                "it"
            } else {
                "them"
            },
        ));
    }
    let home = vars
        .iter()
        .find(|(key, _)| key == "HOME")
        .map(|(_, value)| PathBuf::from(value));
    let overrides =
        assignments(&spec, &args.set).map_err(|failures| Refused::new(failures, notes.clone()))?;
    let values = dotenv.values.into_iter().chain(vars).chain(overrides);
    let caller = starmap::capture(&spec, values);
    let (mode, mode_note) = mode::decide(&spec, &caller);
    notes.push(mode_note);

    let app = AppName::of_dir(&checkout);
    let workspace = args
        .workspace
        .clone()
        .unwrap_or_else(|| WorkspaceName::from(&app));
    let mut places =
        Places::new(checkout.clone()).workspace(workspace.clone(), app.clone(), checkout);
    if let Some(home) = home {
        places = places.home(home);
    }
    Ok(Gathered {
        spec,
        file: path.display().to_string(),
        caller,
        mode,
        places,
        workspace,
        app,
        notes,
    })
}

/// Resolves every variable of `gathered` with `resolvers`, asks `ask` for
/// each required secret that nothing supplies when nothing else failed,
/// and checks the result.
///
/// # Errors
///
/// Returns every failure that stops the program from starting.
pub(crate) fn settle(
    gathered: Gathered,
    resolvers: &[&dyn Resolver],
    ask: &mut dyn Ask,
) -> Result<Ready, Refused> {
    let Gathered {
        spec,
        file,
        caller,
        mode,
        places,
        workspace,
        app,
        mut notes,
    } = gathered;
    let deadline = Instant::now() + RESOLVE_DEADLINE;
    let mut context = Context::new(places, &caller, deadline);
    for resolver in resolvers {
        context = context.resolver(*resolver);
    }
    let resolution = resolve(&spec, mode, &context);
    for name in &resolution.ignored {
        notes.push(ignored_note(&spec, name));
    }
    let mut bindings = resolution.bindings;
    let (unbound, other): (Vec<_>, Vec<_>) = resolution
        .failures
        .into_iter()
        .partition(|failure| matches!(failure.problem, ResolveProblem::Unbound));
    let mut failures: Vec<RunFailure> = other
        .into_iter()
        .map(|failure| RunFailure::resolve(failure.name, failure.problem))
        .collect();
    // A person is asked only when the typed values would let the program
    // start, so nobody types a secret for a run that fails anyway. The
    // check runs first on every value but the unbound secrets, which it
    // refuses as missing until someone types them.
    if failures.is_empty()
        && !unbound.is_empty()
        && let Verdict::Refused(refusals) = starmap::check(&spec, &bindings, mode)
    {
        failures.extend(
            refusals
                .iter()
                .filter(|finding| !unbound.iter().any(|failure| failure.name == finding.name))
                .map(|finding| RunFailure::check(finding.name.clone(), &finding.problem, &file)),
        );
    }
    let can_start = failures.is_empty();
    for failure in unbound {
        let typed = if can_start {
            typed(
                Slot::new(&workspace, &app, &failure.name),
                resolvers,
                ask,
                &mut notes,
            )
        } else {
            None
        };
        match typed {
            Some(bound) => bindings.bind(failure.name, bound),
            None => failures.push(RunFailure::Unbound { name: failure.name }),
        }
    }
    if !failures.is_empty() {
        return Err(Refused::new(failures, notes));
    }
    checked(&spec, &file, &bindings, mode, notes)
}

/// Checks `bindings` in `mode`, and returns the environment or each
/// variable that stops the check.
fn checked(
    spec: &Spec,
    file: &str,
    bindings: &Bindings,
    mode: Mode,
    notes: Vec<String>,
) -> Result<Ready, Refused> {
    match starmap::check(spec, bindings, mode) {
        Verdict::Ready(environment) => Ok(Ready { environment, notes }),
        Verdict::Refused(refusals) => {
            let failures = refusals
                .iter()
                .map(|finding| RunFailure::check(finding.name.clone(), &finding.problem, file))
                .collect();
            Err(Refused::new(failures, notes))
        }
    }
}

/// Asks `ask` for the secret of `slot`, and offers to save the answer with
/// the first of `resolvers` that keeps saved values. It returns `None` when
/// no person can answer or the person typed nothing.
fn typed(
    slot: Slot<'_>,
    resolvers: &[&dyn Resolver],
    ask: &mut dyn Ask,
    notes: &mut Vec<String>,
) -> Option<Bound> {
    if !ask.can_ask() {
        return None;
    }
    let name = slot.name;
    let secret = ask.secret(name)?;
    let lookup = Lookup::Saved { slot };
    let (workspace, app) = (slot.workspace.as_str(), slot.app.as_str());
    let question = format!(
        "Save {name} to the Keychain for workspace {workspace} and app {app}, so later runs find it?"
    );
    if ask.save(&question) {
        let deadline = Instant::now() + SAVE_DEADLINE;
        let mut result = Err(SecretError::Unsupported);
        for resolver in resolvers {
            result = resolver.save(lookup, &secret, deadline);
            if !matches!(result, Err(SecretError::Unsupported)) {
                break;
            }
        }
        // The Keychain keeps a value it already holds for the same item,
        // so the note says what the Keychain holds, not that it took the
        // typed value.
        notes.push(match result {
            Ok(()) => format!(
                "The Keychain holds {name} as service starmap.{workspace} and account {app}/{name}, and later runs read it from there."
            ),
            Err(error) => format!(
                "starmap could not save {name} to the Keychain, because {error}, so only this run has it."
            ),
        });
    }
    Some(Bound::Secret(secret))
}

/// Returns the note of a caller value that resolution ignored for the
/// variable `name`.
fn ignored_note(spec: &Spec, name: &Name) -> String {
    let used = match spec.variable(name).map(starmap::Variable::source) {
        Some(Source::Generated { .. }) => "its generated value in the Keychain",
        _ => "its bind",
    };
    format!("{name} is set in this shell, and starmap ignored it and used {used}.")
}

/// Returns the caller values of `set`, or a failure for each name that the
/// spec does not declare or declares as a secret.
fn assignments(
    spec: &Spec,
    set: &[Assignment],
) -> Result<Vec<(OsString, OsString)>, Vec<RunFailure>> {
    let mut values = Vec::new();
    let mut failures = Vec::new();
    for assignment in set {
        let name = &assignment.name;
        match spec.variable(name).map(starmap::Variable::source) {
            None => failures.push(RunFailure::Unknown { name: name.clone() }),
            Some(Source::Secret { .. } | Source::Generated { .. }) => {
                failures.push(RunFailure::SetSecret { name: name.clone() });
            }
            Some(_) => values.push((
                OsString::from(name.as_str()),
                OsString::from(&assignment.value),
            )),
        }
    }
    if failures.is_empty() {
        Ok(values)
    } else {
        Err(failures)
    }
}

/// Returns the absolute directory of the file at `path`, the checkout. For a
/// pointer, the checkout is the pointer's directory, wherever the file it
/// names lies.
fn checkout(path: &Path) -> Result<PathBuf, Refused> {
    let file = path.display().to_string();
    files::canonical(path)
        .map(|path| path.parent().map(Path::to_path_buf).unwrap_or(path))
        .map_err(|error| {
            Refused::new(
                vec![SpecFailure::Unreadable { file, error }.into()],
                Vec::new(),
            )
        })
}

/// A failure that stops `starmap run` from starting the program.
#[derive(Debug, Failure)]
pub(crate) enum RunFailure {
    /// The spec cannot be read or parsed.
    #[failure(forward, diagnostic)]
    Spec(SpecFailure),
    /// `.env` cannot be read or parsed, or sets a secret.
    #[failure(forward)]
    DotEnv(DotEnvFailure),
    /// `--set` names a variable the spec does not declare.
    #[failure(
        code = VARIABLE_UNKNOWN,
        hint = "declare {name} in starmap.toml",
        alternative = Recovery::Hint(format!("remove --set {name}")),
        field(key = name.as_str()),
    )]
    Unknown {
        /// The variable.
        name: Name,
    },
    /// `--set` gives a secret or generated variable a value.
    #[failure(
        code = VARIABLE_SECRET,
        hint = "remove --set {name}, and give {name} a bind in starmap.toml",
        alternative = Recovery::Hint(format!(
            "remove --set {name}, and let starmap run ask for it at a terminal"
        )),
        field(key = name.as_str()),
    )]
    SetSecret {
        /// The variable.
        name: Name,
    },
    /// A required variable has no value.
    #[failure(
        code = VARIABLE_MISSING,
        hint = "set {name} in this shell, in .env or with --set {name}=VALUE",
        field(key = name.as_str()),
    )]
    Missing {
        /// The variable.
        name: Name,
        /// The mode the check ran in.
        mode: Mode,
    },
    /// A variable's value does not fit its type.
    #[failure(
        code = VARIABLE_INVALID,
        recovery = Recovery::Run(SpecFile::call(&["explain"], file).value("names", name.as_str())),
        field(key = name.as_str()),
    )]
    Invalid {
        /// The variable.
        name: Name,
        /// What is wrong with the value.
        problem: String,
        /// The spec file, as the caller gave it.
        file: String,
    },
    /// No provider of the variable's kind is present.
    #[failure(
        code = BIND_NO_PROVIDER,
        hint = "set {name} in this shell, in .env or with --set {name}=VALUE",
        alternative = Recovery::Hint(format!("start the app under a host that provides {kind}")),
        field(key = name.as_str()),
    )]
    NoProvider {
        /// The variable.
        name: Name,
        /// The kind that no provider supplies.
        kind: ProviderKind,
    },
    /// A path bind names a place that run has no directory for.
    #[failure(
        code = BIND_NO_PLACE,
        hint = "set HOME in this shell",
        alternative = Recovery::Hint(format!("set {name} with --set {name}=VALUE")),
        field(key = name.as_str()),
    )]
    NoPlace {
        /// The variable.
        name: Name,
        /// The placeholder without a place.
        placeholder: Placeholder,
    },
    /// A required secret without a bind has no value, and no person typed
    /// one.
    #[failure(
        code = SECRET_UNBOUND,
        hint = "run starmap run at a terminal without --no-input, type {name} and save it to the Keychain",
        alternative = Recovery::Hint(format!(
            "give {name} a bind in starmap.toml, such as {{ one-password = \"op://vault/item/field\" }}"
        )),
        field(key = name.as_str()),
    )]
    Unbound {
        /// The variable.
        name: Name,
    },
    /// No secret store that run registers handles the variable's lookup,
    /// such as a generated value where the session has no login keychain.
    #[failure(
        code = SECRET_UNREADABLE,
        hint = "starmap run keeps generated values only in the macOS login keychain, so run it in a macOS session that has one, or make {name} a secret with a one-password bind in starmap.toml",
        field(key = name.as_str()),
    )]
    NoStore {
        /// The variable.
        name: Name,
    },
    /// A secret store could not return the secret.
    #[failure(
        code = SECRET_UNREADABLE,
        hint = "check the bind of {name} in starmap.toml, unlock 1Password or the Keychain, and check that the 1Password CLI op is installed and signed in, then run the command again",
        field(key = name.as_str()),
    )]
    Secret {
        /// The variable.
        name: Name,
        /// What the secret store returned.
        error: SecretError,
    },
}

impl RunFailure {
    /// Returns the failure of the variable `name` that resolution could
    /// not resolve because of `problem`.
    fn resolve(name: Name, problem: ResolveProblem) -> Self {
        match problem {
            ResolveProblem::NoProvider(kind) => Self::NoProvider { name, kind },
            ResolveProblem::NoPlace(NoPlace(placeholder)) => Self::NoPlace { name, placeholder },
            ResolveProblem::Unbound => Self::Unbound { name },
            ResolveProblem::Secret(error) => Self::Secret { name, error },
            ResolveProblem::Deadline => Self::Secret {
                name,
                error: SecretError::Deadline,
            },
            ResolveProblem::NoResolver => Self::NoStore { name },
            // run registers no provider, so a provider error reads as a
            // store that failed. So does a problem that a later
            // starmap-resolve adds, until this command learns it.
            ResolveProblem::Provider(error) => Self::Secret {
                name,
                error: SecretError::Program(io::Error::other(error.to_string())),
            },
            _ => Self::Secret {
                name,
                error: SecretError::Program(io::Error::other("the value could not be resolved")),
            },
        }
    }

    /// Returns the failure of the variable `name` that the check of the
    /// spec `file` refused because of `problem`.
    fn check(name: Name, problem: &Problem, file: &str) -> Self {
        let file = file.to_owned();
        match problem {
            Problem::Missing { mode } => Self::Missing { name, mode: *mode },
            Problem::Invalid(error) => Self::Invalid {
                name,
                problem: error.to_string(),
                file,
            },
            Problem::ShowMismatch { .. } => Self::Invalid {
                name,
                problem: "the value is held as a secret where starmap.toml shows it, or the other way round"
                    .to_owned(),
                file,
            },
            Problem::ModeMismatch { decided } => Self::Invalid {
                name,
                problem: format!("the resolved value decides {decided} mode"),
                file,
            },
            // A problem that a later starmap adds reads as an invalid
            // value, until this command learns it.
            _ => Self::Invalid {
                name,
                problem: "the value does not fit the variable".to_owned(),
                file,
            },
        }
    }
}

impl fmt::Display for RunFailure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Spec(failure) => failure.fmt(formatter),
            Self::DotEnv(failure) => failure.fmt(formatter),
            Self::Unknown { name } => {
                write!(
                    formatter,
                    "--set names {name}, which starmap.toml does not declare"
                )
            }
            Self::SetSecret { name } => write!(
                formatter,
                "--set gives {name} a value, and starmap.toml declares it as a secret"
            ),
            Self::Missing { name, mode } => {
                write!(
                    formatter,
                    "{name} is required in {mode} mode and has no value"
                )
            }
            Self::Invalid { name, problem, .. } => write!(formatter, "{name}: {problem}"),
            Self::NoProvider { name, kind } => write!(
                formatter,
                "{name} needs a value from the provider {kind}, and starmap run has no provider of that kind"
            ),
            Self::NoPlace { name, placeholder } => write!(
                formatter,
                "the path bind of {name} uses {placeholder}, and starmap run has no directory for it"
            ),
            Self::Unbound { name } => write!(
                formatter,
                "{name} is a secret with no bind, no value in this shell and no value saved in the Keychain"
            ),
            Self::NoStore { name } => write!(
                formatter,
                "no secret store that starmap run uses on this machine handles {name}"
            ),
            Self::Secret { name, error } => {
                write!(formatter, "{name} could not be read, because {error}")
            }
        }
    }
}

impl std::error::Error for RunFailure {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Spec(failure) => failure.source(),
            Self::DotEnv(failure) => failure.source(),
            Self::Secret { error, .. } => Some(error),
            _ => None,
        }
    }
}

impl From<SpecFailure> for RunFailure {
    fn from(failure: SpecFailure) -> Self {
        Self::Spec(failure)
    }
}

impl From<DotEnvFailure> for RunFailure {
    fn from(failure: DotEnvFailure) -> Self {
        Self::DotEnv(failure)
    }
}