]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/completion.rs
switch from failure to anyhow
[proxmox-backup.git] / src / bin / completion.rs
1 use anyhow::{Error};
2
3 use proxmox::api::{*, cli::*};
4
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
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(())
49 }
50
51 #[api(input: { properties: {} })]
52 /// Quit command. Exit the programm.
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 {
63 let cmd_def = CliCommandMap::new()
64 .insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND))
65 .insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND))
66 .insert("echo", CliCommand::new(&API_METHOD_ECHO_COMMAND)
67 .arg_param(&["text"])
68 )
69 .insert_help();
70
71 CommandLineInterface::Nested(cmd_def)
72 }
73
74 fn main() -> Result<(), Error> {
75
76 let helper = CliHelper::new(cli_definition());
77
78 let mut rl = rustyline::Editor::<CliHelper>::new();
79 rl.set_helper(Some(helper));
80
81 while let Ok(line) = rl.readline("# prompt: ") {
82 let helper = rl.helper().unwrap();
83
84 let args = shellword_split(&line)?;
85
86 let _ = handle_command(helper.cmd_def(), "", args, None);
87
88 rl.add_history_entry(line);
89 }
90
91 Ok(())
92 }