2022-04-10 15:44:34 +00:00
|
|
|
use anyhow::Error;
|
2019-11-29 10:58:01 +00:00
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_router::cli::*;
|
2022-04-10 15:44:34 +00:00
|
|
|
use proxmox_schema::*;
|
2019-11-29 10:58:01 +00:00
|
|
|
|
2019-12-02 10:04:40 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
text: {
|
|
|
|
type: String,
|
|
|
|
description: "Some text.",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Echo command. Print the passed text.
|
|
|
|
///
|
|
|
|
/// Returns: nothing
|
2022-04-10 15:44:34 +00:00
|
|
|
fn echo_command(text: String) -> Result<(), Error> {
|
2019-12-02 10:04:40 +00:00
|
|
|
println!("{}", text);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-12-02 09:57:19 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
verbose: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
description: "Verbose output.",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Hello command.
|
|
|
|
///
|
|
|
|
/// Returns: nothing
|
2022-04-10 15:44:34 +00:00
|
|
|
fn hello_command(verbose: Option<bool>) -> Result<(), Error> {
|
2019-12-02 09:57:19 +00:00
|
|
|
if verbose.unwrap_or(false) {
|
|
|
|
println!("Hello, how are you!");
|
|
|
|
} else {
|
|
|
|
println!("Hello!");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2019-11-29 10:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 09:57:19 +00:00
|
|
|
#[api(input: { properties: {} })]
|
2020-05-30 14:37:33 +00:00
|
|
|
/// Quit command. Exit the program.
|
2019-12-02 09:57:19 +00:00
|
|
|
///
|
|
|
|
/// Returns: nothing
|
|
|
|
fn quit_command() -> Result<(), Error> {
|
|
|
|
println!("Goodbye.");
|
|
|
|
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cli_definition() -> CommandLineInterface {
|
2019-11-29 10:58:01 +00:00
|
|
|
let cmd_def = CliCommandMap::new()
|
2019-12-09 16:40:34 +00:00
|
|
|
.insert("quit", CliCommand::new(&API_METHOD_QUIT_COMMAND))
|
|
|
|
.insert("hello", CliCommand::new(&API_METHOD_HELLO_COMMAND))
|
2022-04-10 15:44:34 +00:00
|
|
|
.insert(
|
|
|
|
"echo",
|
|
|
|
CliCommand::new(&API_METHOD_ECHO_COMMAND).arg_param(&["text"]),
|
2019-12-09 16:40:34 +00:00
|
|
|
)
|
2019-11-30 13:56:31 +00:00
|
|
|
.insert_help();
|
2019-11-29 10:58:01 +00:00
|
|
|
|
2019-12-02 09:57:19 +00:00
|
|
|
CommandLineInterface::Nested(cmd_def)
|
2019-11-29 10:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Error> {
|
2019-12-02 09:57:19 +00:00
|
|
|
let helper = CliHelper::new(cli_definition());
|
2019-11-29 10:58:01 +00:00
|
|
|
|
2019-12-02 09:57:19 +00:00
|
|
|
let mut rl = rustyline::Editor::<CliHelper>::new();
|
2019-11-29 10:58:01 +00:00
|
|
|
rl.set_helper(Some(helper));
|
|
|
|
|
|
|
|
while let Ok(line) = rl.readline("# prompt: ") {
|
|
|
|
let helper = rl.helper().unwrap();
|
|
|
|
|
2019-11-30 13:56:31 +00:00
|
|
|
let args = shellword_split(&line)?;
|
2019-11-30 11:57:02 +00:00
|
|
|
|
2020-05-06 05:51:05 +00:00
|
|
|
let rpcenv = CliEnvironment::new();
|
|
|
|
let _ = handle_command(helper.cmd_def(), "", args, rpcenv, None);
|
2019-11-30 14:08:37 +00:00
|
|
|
|
|
|
|
rl.add_history_entry(line);
|
2019-11-29 10:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|