Skip to main content

deslicer_cli/
cli.rs

1use clap::{Parser, Subcommand, ValueEnum};
2
3use crate::ci::CiPlatform;
4
5#[derive(Parser)]
6#[command(name = "deslicer", version, about)]
7pub struct Cli {
8    #[arg(
9        long,
10        env = "DESLICER_API_URL",
11        default_value = "https://api.deslicer.ai",
12        global = true
13    )]
14    pub deslicer_api_url: url::Url,
15
16    #[arg(long, env = "OBSERVER_API_URL", global = true)]
17    pub observer_api_url: Option<url::Url>,
18
19    #[arg(long, value_enum, default_value_t = CiPlatformArg::Auto, global = true)]
20    pub ci_platform: CiPlatformArg,
21
22    #[arg(long, value_enum, default_value_t = LogFormat::Human, global = true)]
23    pub log_format: LogFormat,
24
25    #[command(subcommand)]
26    pub command: Command,
27}
28
29#[derive(Subcommand)]
30pub enum Command {
31    #[command(subcommand)]
32    Auth(crate::commands::auth::AuthCmd),
33    #[command(subcommand)]
34    Change(crate::commands::change::ChangeCmd),
35}
36
37#[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq)]
38pub enum CiPlatformArg {
39    Auto,
40    Github,
41    Gitlab,
42    Azure,
43    Bitbucket,
44    Local,
45}
46
47impl CiPlatformArg {
48    pub fn as_override(self) -> Option<CiPlatform> {
49        match self {
50            CiPlatformArg::Auto => None,
51            CiPlatformArg::Github => Some(CiPlatform::Github),
52            CiPlatformArg::Gitlab => Some(CiPlatform::Gitlab),
53            CiPlatformArg::Azure => Some(CiPlatform::Azure),
54            CiPlatformArg::Bitbucket => Some(CiPlatform::Bitbucket),
55            CiPlatformArg::Local => Some(CiPlatform::Local),
56        }
57    }
58}
59
60#[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq)]
61pub enum LogFormat {
62    Human,
63    Json,
64}
65
66#[derive(Debug, Clone)]
67pub struct Ctx {
68    pub deslicer_api_url: url::Url,
69    pub observer_api_url: Option<url::Url>,
70    pub ci_override: Option<CiPlatform>,
71    pub log_format: LogFormat,
72}
73
74impl Cli {
75    pub async fn run(self) -> i32 {
76        let ctx = Ctx {
77            deslicer_api_url: self.deslicer_api_url,
78            observer_api_url: self.observer_api_url,
79            ci_override: self.ci_platform.as_override(),
80            log_format: self.log_format,
81        };
82        match self.command {
83            Command::Auth(cmd) => crate::commands::auth::dispatch(ctx, cmd).await,
84            Command::Change(cmd) => crate::commands::change::dispatch(ctx, cmd).await,
85        }
86    }
87}