Impl OutputFormat for solana program (#14911)

This commit is contained in:
Tyera Eulberg
2021-02-01 10:39:37 -07:00
committed by GitHub
parent 49c908dc50
commit 52f0de3207
4 changed files with 104 additions and 35 deletions

View File

@ -1466,6 +1466,60 @@ impl fmt::Display for CliTokenAccount {
} }
} }
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliProgramId {
pub program_id: String,
}
impl QuietDisplay for CliProgramId {}
impl VerboseDisplay for CliProgramId {}
impl fmt::Display for CliProgramId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln_name_value(f, "Program Id:", &self.program_id)
}
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliProgramBuffer {
pub buffer: String,
}
impl QuietDisplay for CliProgramBuffer {}
impl VerboseDisplay for CliProgramBuffer {}
impl fmt::Display for CliProgramBuffer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln_name_value(f, "Buffer:", &self.buffer)
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CliProgramAccountType {
Buffer,
Program,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CliProgramAuthority {
pub authority: String,
pub account_type: CliProgramAccountType,
}
impl QuietDisplay for CliProgramAuthority {}
impl VerboseDisplay for CliProgramAuthority {}
impl fmt::Display for CliProgramAuthority {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln_name_value(f, "Account Type:", &format!("{:?}", self.account_type))?;
writeln_name_value(f, "Authority:", &self.authority)
}
}
pub fn return_signers( pub fn return_signers(
tx: &Transaction, tx: &Transaction,
output_format: &OutputFormat, output_format: &OutputFormat,

View File

@ -2724,6 +2724,7 @@ mod tests {
use_deprecated_loader: false, use_deprecated_loader: false,
allow_excessive_balance: false, allow_excessive_balance: false,
}; };
config.output_format = OutputFormat::JsonCompact;
let result = process_command(&config); let result = process_command(&config);
let json: Value = serde_json::from_str(&result.unwrap()).unwrap(); let json: Value = serde_json::from_str(&result.unwrap()).unwrap();
let program_id = json let program_id = json

View File

@ -10,10 +10,12 @@ use bincode::serialize;
use bip39::{Language, Mnemonic, MnemonicType, Seed}; use bip39::{Language, Mnemonic, MnemonicType, Seed};
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand}; use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use log::*; use log::*;
use serde_json::{self, json, Value};
use solana_bpf_loader_program::{bpf_verifier, BPFError, ThisInstructionMeter}; use solana_bpf_loader_program::{bpf_verifier, BPFError, ThisInstructionMeter};
use solana_clap_utils::{self, input_parsers::*, input_validators::*, keypair::*}; use solana_clap_utils::{self, input_parsers::*, input_validators::*, keypair::*};
use solana_cli_output::display::new_spinner_progress_bar; use solana_cli_output::{
display::new_spinner_progress_bar, CliProgramAccountType, CliProgramAuthority,
CliProgramBuffer, CliProgramId,
};
use solana_client::{ use solana_client::{
rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig, rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig,
rpc_request::MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS, rpc_response::RpcLeaderSchedule, rpc_request::MAX_GET_SIGNATURE_STATUSES_QUERY_ITEMS, rpc_response::RpcLeaderSchedule,
@ -863,7 +865,17 @@ fn process_set_authority(
) )
.map_err(|e| format!("Setting authority failed: {}", e))?; .map_err(|e| format!("Setting authority failed: {}", e))?;
Ok(option_pubkey_to_string("authority", new_authority).to_string()) let authority = CliProgramAuthority {
authority: new_authority
.map(|pubkey| pubkey.to_string())
.unwrap_or_else(|| "none".to_string()),
account_type: if program_pubkey.is_some() {
CliProgramAccountType::Program
} else {
CliProgramAccountType::Buffer
},
};
Ok(config.output_format.formatted_string(&authority))
} }
fn process_get_authority( fn process_get_authority(
@ -889,11 +901,13 @@ fn process_get_authority(
.. ..
}) = account.state() }) = account.state()
{ {
let mut value = let authority = CliProgramAuthority {
option_pubkey_to_string("authority", upgrade_authority_address); authority: upgrade_authority_address
let map = value.as_object_mut().unwrap(); .map(|pubkey| pubkey.to_string())
map.insert("accountType".to_string(), json!("program".to_string())); .unwrap_or_else(|| "none".to_string()),
Ok(value.to_string()) account_type: CliProgramAccountType::Program,
};
Ok(config.output_format.formatted_string(&authority))
} else { } else {
Err("Invalid associated ProgramData account found for the program".into()) Err("Invalid associated ProgramData account found for the program".into())
} }
@ -905,10 +919,13 @@ fn process_get_authority(
} }
} else if let Ok(UpgradeableLoaderState::Buffer { authority_address }) = account.state() } else if let Ok(UpgradeableLoaderState::Buffer { authority_address }) = account.state()
{ {
let mut value = option_pubkey_to_string("authority", authority_address); let authority = CliProgramAuthority {
let map = value.as_object_mut().unwrap(); authority: authority_address
map.insert("accountType".to_string(), json!("buffer".to_string())); .map(|pubkey| pubkey.to_string())
Ok(value.to_string()) .unwrap_or_else(|| "none".to_string()),
account_type: CliProgramAccountType::Buffer,
};
Ok(config.output_format.formatted_string(&authority))
} else { } else {
Err("Not a buffer or program account".into()) Err("Not a buffer or program account".into())
} }
@ -1117,15 +1134,15 @@ fn do_process_program_write_and_deploy(
)?; )?;
if let Some(program_signers) = program_signers { if let Some(program_signers) = program_signers {
Ok(json!({ let program_id = CliProgramId {
"programId": format!("{}", program_signers[0].pubkey()), program_id: program_signers[0].pubkey().to_string(),
}) };
.to_string()) Ok(config.output_format.formatted_string(&program_id))
} else { } else {
Ok(json!({ let buffer = CliProgramBuffer {
"buffer": format!("{}", buffer_pubkey), buffer: buffer_pubkey.to_string(),
}) };
.to_string()) Ok(config.output_format.formatted_string(&buffer))
} }
} }
@ -1238,10 +1255,10 @@ fn do_process_program_upgrade(
Some(&[upgrade_authority]), Some(&[upgrade_authority]),
)?; )?;
Ok(json!({ let program_id = CliProgramId {
"programId": format!("{}", program_id), program_id: program_id.to_string(),
}) };
.to_string()) Ok(config.output_format.formatted_string(&program_id))
} }
fn read_and_verify_elf(program_location: &str) -> Result<Vec<u8>, Box<dyn std::error::Error>> { fn read_and_verify_elf(program_location: &str) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
@ -1442,17 +1459,6 @@ fn report_ephemeral_mnemonic(words: usize, mnemonic: bip39::Mnemonic) {
); );
} }
fn option_pubkey_to_string(tag: &str, option: Option<Pubkey>) -> Value {
match option {
Some(pubkey) => json!({
tag: format!("{:?}", pubkey),
}),
None => json!({
tag: "none",
}),
}
}
fn send_and_confirm_transactions_with_spinner<T: Signers>( fn send_and_confirm_transactions_with_spinner<T: Signers>(
rpc_client: &RpcClient, rpc_client: &RpcClient,
mut transactions: Vec<Transaction>, mut transactions: Vec<Transaction>,
@ -1623,6 +1629,7 @@ mod tests {
use super::*; use super::*;
use crate::cli::{app, parse_command, process_command}; use crate::cli::{app, parse_command, process_command};
use serde_json::Value; use serde_json::Value;
use solana_cli_output::OutputFormat;
use solana_sdk::signature::write_keypair_file; use solana_sdk::signature::write_keypair_file;
fn make_tmp_path(name: &str) -> String { fn make_tmp_path(name: &str) -> String {
@ -2240,6 +2247,7 @@ mod tests {
allow_excessive_balance: false, allow_excessive_balance: false,
}), }),
signers: vec![&default_keypair], signers: vec![&default_keypair],
output_format: OutputFormat::JsonCompact,
..CliConfig::default() ..CliConfig::default()
}; };

