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