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::Script { name, arguments } => commands::script::execute(&name, &arguments),
36        CliCommand::Db(cmd) => commands::db::execute(cmd),
37    };
38    match result {
39        Ok(()) => ExitCode::SUCCESS,
40        Err(error) => {
41            eprintln!("error: {error}");
42            ExitCode::FAILURE
43        }
44    }
45}