1
use server::command::{Argument, CmdError, CmdResult, config::SelectColumn};
2
use std::collections::HashMap;
3
use std::fmt::Debug;
4
use std::future::Future;
5
use std::pin::Pin;
6
use thiserror::Error;
7

            
8
pub trait CliRunnable: Debug + Send {
9
    fn run<'a>(
10
        &'a self,
11
        args: &'a HashMap<&str, &Argument>,
12
    ) -> Pin<Box<dyn Future<Output = Result<Option<CmdResult>, CommandError>> + Send + 'a>>;
13
}
14

            
15
#[derive(Debug)]
16
pub struct CommandNode {
17
    pub name: String,
18
    pub is_leaf: bool,
19
    pub subcommands: Vec<CommandNode>,
20
}
21

            
22
#[derive(Debug, Error)]
23
pub enum CommandError {
24
    #[error("{0}")]
25
    Command(String),
26
    #[error("Arguments error: {0}")]
27
    Argument(String),
28
    #[error("Execution: {0}")]
29
    Execution(#[from] CmdError),
30
    #[error("{}", .0.join("\n"))]
31
    FormInvalid(Vec<String>),
32
}
33

            
34
pub trait CliCommand: Debug + Send {
35
    fn node() -> CommandNode;
36
}
37

            
38
#[derive(Debug)]
39
pub struct CliGetConfig;
40

            
41
impl CliCommand for CliGetConfig {
42
2738
    fn node() -> CommandNode {
43
2738
        CommandNode {
44
2738
            name: "get".to_string(),
45
2738
            is_leaf: true,
46
2738
            subcommands: vec![],
47
2738
        }
48
2738
    }
49
}
50

            
51
#[derive(Debug)]
52
pub struct CliSetConfig;
53

            
54
impl CliCommand for CliSetConfig {
55
2738
    fn node() -> CommandNode {
56
2738
        CommandNode {
57
2738
            name: "set".to_string(),
58
2738
            is_leaf: true,
59
2738
            subcommands: vec![],
60
2738
        }
61
2738
    }
62
}
63

            
64
#[derive(Debug)]
65
pub struct CliVersion;
66

            
67
impl CliCommand for CliVersion {
68
2738
    fn node() -> CommandNode {
69
2738
        CommandNode {
70
2738
            name: "version".to_string(),
71
2738
            is_leaf: true,
72
2738
            subcommands: vec![],
73
2738
        }
74
2738
    }
75
}
76

            
77
#[derive(Debug)]
78
pub struct CliSelectColumn;
79

            
80
impl CliRunnable for CliSelectColumn {
81
1
    fn run<'a>(
82
1
        &'a self,
83
1
        args: &'a HashMap<&str, &Argument>,
84
1
    ) -> Pin<Box<dyn Future<Output = Result<Option<CmdResult>, CommandError>> + Send + 'a>> {
85
1
        Box::pin(async move {
86
1
            match (args.get("field"), args.get("table")) {
87
                (Some(Argument::String(field)), Some(Argument::String(table))) => {
88
                    Ok(SelectColumn::new()
89
                        .field(field.clone())
90
                        .table(table.clone())
91
                        .run()
92
                        .await?)
93
                }
94
1
                _ => Err(CommandError::Argument(
95
1
                    "No column or table provided".to_string(),
96
1
                )),
97
            }
98
1
        })
99
1
    }
