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 Clean,
43}
44
45pub fn cli_main() {
46 let args = CliArgs::parse();
47
48 let environment = if args.dev {
49 Environment::Development
50 } else if let Some(version) = args.staging {
51 Environment::Staging(version)
52 } else {
53 Environment::Production
54 };
55
56 let terminal = Terminal::new();
57
58 if args.dev {
59 terminal
60 .print_warning("Running in development mode - using local server and dev credentials");
61 }
62
63 let context = CliContext::new(terminal.clone(), environment).init();
64
65 let cli_res = match args.command {
66 Some(command) => handle_command(command, context),
67 None => default_command(context),
68 };
69
70 if let Err(e) = cli_res {
71 terminal.cancel_finalize(&format!("{e}"));
72 }
73}
74
75fn handle_command(command: Commands, context: CliContext) -> anyhow::Result<()> {
76 match command {
77 Commands::Train(run_args) => commands::training::handle_command(run_args, context),
78 Commands::Package(package_args) => commands::package::handle_command(package_args, context),
79 Commands::Login(login_args) => commands::login::handle_command(login_args, context),
80 Commands::Init(init_args) => commands::init::handle_command(init_args, context),
81 Commands::Unlink => commands::unlink::handle_command(context),
82 Commands::Me => commands::me::handle_command(context),
83 Commands::Project => commands::project::handle_command(context),
84 Commands::Clean => commands::clean::handle_command(context),
85 }
86}