diff --git a/src/bin/pbs-datastore.rs b/src/bin/pbs-datastore.rs index ce8b3ba8..b1f1960d 100644 --- a/src/bin/pbs-datastore.rs +++ b/src/bin/pbs-datastore.rs @@ -1,95 +1,9 @@ extern crate apitest; -use failure::*; use std::collections::HashMap; -//use std::sync::Arc; - -use apitest::api::schema::*; -use apitest::api::router::*; -use apitest::api::config::*; -use apitest::getopts; - use apitest::api3; - -fn print_cli_usage() { - - eprintln!("Usage: TODO"); -} - - -fn handle_simple_command(cli_cmd: &CliCommand, args: Vec) -> 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(()) -} - -fn handle_nested_command(def: &HashMap, mut args: Vec) -> Result<(), Error> { - - if args.len() < 1 { - let mut cmds: Vec<&String> = def.keys().collect(); - 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); - - let sub_cmd = match def.get(&command) { - Some(cmd) => cmd, - None => { - bail!("no such command '{}'", command); - } - }; - - match sub_cmd { - CmdDef::Simple(cli_cmd) => { - handle_simple_command(cli_cmd, args)?; - } - CmdDef::Nested(map) => { - handle_nested_command(map, args)?; - } - } - - Ok(()) -} - -fn run_cli_command(def: &CmdDef) -> Result<(), Error> { - - let args: Vec = std::env::args().skip(1).collect(); - - match def { - CmdDef::Simple(cli_cmd) => handle_simple_command(cli_cmd, args), - CmdDef::Nested(map) => handle_nested_command(map, args), - } -} - -struct CliCommand { - info: ApiMethod, - arg_param: Vec<&'static str>, - fixed_param: Vec<&'static str>, -} - -enum CmdDef { - Simple(CliCommand), - Nested(HashMap), -} +use apitest::cli::command::*; fn datastore_commands() -> CmdDef { diff --git a/src/cli/command.rs b/src/cli/command.rs new file mode 100644 index 00000000..f817d6fd --- /dev/null +++ b/src/cli/command.rs @@ -0,0 +1,85 @@ +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) -> 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(()) +} + +fn handle_nested_command(def: &HashMap, mut args: Vec) -> Result<(), Error> { + + if args.len() < 1 { + let mut cmds: Vec<&String> = def.keys().collect(); + 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); + + let sub_cmd = match def.get(&command) { + Some(cmd) => cmd, + None => { + bail!("no such command '{}'", command); + } + }; + + match sub_cmd { + CmdDef::Simple(cli_cmd) => { + handle_simple_command(cli_cmd, args)?; + } + CmdDef::Nested(map) => { + handle_nested_command(map, args)?; + } + } + + Ok(()) +} + +pub fn run_cli_command(def: &CmdDef) -> Result<(), Error> { + + let args: Vec = std::env::args().skip(1).collect(); + + match def { + CmdDef::Simple(cli_cmd) => handle_simple_command(cli_cmd, args), + CmdDef::Nested(map) => handle_nested_command(map, args), + } +} + +pub struct CliCommand { + pub info: ApiMethod, + pub arg_param: Vec<&'static str>, + pub fixed_param: Vec<&'static str>, +} + +pub enum CmdDef { + Simple(CliCommand), + Nested(HashMap), +} diff --git a/src/lib.rs b/src/lib.rs index 4731de06..808c988e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,4 +50,10 @@ pub mod storage { pub mod getopts; +pub mod cli { + + pub mod command; +} + + pub mod api3;