src/cli/command.rs: add helpers/schema to format output
This commit is contained in:
parent
aad2ee4947
commit
25500cfa26
@ -1,6 +1,9 @@
|
|||||||
use failure::*;
|
use failure::*;
|
||||||
use std::collections::HashMap;
|
use lazy_static::lazy_static;
|
||||||
use std::collections::HashSet;
|
use std::sync::Arc;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
use crate::api_schema::*;
|
use crate::api_schema::*;
|
||||||
use crate::api_schema::router::*;
|
use crate::api_schema::router::*;
|
||||||
@ -10,6 +13,30 @@ use super::environment::CliEnvironment;
|
|||||||
|
|
||||||
use super::getopts;
|
use super::getopts;
|
||||||
|
|
||||||
|
lazy_static!{
|
||||||
|
|
||||||
|
pub static ref OUTPUT_FORMAT: Arc<Schema> =
|
||||||
|
StringSchema::new("Output format.")
|
||||||
|
.format(Arc::new(ApiStringFormat::Enum(&["text", "json", "json-pretty"])))
|
||||||
|
.into();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper function to format and print result
|
||||||
|
///
|
||||||
|
/// This is implemented for machine generatable formats 'json' and
|
||||||
|
/// 'json-pretty'. The 'text' format needs to be handled somewhere
|
||||||
|
/// else.
|
||||||
|
pub fn format_and_print_result(result: Value, output_format: &str) {
|
||||||
|
|
||||||
|
if output_format == "json-pretty" {
|
||||||
|
println!("{}", serde_json::to_string_pretty(&result).unwrap());
|
||||||
|
} else if output_format == "json" {
|
||||||
|
println!("{}", serde_json::to_string(&result).unwrap());
|
||||||
|
} else {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn generate_usage_str(
|
fn generate_usage_str(
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
@ -179,8 +206,10 @@ fn handle_simple_command(
|
|||||||
|
|
||||||
match (cli_cmd.info.handler.as_ref().unwrap())(params, &cli_cmd.info, &mut rpcenv) {
|
match (cli_cmd.info.handler.as_ref().unwrap())(params, &cli_cmd.info, &mut rpcenv) {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
|
if value != Value::Null {
|
||||||
println!("Result: {}", serde_json::to_string_pretty(&value).unwrap());
|
println!("Result: {}", serde_json::to_string_pretty(&value).unwrap());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {}", err);
|
eprintln!("Error: {}", err);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user