Skip to main content

deslicer_cli/
cli.rs

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