1use clap::{Parser, Subcommand};
2
3use crate::app_config::Environment;
4use crate::commands;
5use crate::commands::default_command;
6use crate::context::CliContext;
7use crate::tools::terminal::Terminal;
8
9#[derive(Parser, Debug)]
10#[clap(author, version, about, long_about = None)]
11pub struct CliArgs {
12 #[command(subcommand)]
13 pub command: Option<Commands>,
14
15 #[arg(long, action = clap::ArgAction::SetTrue, hide = true, conflicts_with = "staging")]
17 pub dev: bool,
18
19 #[arg(long, value_name = "VERSION", hide = true, conflicts_with = "dev")]
21 pub staging: Option<u8>,
22}
23
24#[derive(Subcommand, Debug)]
25pub enum Commands {
26 Train(commands::training::TrainingArgs),
28
29 Package(commands::package::PackageArgs),
31 Login(commands::login::LoginArgs),
33 Init(commands::init::InitArgs),
35 Unlink,
37 Me,
39 Project,
41}
42
43pub fn cli_main() {
44 let args = CliArgs::parse();
45
46 let environment = if args.dev {
47 Environment::Development
48 } else if let Some(version) = args.staging {
49 Environment::Staging(version)
50 } else {
51 Environment::Production
52 };
53
54 let terminal = Terminal::new();
55
56 if args.dev {
57 terminal
58 .print_warning("Running in development mode - using local server and dev credentials");
59 }
60
61 let context = CliContext::new(terminal.clone(), environment).init();
62
63 let cli_res = match args.command {
64 Some(command) => handle_command(command, context),
65 None => default_command(context),
66 };
67
68 if let Err(e) = cli_res {
69 terminal.cancel_finalize(&format!("{e}"));
70 }
71}
72
73fn handle_command(command: Commands, context: CliContext) -> anyhow::Result<()> {
74 match command {
75 Commands::Train(run_args) => commands::training::handle_command(run_args, context),
76 Commands::Package(package_args) => commands::package::handle_command(package_args, context),
77 Commands::Login(login_args) => commands::login::handle_command(login_args, context),
78 Commands::Init(init_args) => commands::init::handle_command(init_args, context),
79 Commands::Unlink => commands::unlink::handle_command(context),
80 Commands::Me => commands::me::handle_command(context),
81 Commands::Project => commands::project::handle_command(context),
82 }
83}