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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crate::client::rest::VitupAdminRestClient;
use crate::client::rest::VitupDisruptionRestClient;
use crate::client::rest::VitupRest;
use crate::config::Config;
use crate::Result;
use clap::Parser;
use std::path::PathBuf;
use thor::PersistentLogViewer;

#[derive(Parser, Debug)]
pub struct VitupClientCommand {
    #[clap(short, long, env = "VIT_TOKEN")]
    token: Option<String>,

    #[clap(short, long, env = "VIT_ENDPOINT")]
    endpoint: String,

    #[clap(subcommand)]
    command: Command,
}

impl VitupClientCommand {
    pub fn exec(self) -> Result<()> {
        if let Command::Utils(command) = self.command {
            return command.exec();
        }
        let endpoint = self.endpoint;
        let rest = match self.token {
            Some(token) => VitupRest::new_with_token(token, endpoint),
            None => VitupRest::new(endpoint),
        };

        match self.command {
            Command::Disruption(disruption_command) => disruption_command.exec(rest.into()),
            Command::Mock(mock_command) => mock_command.exec(rest.into()),
            _ => panic!("should not happen"),
        }
    }
}

#[derive(Parser, Debug)]
pub enum Command {
    /// disruption
    #[clap(subcommand)]
    Disruption(DisruptionCommand),
    /// mock
    #[clap(subcommand)]
    Mock(MockCommand),
    /// utils
    #[clap(subcommand)]
    Utils(UtilsCommand),
}

#[derive(Parser, Debug)]
pub enum DisruptionCommand {
    /// start backend from scratch
    #[clap(subcommand)]
    Logs(LogsCommand),
    /// start advanced backend from scratch
    #[clap(subcommand)]
    Files(FilesCommand),
    /// start mock env
    #[clap(subcommand)]
    Control(ControlCommand),
}

impl DisruptionCommand {
    pub fn exec(self, rest: VitupDisruptionRestClient) -> Result<()> {
        match self {
            Self::Logs(logs_command) => logs_command.exec(rest),
            Self::Files(files_command) => files_command.exec(rest),
            Self::Control(control_command) => control_command.exec(rest),
        }
    }
}

#[derive(Parser, Debug)]
pub enum LogsCommand {
    /// start backend from scratch
    Clear,
    /// start advanced backend from scratch
    Get,
}

impl LogsCommand {
    pub fn exec(self, rest: VitupDisruptionRestClient) -> Result<()> {
        match self {
            Self::Clear => rest.clear_logs().map_err(Into::into),
            Self::Get => {
                println!("{:?}", rest.get_logs());
                Ok(())
            }
        }
    }
}

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

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

#[derive(Parser, Debug)]
pub enum ControlCommand {
    Reset,
    SetUnavailable,
    SetErrorCode(SetErrorCodeCommand),
    SetAvailable,
    SetFundId(SetFundIdCommand),
    #[clap(subcommand)]
    Fragments(FragmentsCommand),
    Health,
}

impl ControlCommand {
    pub fn exec(self, rest: VitupDisruptionRestClient) -> Result<()> {
        match self {
            Self::Reset => rest.reset().map_err(Into::into),
            Self::SetUnavailable => rest.make_unavailable().map_err(Into::into),
            Self::SetErrorCode(set_error_code) => {
                rest.set_error_code(set_error_code.code).map_err(Into::into)
            }
            Self::SetAvailable => rest.make_available().map_err(Into::into),
            Self::SetFundId(set_fund_id) => {
                rest.set_fund_id(set_fund_id.fund_id).map_err(Into::into)
            }
            Self::Fragments(fragments_command) => fragments_command.exec(rest).map_err(Into::into),
            Self::Health => {
                match rest.is_up() {
                    true => {
                        println!("env is up");
                    }
                    false => {
                        println!("env is down");
                    }
                };
                Ok(())
            }
        }
    }
}

#[derive(Parser, Debug)]
pub struct SetFundIdCommand {
    #[clap(long = "fund-id")]
    fund_id: u32,
}

#[derive(Parser, Debug)]
pub struct SetErrorCodeCommand {
    #[clap(long = "code")]
    code: u16,
}

#[derive(Parser, Debug)]
pub enum FragmentsCommand {
    Reject,
    Hold,
    Accept,
    Reset,
}

impl FragmentsCommand {
    pub fn exec(self, rest: VitupDisruptionRestClient) -> Result<()> {
        match self {
            Self::Reject => rest.reject_all_fragments().map_err(Into::into),
            Self::Hold => rest.hold_all_fragments().map_err(Into::into),
            Self::Accept => rest.accept_all_fragments().map_err(Into::into),
            Self::Reset => rest.reset_fragments_behavior().map_err(Into::into),
        }
    }
}

#[derive(Parser, Debug)]
pub enum MockCommand {
    /// files commands
    #[clap(subcommand)]
    Files(MockFilesCommand),
    /// start commands
    #[clap(subcommand)]
    Start(MockStartCommand),
    /// stop command
    Stop,
    /// status command
    Status,
}

#[derive(Parser, Debug)]
pub enum MockStartCommand {
    /// start custom
    Custom(MockStartCustomCommand),
    /// start default
    Standard,
}

impl MockStartCommand {
    pub fn exec(self, rest: VitupAdminRestClient) -> Result<()> {
        match self {
            Self::Custom(custom_start) => {
                custom_start.exec(rest)?;
                Ok(())
            }
            Self::Standard => {
                println!("{}", rest.start_default()?);
                Ok(())
            }
        }
    }
}

#[derive(Parser, Debug)]
pub struct MockStartCustomCommand {
    #[clap(short = 'p', long = "params")]
    params: std::path::PathBuf,
}

impl MockStartCustomCommand {
    pub fn exec(self, rest: VitupAdminRestClient) -> Result<()> {
        let content = jortestkit::prelude::read_file(self.params)?;
        let params: Config = serde_json::from_str(&content)?;
        println!("{}", rest.start_custom(params)?);
        Ok(())
    }
}

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

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

impl MockCommand {
    pub fn exec(self, rest: VitupAdminRestClient) -> Result<()> {
        match self {
            Self::Files(files_command) => files_command.exec(rest),
            Self::Start(start_command) => {
                start_command.exec(rest)?;
                Ok(())
            }
            Self::Stop => {
                println!("{}", rest.stop()?);
                Ok(())
            }
            Self::Status => {
                println!("{}", rest.status()?);
                Ok(())
            }
        }
    }
}

#[derive(Parser, Debug)]
pub enum UtilsCommand {
    /// persistent log comamnds
    #[clap(subcommand)]
    PersistentLog(PersistentLogCommand),
}

impl UtilsCommand {
    pub fn exec(self) -> Result<()> {
        match self {
            Self::PersistentLog(persistent_logs_command) => persistent_logs_command.exec(),
        }
    }
}

#[derive(Parser, Debug)]
pub enum PersistentLogCommand {
    /// persistent log commands
    Count(CountPersistentLogCommand),
}

impl PersistentLogCommand {
    pub fn exec(self) -> Result<()> {
        match self {
            Self::Count(count_command) => count_command.exec(),
        }
    }
}

#[derive(Parser, Debug)]
pub struct CountPersistentLogCommand {
    /// count commands  
    #[clap(long = "folder")]
    pub folder: PathBuf,
}

impl CountPersistentLogCommand {
    pub fn exec(self) -> Result<()> {
        println!("{}", PersistentLogViewer::new(self.folder).count());
        Ok(())
    }
}