1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use super::SchedulerRestClient;
use clap::Parser;
use serde::de::DeserializeOwned;
use thiserror::Error;

pub struct HealthCommand;

impl HealthCommand {
    pub fn exec(rest: SchedulerRestClient) -> Result<(), Error> {
        match rest.is_up() {
            true => println!("env is up"),
            false => println!("env is down"),
        }
        Ok(())
    }
}

#[derive(Parser, Debug)]
pub struct StatusCommand {
    /// job id
    #[clap(short, long)]
    job_id: String,
}

impl StatusCommand {
    pub fn exec<State: DeserializeOwned>(
        self,
        rest: SchedulerRestClient,
    ) -> Result<Result<State, crate::Error>, Error> {
        rest.job_status(self.job_id).map_err(Into::into)
    }
}

#[derive(Parser, Debug)]
pub enum FilesCommand {
    List,
}

impl FilesCommand {
    pub fn exec(self, rest: SchedulerRestClient) -> Result<(), Error> {
        match self {
            Self::List => {
                println!("{}", serde_json::to_string_pretty(&rest.list_files()?)?);
                Ok(())
            }
        }
    }
}

#[derive(Error, Debug)]
pub enum Error {
    #[error("internal rest error")]
    ReqwestError(#[from] reqwest::Error),
    #[error("response serialization error")]
    SerdeError(#[from] serde_json::Error),
    #[error("rest error")]
    RestError(#[from] super::rest::Error),
    #[error(transparent)]
    Scheduler(#[from] crate::Error),
}