Skip to main content

starmap_cli/
lib.rs

1//! The `starmap` command, which writes, checks, explains and runs an
2//! application's declared environment.
3//!
4//! The library holds the whole command, and the `starmap` binary calls
5//! `run`. It exports the parser, the tool record and the failure codes, so the
6//! integration tests can pass them to crumbkit-test's checks beside the
7//! built binary. It is not an API for other crates, so each public item is
8//! hidden from the documentation and outside the semver promise.
9
10mod check;
11mod cli;
12#[doc(hidden)]
13pub mod codes;
14mod diff;
15mod dotenv;
16mod explain;
17mod files;
18mod git;
19mod init;
20mod mode;
21mod run;
22mod scan;
23mod spec;
24
25use std::ffi::OsString;
26use std::process::ExitCode;
27
28use clap::CommandFactory as _;
29
30#[doc(hidden)]
31pub use crate::cli::{Cli, TOOL};
32use crate::cli::{
33    CommandsCommand, CompletionCommand, SKILL, SchemasCommand, SelfCommand, SkillsCommand, Verb,
34};
35
36/// Parses `args`, runs the command and returns the process's exit code. The
37/// first argument is the program's name. `vars` is the process environment,
38/// which `main` reads once (BASE-6), and `check`, `explain` and `run` read
39/// the declared variables from it.
40///
41/// `crumbkit::parse_from` prints help or a refused command line and returns
42/// the exit code. `crumbkit::finish` prints the result or the failure in
43/// the format that the output flags choose, and returns the exit code.
44#[doc(hidden)]
45#[must_use]
46pub fn run<I, T>(args: I, vars: Vec<(OsString, OsString)>) -> ExitCode
47where
48    I: IntoIterator<Item = T>,
49    T: Into<OsString> + Clone,
50{
51    let (cli, mut output) = match crumbkit::parse_from::<Cli>(&TOOL, args) {
52        Ok(parsed) => parsed,
53        Err(stop) => return stop.exit_code(),
54    };
55    match cli.verb {
56        Verb::Init(args) => {
57            let dry_run = output.is_dry_run();
58            crumbkit::finish(output, init::INIT, init::init(&args, dry_run))
59        }
60        Verb::Check(args) => crumbkit::finish(output, check::CHECK, check::check(&args, vars)),
61        Verb::Explain(args) => {
62            crumbkit::finish(output, explain::EXPLAIN, explain::explain(&args, vars))
63        }
64        Verb::Diff(args) => crumbkit::finish(output, diff::DIFF, diff::diff(&args)),
65        Verb::Run(args) => {
66            #[expect(
67                clippy::disallowed_methods,
68                reason = "run prints its notes on standard error before the child takes the terminal, after which nothing of starmap prints"
69            )]
70            let mut notes = std::io::stderr();
71            run::run(&args, vars, output, &mut notes)
72        }
73        // `commands list`, `schemas` and `self usage` read the command tree through
74        // `&mut output`, so each runs before `finish` takes `output`.
75        Verb::Commands {
76            command: CommandsCommand::List(args),
77        } => {
78            let result = crumbkit::commands::list(&mut output, &TOOL, &args);
79            crumbkit::finish(output, crumbkit::commands::COMMANDS_LIST, result)
80        }
81        Verb::Schemas {
82            command: SchemasCommand::List(args),
83        } => {
84            let result = crumbkit::schemas::list(&mut output, &TOOL, &args);
85            crumbkit::finish(output, crumbkit::schemas::SCHEMAS_LIST, result)
86        }
87        Verb::Schemas {
88            command: SchemasCommand::Show(args),
89        } => {
90            let result = crumbkit::schemas::show(&mut output, &TOOL, &args);
91            crumbkit::finish(output, crumbkit::schemas::SCHEMAS_SHOW, result)
92        }
93        Verb::SelfCommand {
94            command: SelfCommand::Usage(args),
95        } => {
96            let result =
97                Ok::<_, crumbkit::Error>(crumbkit_usage::findings(&mut output, &TOOL, &args));
98            crumbkit::finish(output, crumbkit_usage::SELF_USAGE, result)
99        }
100        Verb::Skills { command } => match command {
101            SkillsCommand::Install(args) => {
102                let result = Ok::<_, crumbkit::Error>(crumbkit_skills::install(&SKILL, &args));
103                crumbkit::finish(output, crumbkit_skills::SKILLS_INSTALL, result)
104            }
105            SkillsCommand::Check(args) => {
106                let result = Ok::<_, crumbkit::Error>(crumbkit_skills::check(&SKILL, &args));
107                crumbkit::finish(output, crumbkit_skills::SKILLS_CHECK, result)
108            }
109            SkillsCommand::Print => {
110                let result = Ok::<_, crumbkit::Error>(crumbkit_skills::print(&SKILL));
111                crumbkit::finish(output, crumbkit_skills::SKILLS_PRINT, result)
112            }
113        },
114        Verb::Completion { command } => match command {
115            CompletionCommand::Install(args) => {
116                let result = crumbkit::completion::install(Cli::command, &args);
117                crumbkit::finish(output, crumbkit::completion::COMPLETION_INSTALL, result)
118            }
119            CompletionCommand::Check(args) => {
120                let result = crumbkit::completion::check(Cli::command, &args);
121                crumbkit::finish(output, crumbkit::completion::COMPLETION_CHECK, result)
122            }
123            CompletionCommand::Print(args) => {
124                let result = crumbkit::completion::print(Cli::command, &args);
125                crumbkit::finish(output, crumbkit::completion::COMPLETION_PRINT, result)
126            }
127        },
128    }
129}