From 7dba8bb49f57cd98a37973054bdf9e8184c4af84 Mon Sep 17 00:00:00 2001 From: losman0s Date: Sat, 11 Dec 2021 21:27:31 +0100 Subject: [PATCH] Add complete account dump to file This commit introduces the ability to dump the complete content of an account to a JSON file (compact or not depending on the provided format option). Example: ```sh solana account -u m \ --output json-compact \ --output-file SRM_token.json \ SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt ``` Note: Behavior remains untouched if format option `--output` is not provided (only account data gets written to file). (cherry picked from commit 0e9e67b65d95bebd1f8ecccf30bad942072dddf3) --- cli/src/wallet.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/cli/src/wallet.rs b/cli/src/wallet.rs index 0abcffccbc..8f53a0cd75 100644 --- a/cli/src/wallet.rs +++ b/cli/src/wallet.rs @@ -462,18 +462,27 @@ pub fn process_show_account( let mut account_string = config.output_format.formatted_string(&cli_account); - if config.output_format == OutputFormat::Display - || config.output_format == OutputFormat::DisplayVerbose - { - if let Some(output_file) = output_file { - let mut f = File::create(output_file)?; - f.write_all(&data)?; - writeln!(&mut account_string)?; - writeln!(&mut account_string, "Wrote account data to {}", output_file)?; - } else if !data.is_empty() { - use pretty_hex::*; - writeln!(&mut account_string, "{:?}", data.hex_dump())?; + match config.output_format { + OutputFormat::Json | OutputFormat::JsonCompact => { + if let Some(output_file) = output_file { + let mut f = File::create(output_file)?; + f.write_all(account_string.as_bytes())?; + writeln!(&mut account_string)?; + writeln!(&mut account_string, "Wrote account to {}", output_file)?; + } } + OutputFormat::Display | OutputFormat::DisplayVerbose => { + if let Some(output_file) = output_file { + let mut f = File::create(output_file)?; + f.write_all(&data)?; + writeln!(&mut account_string)?; + writeln!(&mut account_string, "Wrote account data to {}", output_file)?; + } else if !data.is_empty() { + use pretty_hex::*; + writeln!(&mut account_string, "{:?}", data.hex_dump())?; + } + } + OutputFormat::DisplayQuiet => (), } Ok(account_string)