View File

@ -3,6 +3,7 @@ use solana_cli::{
cli::{process_command, CliCommand, CliConfig}, cli::{process_command, CliCommand, CliConfig},
program::ProgramCliCommand, program::ProgramCliCommand,
}; };
use solana_cli_output::OutputFormat;
use solana_client::rpc_client::RpcClient; use solana_client::rpc_client::RpcClient;
use solana_core::test_validator::TestValidator; use solana_core::test_validator::TestValidator;
use solana_faucet::faucet::run_local_faucet; use solana_faucet::faucet::run_local_faucet;
@ -58,6 +59,7 @@ fn test_cli_program_deploy_non_upgradeable() {
use_deprecated_loader: false, use_deprecated_loader: false,
allow_excessive_balance: false, allow_excessive_balance: false,
}; };
config.output_format = OutputFormat::JsonCompact;
let response = process_command(&config); let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap(); let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json let program_id_str = json
@ -190,6 +192,7 @@ fn test_cli_program_deploy_no_authority() {
is_final: true, is_final: true,
max_len: None, max_len: None,
}); });
config.output_format = OutputFormat::JsonCompact;
let response = process_command(&config); let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap(); let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_id_str = json let program_id_str = json
@ -274,6 +277,7 @@ fn test_cli_program_deploy_with_authority() {
is_final: false, is_final: false,
max_len: Some(max_len), max_len: Some(max_len),
}); });
config.output_format = OutputFormat::JsonCompact;
let response = process_command(&config); let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap(); let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let program_pubkey_str = json let program_pubkey_str = json
@ -592,6 +596,7 @@ fn test_cli_program_write_buffer() {
buffer_authority_signer_index: None, buffer_authority_signer_index: None,
max_len: None, max_len: None,
}); });
config.output_format = OutputFormat::JsonCompact;
let response = process_command(&config); let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap(); let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let buffer_pubkey_str = json let buffer_pubkey_str = json
@ -826,6 +831,7 @@ fn test_cli_program_set_buffer_authority() {
buffer_authority_index: Some(0), buffer_authority_index: Some(0),
new_buffer_authority: new_buffer_authority.pubkey(), new_buffer_authority: new_buffer_authority.pubkey(),
}); });
config.output_format = OutputFormat::JsonCompact;
let response = process_command(&config); let response = process_command(&config);
let json: Value = serde_json::from_str(&response.unwrap()).unwrap(); let json: Value = serde_json::from_str(&response.unwrap()).unwrap();
let new_buffer_authority_str = json let new_buffer_authority_str = json