bin/pbs-datastore.rs: start implementing cli commands
This commit is contained in:
		@ -6,7 +6,7 @@ use crate::api::schema::*;
 | 
			
		||||
use crate::api::router::*;
 | 
			
		||||
use serde_json::{json, Value};
 | 
			
		||||
 | 
			
		||||
mod config;
 | 
			
		||||
pub mod config;
 | 
			
		||||
mod version;
 | 
			
		||||
 | 
			
		||||
fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@ use crate::api::schema::*;
 | 
			
		||||
use crate::api::router::*;
 | 
			
		||||
use serde_json::{json};
 | 
			
		||||
 | 
			
		||||
mod datastore;
 | 
			
		||||
pub mod datastore;
 | 
			
		||||
 | 
			
		||||
pub fn router() -> Router {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -7,20 +7,40 @@ use serde_json::{json, Value};
 | 
			
		||||
 | 
			
		||||
use crate::config::datastore;
 | 
			
		||||
 | 
			
		||||
fn datastore_list(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
 | 
			
		||||
    println!("This is a test {}", param);
 | 
			
		||||
pub fn get() -> ApiMethod {
 | 
			
		||||
    ApiMethod::new(
 | 
			
		||||
        get_datastore_list,
 | 
			
		||||
        ObjectSchema::new("Directory index."))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn get_datastore_list(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    let config = datastore::config()?;
 | 
			
		||||
 | 
			
		||||
    Ok(config.convert_to_array("id"))
 | 
			
		||||
    Ok(config.convert_to_array("name"))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn post() -> ApiMethod {
 | 
			
		||||
    ApiMethod::new(
 | 
			
		||||
        create_datastore,
 | 
			
		||||
        ObjectSchema::new("Create new datastore.")
 | 
			
		||||
            .required("name", StringSchema::new("Datastore name."))
 | 
			
		||||
            .required("path", StringSchema::new("Directory path (must exist)."))
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn create_datastore(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
 | 
			
		||||
    println!("This is a test {}", param);
 | 
			
		||||
 | 
			
		||||
    Ok(json!({}))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn router() -> Router {
 | 
			
		||||
 | 
			
		||||
    let route = Router::new()
 | 
			
		||||
        .get(ApiMethod::new(
 | 
			
		||||
            datastore_list,
 | 
			
		||||
            ObjectSchema::new("Directory index.")));
 | 
			
		||||
        .get(get())
 | 
			
		||||
        .post(post());
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    route
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										81
									
								
								src/bin/pbs-datastore.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								src/bin/pbs-datastore.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,81 @@
 | 
			
		||||
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");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type CliCommandDefinition = HashMap<String, CliCommand>;
 | 
			
		||||
 | 
			
		||||
fn run_cli_command(def: &CliCommandDefinition) -> Result<(), Error> {
 | 
			
		||||
 | 
			
		||||
    let mut args: Vec<String> = std::env::args().skip(1).collect();
 | 
			
		||||
 | 
			
		||||
    if args.len() < 1 {
 | 
			
		||||
        bail!("no command specified.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let command = args.remove(0);
 | 
			
		||||
 | 
			
		||||
    let cli_cmd = match def.get(&command) {
 | 
			
		||||
        Some(cmd) => cmd,
 | 
			
		||||
        None => {
 | 
			
		||||
            bail!("no such command '{}'", command);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    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(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct CliCommand {
 | 
			
		||||
    info: ApiMethod,
 | 
			
		||||
    arg_param: Vec<&'static str>,
 | 
			
		||||
    fixed_param: Vec<&'static str>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
 | 
			
		||||
    let mut cmd_def = HashMap::new();
 | 
			
		||||
 | 
			
		||||
    cmd_def.insert("list".to_owned(), CliCommand {
 | 
			
		||||
        info: api3::config::datastore::get(),
 | 
			
		||||
        arg_param: vec![],
 | 
			
		||||
        fixed_param: vec![],
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    cmd_def.insert("create".to_owned(), CliCommand {
 | 
			
		||||
        info: api3::config::datastore::post(),
 | 
			
		||||
        arg_param: vec!["name", "path"],
 | 
			
		||||
        fixed_param: vec![],
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if let Err(err) = run_cli_command(&cmd_def) {
 | 
			
		||||
        eprintln!("Error: {}", err);
 | 
			
		||||
        print_cli_usage();
 | 
			
		||||
        std::process::exit(-1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user