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

Commit

Permalink
Cli: enable json output (#9478) (#9495)
Browse files Browse the repository at this point in the history
automerge
  • Loading branch information
mergify[bot] authored Apr 14, 2020
1 parent 6765453 commit d7c43f0
Show file tree
Hide file tree
Showing 12 changed files with 1,137 additions and 419 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ctrlc = { version = "3.1.4", features = ["termination"] }
console = "0.10.0"
dirs = "2.0.2"
log = "0.4.8"
Inflector = "0.11.4"
indicatif = "0.14.0"
humantime = "2.0.0"
num-traits = "0.2"
Expand All @@ -41,7 +42,6 @@ solana-stake-program = { path = "../programs/stake", version = "1.1.4" }
solana-storage-program = { path = "../programs/storage", version = "1.1.4" }
solana-vote-program = { path = "../programs/vote", version = "1.1.4" }
solana-vote-signer = { path = "../vote-signer", version = "1.1.4" }
titlecase = "1.1.0"
thiserror = "1.0.13"
url = "2.1.1"

Expand Down
62 changes: 37 additions & 25 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
cli_output::{CliAccount, OutputFormat},
cluster_query::*,
display::{println_name_value, println_signers},
nonce::{self, *},
Expand All @@ -21,6 +22,7 @@ use solana_clap_utils::{
use solana_client::{
client_error::{ClientErrorKind, Result as ClientResult},
rpc_client::RpcClient,
rpc_response::{RpcAccount, RpcKeyedAccount},
};
#[cfg(not(test))]
use solana_faucet::faucet::request_airdrop_transaction;
Expand Down Expand Up @@ -472,6 +474,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 +566,7 @@ impl Default for CliConfig<'_> {
keypair_path: Self::default_keypair_path(),
rpc_client: None,
verbose: false,
output_format: OutputFormat::Display,
}
}
}
Expand Down Expand Up @@ -1179,31 +1183,33 @@ fn process_confirm(rpc_client: &RpcClient, signature: &Signature) -> ProcessResu

fn process_show_account(
rpc_client: &RpcClient,
_config: &CliConfig,
config: &CliConfig,
account_pubkey: &Pubkey,
output_file: &Option<String>,
use_lamports_unit: bool,
) -> ProcessResult {
let account = rpc_client.get_account(account_pubkey)?;
let data = account.data.clone();
let cli_account = CliAccount {
keyed_account: RpcKeyedAccount {
pubkey: account_pubkey.to_string(),
account: RpcAccount::encode(account),
},
use_lamports_unit,
};

println!();
println_name_value("Public Key:", &account_pubkey.to_string());
println_name_value(
"Balance:",
&build_balance_message(account.lamports, use_lamports_unit, true),
);
println_name_value("Owner:", &account.owner.to_string());
println_name_value("Executable:", &account.executable.to_string());
println_name_value("Rent Epoch:", &account.rent_epoch.to_string());

if let Some(output_file) = output_file {
let mut f = File::create(output_file)?;
f.write_all(&account.data)?;
println!();
println!("Wrote account data to {}", output_file);
} else if !account.data.is_empty() {
use pretty_hex::*;
println!("{:?}", account.data.hex_dump());
config.output_format.formatted_print(&cli_account);

if config.output_format == OutputFormat::Display {
if let Some(output_file) = output_file {
let mut f = File::create(output_file)?;
f.write_all(&data)?;
println!();
println!("Wrote account data to {}", output_file);
} else if !data.is_empty() {
use pretty_hex::*;
println!("{:?}", data.hex_dump());
}
}

Ok("".to_string())
Expand Down Expand Up @@ -1577,7 +1583,7 @@ fn process_witness(
}

pub fn process_command(config: &CliConfig) -> ProcessResult {
if config.verbose {
if config.verbose && config.output_format == OutputFormat::Display {
println_name_value("RPC URL:", &config.json_rpc_url);
println_name_value("Default Signer Path:", &config.keypair_path);
if config.keypair_path.starts_with("usb://") {
Expand Down Expand Up @@ -1622,7 +1628,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
CliCommand::GetBlockTime { slot } => process_get_block_time(&rpc_client, *slot),
CliCommand::GetGenesisHash => process_get_genesis_hash(&rpc_client),
CliCommand::GetEpochInfo { commitment_config } => {
process_get_epoch_info(&rpc_client, *commitment_config)
process_get_epoch_info(&rpc_client, config, *commitment_config)
}
CliCommand::GetEpoch { commitment_config } => {
process_get_epoch(&rpc_client, *commitment_config)
Expand Down Expand Up @@ -1662,13 +1668,14 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
vote_account_pubkeys,
} => process_show_stakes(
&rpc_client,
config,
*use_lamports_unit,
vote_account_pubkeys.as_deref(),
),
CliCommand::ShowValidators {
use_lamports_unit,
commitment_config,
} => process_show_validators(&rpc_client, *use_lamports_unit, *commitment_config),
} => process_show_validators(&rpc_client, config, *use_lamports_unit, *commitment_config),

// Nonce Commands

Expand Down Expand Up @@ -1711,7 +1718,12 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
CliCommand::ShowNonceAccount {
nonce_account_pubkey,
use_lamports_unit,
} => process_show_nonce_account(&rpc_client, &nonce_account_pubkey, *use_lamports_unit),
} => process_show_nonce_account(
&rpc_client,
config,
&nonce_account_pubkey,
*use_lamports_unit,
),
// Withdraw lamports from a nonce account
CliCommand::WithdrawFromNonceAccount {
nonce_account,
Expand Down Expand Up @@ -1940,7 +1952,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {

// Return all or single validator info
CliCommand::GetValidatorInfo(info_pubkey) => {
process_get_validator_info(&rpc_client, *info_pubkey)
process_get_validator_info(&rpc_client, config, *info_pubkey)
}
// Publish validator info
CliCommand::SetValidatorInfo {
Expand Down Expand Up @@ -2550,7 +2562,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
Loading

0 comments on commit d7c43f0

Please sign in to comment.