Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Cli: enable json output #9478

Merged
merged 12 commits into from
Apr 14, 2020
5 changes: 4 additions & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
cli_output::OutputFormat,
cluster_query::*,
display::{println_name_value, println_signers},
nonce::{self, *},
Expand Down Expand Up @@ -472,6 +473,7 @@ pub struct CliConfig<'a> {
pub keypair_path: String,
pub rpc_client: Option<RpcClient>,
pub verbose: bool,
pub output_format: OutputFormat,
}

impl CliConfig<'_> {
Expand Down Expand Up @@ -563,6 +565,7 @@ impl Default for CliConfig<'_> {
keypair_path: Self::default_keypair_path(),
rpc_client: None,
verbose: false,
output_format: OutputFormat::Display,
}
}
}
Expand Down Expand Up @@ -2532,7 +2535,7 @@ pub fn app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> App<'ab, '
)
.arg(
Arg::with_name("output_file")
.long("output")
.long("output-file")
.short("o")
.value_name("FILEPATH")
.takes_value(true)
Expand Down
20 changes: 20 additions & 0 deletions cli/src/cli_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub enum OutputFormat {
Display,
Json,
}

impl OutputFormat {
pub fn formatted_print<T>(&self, item: &T)
where
T: Serialize + fmt::Display,
{
match self {
OutputFormat::Display => {
println!("{}", item);
}
OutputFormat::Json => {
println!("{}", serde_json::to_value(item).unwrap());
}
CriesofCarrots marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
4 changes: 4 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ macro_rules! pubkey {
};
}

#[macro_use]
extern crate serde_derive;

pub mod cli;
pub mod cli_output;
pub mod cluster_query;
pub mod display;
pub mod nonce;
Expand Down
15 changes: 15 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use solana_clap_utils::{
};
use solana_cli::{
cli::{app, parse_command, process_command, CliCommandInfo, CliConfig, CliSigners},
cli_output::OutputFormat,
display::{println_name_value, println_name_value_or},
};
use solana_cli_config::{Config, CONFIG_FILE};
Expand Down Expand Up @@ -129,6 +130,11 @@ pub fn parse_args<'a>(
let CliCommandInfo { command, signers } =
parse_command(&matches, &default_signer_path, wallet_manager.as_ref())?;

let output_format = matches
.value_of("output_format")
.map(|_| OutputFormat::Json)
.unwrap_or(OutputFormat::Display);

Ok((
CliConfig {
command,
Expand All @@ -138,6 +144,7 @@ pub fn parse_args<'a>(
keypair_path: default_signer_path,
rpc_client: None,
verbose: matches.is_present("verbose"),
output_format,
},
signers,
))
Expand Down Expand Up @@ -199,6 +206,14 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.global(true)
.help("Show additional information"),
)
.arg(
Arg::with_name("output_format")
.long("output")
.global(true)
.takes_value(true)
.possible_value("json")
.help("Return information in specified output format. Supports: json"),
)
.arg(
Arg::with_name(SKIP_SEED_PHRASE_VALIDATION_ARG.name)
.long(SKIP_SEED_PHRASE_VALIDATION_ARG.long)
Expand Down