Skip to main content

arcature_cli/cli/
run.rs

1use std::ffi::OsString;
2use std::process::ExitCode;
3
4use super::{CliCommand, parse};
5use crate::commands;
6
7/// Parse arguments, execute one Arcature lifecycle command, and return its
8/// process exit status.
9pub fn run<I, T>(arguments: I) -> ExitCode
10where
11    I: IntoIterator<Item = T>,
12    T: Into<OsString> + Clone,
13{
14    let command = match parse(arguments) {
15        Ok(command) => command,
16        Err(error) => {
17            let success = error.exit_code() == 0;
18            if let Err(print_error) = error.print() {
19                eprintln!("error: cannot print command-line diagnostic: {print_error}");
20            }
21            return if success {
22                ExitCode::SUCCESS
23            } else {
24                ExitCode::FAILURE
25            };
26        }
27    };
28    let result = match command {
29        CliCommand::New(options) => commands::new::execute(options),
30        CliCommand::Dev => commands::dev::execute(),
31        CliCommand::Build => commands::build::execute(),
32        CliCommand::Check(format) => commands::check::execute(format),
33        CliCommand::Doctor(format) => commands::doctor::execute(format),
34        CliCommand::Exposure(format) => commands::exposure::execute(format),
35        CliCommand::Routes(format) => commands::routes::execute(format),
36        CliCommand::Modules(format) => commands::modules::execute(format),
37        CliCommand::Services(format) => commands::services::execute(format),
38        CliCommand::Schedule(format) => commands::schedule::execute(format),
39        CliCommand::Run { name, arguments } => commands::run_command::execute(&name, &arguments),
40        CliCommand::Make(options) => commands::make::execute(options),
41        CliCommand::Script { name, arguments } => commands::script::execute(&name, &arguments),
42        CliCommand::Db(cmd) => commands::db::execute(cmd),
43    };
44    match result {
45        Ok(()) => ExitCode::SUCCESS,
46        Err(error) => {
47            eprintln!("error: {error}");
48            ExitCode::FAILURE
49        }
50    }
51}