]>
Commit | Line | Data |
---|---|---|
f7d4e4b5 | 1 | use anyhow::{Error}; |
ca60ac13 | 2 | |
7eea56ca | 3 | use proxmox::api::{*, cli::*}; |
ca60ac13 | 4 | |
e8e9bae4 DM |
5 | #[api( |
6 | input: { | |
7 | properties: { | |
8 | text: { | |
9 | type: String, | |
10 | description: "Some text.", | |
11 | } | |
12 | } | |
13 | }, | |
14 | )] | |
15 | /// Echo command. Print the passed text. | |
16 | /// | |
17 | /// Returns: nothing | |
18 | fn echo_command( | |
19 | text: String, | |
20 | ) -> Result<(), Error> { | |
21 | println!("{}", text); | |
22 | Ok(()) | |
23 | } | |
24 | ||
19d7a592 DM |
25 | #[api( |
26 | input: { | |
27 | properties: { | |
28 | verbose: { | |
29 | type: Boolean, | |
30 | optional: true, | |
31 | description: "Verbose output.", | |
32 | } | |
33 | } | |
34 | }, | |
35 | )] | |
36 | /// Hello command. | |
37 | /// | |
38 | /// Returns: nothing | |
39 | fn hello_command( | |
40 | verbose: Option<bool>, | |
41 | ) -> Result<(), Error> { | |
42 | if verbose.unwrap_or(false) { | |
43 | println!("Hello, how are you!"); | |
44 | } else { | |
45 | println!("Hello!"); | |
46 | } | |
47 | ||
48 | Ok(()) | |
ca60ac13 DM |
49 | } |
50 | ||
19d7a592 | 51 | #[api(input: { properties: {} })] |
add5861e | 52 | /// Quit command. Exit the program. |
19d7a592 DM |
53 | /// |
54 | /// Returns: nothing | |
55 | fn quit_command() -> Result<(), Error> { | |
56 | ||
57 | println!("Goodbye."); | |
58 | ||
59 | std::process::exit(0); | |
60 | } | |
61 | ||
62 | fn cli_definition() -> CommandLineInterface { | |
ca60ac13 | 63 | let cmd_def = CliCommandMap::new() |
48ef3c33 DM |
64 | .insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND)) |
65 | .insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND)) | |
e8e9bae4 | 66 | .insert("echo", CliCommand::new(&API_METHOD_ECHO_COMMAND) |
48ef3c33 DM |
67 | .arg_param(&["text"]) |
68 | ) | |
2b691daf | 69 | .insert_help(); |
ca60ac13 | 70 | |
19d7a592 | 71 | CommandLineInterface::Nested(cmd_def) |
ca60ac13 DM |
72 | } |
73 | ||
ca60ac13 DM |
74 | fn main() -> Result<(), Error> { |
75 | ||
19d7a592 | 76 | let helper = CliHelper::new(cli_definition()); |
ca60ac13 | 77 | |
19d7a592 | 78 | let mut rl = rustyline::Editor::<CliHelper>::new(); |
ca60ac13 DM |
79 | rl.set_helper(Some(helper)); |
80 | ||
81 | while let Ok(line) = rl.readline("# prompt: ") { | |
82 | let helper = rl.helper().unwrap(); | |
ca60ac13 | 83 | |
2b691daf | 84 | let args = shellword_split(&line)?; |
1201abcf | 85 | |
7b22acd0 DM |
86 | let rpcenv = CliEnvironment::new(); |
87 | let _ = handle_command(helper.cmd_def(), "", args, rpcenv, None); | |
7afc1af7 DM |
88 | |
89 | rl.add_history_entry(line); | |
ca60ac13 DM |
90 | } |
91 | ||
92 | Ok(()) | |
93 | } |