api: pass RpcEnvirnment to api handlers

This commit is contained in:
Dietmar Maurer
2019-01-26 14:50:37 +01:00
parent d96d82736d
commit 6049b71f41
18 changed files with 252 additions and 109 deletions

View File

@ -9,6 +9,28 @@ use crate::api::router::*;
//use crate::api::config::*;
use crate::getopts;
struct CliEnvironment {
result_attributes: HashMap<String, Value>,
}
impl CliEnvironment {
fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for CliEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}
pub fn print_cli_usage() {
eprintln!("Usage: TODO");
@ -260,10 +282,11 @@ pub fn run_cli_command(def: &CommandLineInterface) -> Result<(), Error> {
CommandLineInterface::Nested(map) => handle_nested_command(map, args),
};
let mut rpcenv = CliEnvironment::new();
let res = match invocation {
Err(e) => return Err(UsageError(e).into()),
Ok(invocation) => (invocation.0.info.handler)(invocation.1, &invocation.0.info)?,
Ok(invocation) => (invocation.0.info.handler)(invocation.1, &invocation.0.info, &mut rpcenv)?,
};
println!("Result: {}", serde_json::to_string_pretty(&res).unwrap());