2018-12-10 12:36:52 +00:00
|
|
|
use failure::*;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use crate::api::schema::*;
|
|
|
|
use crate::api::router::*;
|
|
|
|
use crate::api::config::*;
|
|
|
|
use crate::getopts;
|
|
|
|
|
|
|
|
pub fn print_cli_usage() {
|
|
|
|
|
|
|
|
eprintln!("Usage: TODO");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_simple_command(cli_cmd: &CliCommand, args: Vec<String>) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let (params, rest) = getopts::parse_arguments(
|
|
|
|
&args, &cli_cmd.arg_param, &cli_cmd.info.parameters)?;
|
|
|
|
|
|
|
|
if !rest.is_empty() {
|
|
|
|
bail!("got additional arguments: {:?}", rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = (cli_cmd.info.handler)(params, &cli_cmd.info)?;
|
|
|
|
|
|
|
|
println!("Result: {}", serde_json::to_string_pretty(&res).unwrap());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
fn find_command<'a>(def: &'a CliCommandMap, name: &str) -> Option<&'a CommandLineInterface> {
|
2018-12-10 17:13:55 +00:00
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
if let Some(sub_cmd) = def.commands.get(name) {
|
2018-12-10 17:13:55 +00:00
|
|
|
return Some(sub_cmd);
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut matches: Vec<&str> = vec![];
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
for cmd in def.commands.keys() {
|
2018-12-10 17:13:55 +00:00
|
|
|
if cmd.starts_with(name) {
|
|
|
|
matches.push(cmd); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.len() != 1 { return None; }
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
if let Some(sub_cmd) = def.commands.get(matches[0]) {
|
2018-12-10 17:13:55 +00:00
|
|
|
return Some(sub_cmd);
|
|
|
|
};
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
fn handle_nested_command(def: &CliCommandMap, mut args: Vec<String>) -> Result<(), Error> {
|
2018-12-10 12:36:52 +00:00
|
|
|
|
|
|
|
if args.len() < 1 {
|
2018-12-11 10:31:36 +00:00
|
|
|
let mut cmds: Vec<&String> = def.commands.keys().collect();
|
2018-12-10 12:36:52 +00:00
|
|
|
cmds.sort();
|
|
|
|
|
|
|
|
let list = cmds.iter().fold(String::new(),|mut s,item| {
|
|
|
|
if !s.is_empty() { s+= ", "; }
|
|
|
|
s += item;
|
|
|
|
s
|
|
|
|
});
|
|
|
|
|
|
|
|
bail!("expected command argument, but no command specified.\nPossible commands: {}", list);
|
|
|
|
}
|
|
|
|
|
|
|
|
let command = args.remove(0);
|
|
|
|
|
2018-12-10 17:13:55 +00:00
|
|
|
let sub_cmd = match find_command(def, &command) {
|
2018-12-10 12:36:52 +00:00
|
|
|
Some(cmd) => cmd,
|
2018-12-10 17:13:55 +00:00
|
|
|
None => bail!("no such command '{}'", command),
|
2018-12-10 12:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match sub_cmd {
|
2018-12-10 12:40:10 +00:00
|
|
|
CommandLineInterface::Simple(cli_cmd) => {
|
2018-12-10 12:36:52 +00:00
|
|
|
handle_simple_command(cli_cmd, args)?;
|
|
|
|
}
|
2018-12-10 12:40:10 +00:00
|
|
|
CommandLineInterface::Nested(map) => {
|
2018-12-10 12:36:52 +00:00
|
|
|
handle_nested_command(map, args)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:40:10 +00:00
|
|
|
pub fn run_cli_command(def: &CommandLineInterface) -> Result<(), Error> {
|
2018-12-10 12:36:52 +00:00
|
|
|
|
|
|
|
let args: Vec<String> = std::env::args().skip(1).collect();
|
|
|
|
|
|
|
|
match def {
|
2018-12-10 12:40:10 +00:00
|
|
|
CommandLineInterface::Simple(cli_cmd) => handle_simple_command(cli_cmd, args),
|
|
|
|
CommandLineInterface::Nested(map) => handle_nested_command(map, args),
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CliCommand {
|
|
|
|
pub info: ApiMethod,
|
|
|
|
pub arg_param: Vec<&'static str>,
|
|
|
|
pub fixed_param: Vec<&'static str>,
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:12:13 +00:00
|
|
|
impl CliCommand {
|
|
|
|
|
|
|
|
pub fn new(info: ApiMethod) -> Self {
|
|
|
|
Self { info, arg_param: vec![], fixed_param: vec![] }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn arg_param(mut self, names: Vec<&'static str>) -> Self {
|
|
|
|
self.arg_param = names;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fixed_param(mut self, args: Vec<&'static str>) -> Self {
|
|
|
|
self.fixed_param = args;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
pub struct CliCommandMap {
|
|
|
|
pub commands: HashMap<String, CommandLineInterface>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CliCommandMap {
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { commands: HashMap:: new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert<S: Into<String>>(mut self, name: S, cli: CommandLineInterface) -> Self {
|
|
|
|
self.commands.insert(name.into(), cli);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:40:10 +00:00
|
|
|
pub enum CommandLineInterface {
|
2018-12-10 12:36:52 +00:00
|
|
|
Simple(CliCommand),
|
2018-12-11 10:31:36 +00:00
|
|
|
Nested(CliCommandMap),
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
2018-12-10 12:51:10 +00:00
|
|
|
|
|
|
|
impl From<CliCommand> for CommandLineInterface {
|
|
|
|
fn from(cli_cmd: CliCommand) -> Self {
|
|
|
|
CommandLineInterface::Simple(cli_cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
impl From<CliCommandMap> for CommandLineInterface {
|
|
|
|
fn from(list: CliCommandMap) -> Self {
|
|
|
|
CommandLineInterface::Nested(list)
|
2018-12-10 12:51:10 +00:00
|
|
|
}
|
|
|
|
}
|