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