1mod args;
2mod commands;
3mod error;
4pub mod output;
5
6use std::env;
7use std::process::{ExitCode, ExitStatus};
8
9use clap::{CommandFactory, Parser, error::ErrorKind};
10
11use crate::application::run::{self, RunOptions};
12use crate::project::Project;
13
14use args::{Cli, Commands};
15use error::CliError;
16
17pub fn run() -> ExitCode {
18 let cli = Cli::parse();
19
20 match execute(cli.command) {
21 Ok(status) => process_exit_code(status),
22 Err(error) => {
23 eprintln!("✗ {error}");
24 ExitCode::FAILURE
25 }
26 }
27}
28
29fn execute(command: Commands) -> Result<ExitStatus, CliError> {
30 match command {
31 Commands::Run(args) => {
32 let options = RunOptions::from(args);
33
34 if let Err(error) = options.validate() {
35 Cli::command()
36 .error(ErrorKind::InvalidValue, error.to_string())
37 .exit();
38 }
39
40 let cwd = env::current_dir().map_err(CliError::CurrentDirectory)?;
41
42 let project = Project::discover(&cwd)?;
43
44 Ok(run::execute(&project, &options)?)
45 }
46 }
47}
48
49fn process_exit_code(status: ExitStatus) -> ExitCode {
50 status
51 .code()
52 .and_then(|code| u8::try_from(code).ok())
53 .map_or(ExitCode::FAILURE, ExitCode::from)
54}