100
}
101

            
102
#[derive(Debug)]
103
pub struct CliCommodityCreate;
104

            
105
impl CliCommand for CliCommodityCreate {
106
2738
    fn node() -> CommandNode {
107
2738
        CommandNode {
108
2738
            name: "create".to_string(),
109
2738
            is_leaf: true,
110
2738
            subcommands: vec![],
111
2738
        }
112
2738
    }
113
}
114

            
115
#[derive(Debug)]
116
pub struct CliCommodityList;
117

            
118
impl CliCommand for CliCommodityList {
119
2738
    fn node() -> CommandNode {
120
2738
        CommandNode {
121
2738
            name: "list".to_string(),
122
2738
            is_leaf: true,
123
2738
            subcommands: vec![],
124
2738
        }
125
2738
    }
126
}
127

            
128
#[derive(Debug)]
129
pub struct CliAccountCreate;
130

            
131
impl CliCommand for CliAccountCreate {
132
2738
    fn node() -> CommandNode {
133
2738
        CommandNode {
134
2738
            name: "create".to_string(),
135
2738
            is_leaf: true,
136
2738
            subcommands: vec![],
137
2738
        }
138
2738
    }
139
}
140

            
141
#[derive(Debug)]
142
pub struct CliAccountList;
143

            
144
impl CliCommand for CliAccountList {
145
2738
    fn node() -> CommandNode {
146
2738
        CommandNode {
147
2738
            name: "list".to_string(),
148
2738
            is_leaf: true,
149
2738
            subcommands: vec![],
150
2738
        }
151
2738
    }
152
}
153

            
154
#[derive(Debug)]
155
pub struct CliTransactionCreate;
156

            
157
impl CliCommand for CliTransactionCreate {
158
2738
    fn node() -> CommandNode {
159
2738
        CommandNode {
160
2738
            name: "create".to_string(),
161
2738
            is_leaf: true,
162
2738
            subcommands: vec![],
163
2738
        }
164
2738
    }
165
}
166

            
167
#[derive(Debug)]
168
pub struct CliTransactionList;
169

            
170
impl CliCommand for CliTransactionList {
171
2738
    fn node() -> CommandNode {
172
2738
        CommandNode {
173
2738
            name: "list".to_string(),
174
2738
            is_leaf: true,
175
2738
            subcommands: vec![],
176
2738
        }
177
2738
    }
178
}
179

            
180
#[derive(Debug)]
181
pub struct CliAccountBalance;
182

            
183
impl CliCommand for CliAccountBalance {
184
2738
    fn node() -> CommandNode {
185
2738
        CommandNode {
186
2738
            name: "balance".to_string(),
187
2738
            is_leaf: true,
188
2738
            subcommands: vec![],
189
2738
        }
190
2738
    }
191
}
192

            
193
#[derive(Debug)]
194
pub struct CliListDataFindings;
195

            
196
impl CliRunnable for CliListDataFindings {
197
    fn run<'a>(
198
        &'a self,
199
        args: &'a HashMap<&str, &Argument>,
200
    ) -> Pin<Box<dyn Future<Output = Result<Option<CmdResult>, CommandError>> + Send + 'a>> {
201
        Box::pin(async move {
202
            let mut cmd = server::command::data_findings::ListDataFindings::new();
203
            if let Some(Argument::String(kind)) = args.get("kind") {
204
                cmd = cmd.kind(kind.clone());
205
            }
206
            Ok(cmd.run().await?)
207
        })
208
    }
209
}
210

            
211
#[derive(Debug)]
212
pub struct CliSshKeyRemove;
213

            
214
impl CliCommand for CliSshKeyRemove {
215
2738
    fn node() -> CommandNode {
216
2738
        CommandNode {
217
2738
            name: "remove".to_string(),
218
2738
            is_leaf: true,
219
2738
            subcommands: vec![],
220
2738
        }
221
2738
    }
222
}
223

            
224
#[cfg(test)]
225
mod tests {
226
    use super::*;
227

            
228
1
    fn block_on<F: Future>(f: F) -> F::Output {
229
1
        tokio::runtime::Builder::new_current_thread()
230
1
            .enable_all()
231
1
            .build()
232
1
            .expect("runtime")
233
1
            .block_on(f)
234
1
    }
235

            
236
    #[test]
237
1
    fn select_column_rejects_missing_table() {
238
1
        let field = Argument::String("foo".to_string());
239
1
        let mut args: HashMap<&str, &Argument> = HashMap::new();
240
1
        args.insert("field", &field);
241
1
        let err = block_on(CliSelectColumn.run(&args)).expect_err("missing table should error");
242
1
        assert!(matches!(err, CommandError::Argument(_)));
243
1
    }
244
}