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

Commit

Permalink
Add json output to show-account
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Apr 14, 2020
1 parent bbda0c8 commit 5ed7924
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
43 changes: 23 additions & 20 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
cli_output::OutputFormat,
cli_output::{CliAccount, OutputFormat},
cluster_query::*,
display::{println_name_value, println_signers},
nonce::{self, *},
Expand All @@ -22,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 @@ -1182,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
38 changes: 37 additions & 1 deletion cli/src/cli_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use console::style;
use inflector::cases::titlecase::to_title_case;
use serde::Serialize;
use serde_json::{Map, Value};
use solana_client::rpc_response::RpcEpochInfo;
use solana_client::rpc_response::{RpcEpochInfo, RpcKeyedAccount};
use solana_sdk::{
clock::{self, Epoch, Slot, UnixTimestamp},
stake_history::StakeHistoryEntry,
Expand Down Expand Up @@ -38,6 +38,42 @@ impl OutputFormat {
}
}

#[derive(Serialize, Deserialize)]
pub struct CliAccount {
#[serde(flatten)]
pub keyed_account: RpcKeyedAccount,
#[serde(skip_serializing)]
pub use_lamports_unit: bool,
}

impl fmt::Display for CliAccount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f)?;
writeln_name_value(f, "Public Key:", &self.keyed_account.pubkey)?;
writeln_name_value(
f,
"Balance:",
&build_balance_message(
self.keyed_account.account.lamports,
self.use_lamports_unit,
true,
),
)?;
writeln_name_value(f, "Owner:", &self.keyed_account.account.owner)?;
writeln_name_value(
f,
"Executable:",
&self.keyed_account.account.executable.to_string(),
)?;
writeln_name_value(
f,
"Rent Epoch:",
&self.keyed_account.account.rent_epoch.to_string(),
)?;
Ok(())
}
}

#[derive(Default, Serialize, Deserialize)]
pub struct CliBlockProduction {
pub epoch: Epoch,
Expand Down

0 comments on commit 5ed7924

Please sign in to comment.