cli: get command now shows default values instead of 'not set' (#5796)
* get command now shows default values instead of 'not set' * Add default indicator
This commit is contained in:
@@ -9,3 +9,16 @@ pub fn println_name_value(name: &str, value: &str) {
|
|||||||
};
|
};
|
||||||
println!("{} {}", style(name).bold(), styled_value);
|
println!("{} {}", style(name).bold(), styled_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn println_name_value_or(name: &str, value: &str, default_value: &str) {
|
||||||
|
if value == "" {
|
||||||
|
println!(
|
||||||
|
"{} {} {}",
|
||||||
|
style(name).bold(),
|
||||||
|
style(default_value),
|
||||||
|
style("(default)").italic()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
println!("{} {}", style(name).bold(), style(value));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@@ -2,7 +2,7 @@ use clap::{crate_description, crate_name, crate_version, Arg, ArgGroup, ArgMatch
|
|||||||
use console::style;
|
use console::style;
|
||||||
use solana_cli::{
|
use solana_cli::{
|
||||||
config::{self, Config},
|
config::{self, Config},
|
||||||
display::println_name_value,
|
display::{println_name_value, println_name_value_or},
|
||||||
input_validators::is_url,
|
input_validators::is_url,
|
||||||
wallet::{app, parse_command, process_command, WalletConfig, WalletError},
|
wallet::{app, parse_command, process_command, WalletConfig, WalletError},
|
||||||
};
|
};
|
||||||
@@ -13,18 +13,27 @@ fn parse_settings(matches: &ArgMatches<'_>) -> Result<bool, Box<dyn error::Error
|
|||||||
let parse_args = match matches.subcommand() {
|
let parse_args = match matches.subcommand() {
|
||||||
("get", Some(subcommand_matches)) => {
|
("get", Some(subcommand_matches)) => {
|
||||||
if let Some(config_file) = matches.value_of("config_file") {
|
if let Some(config_file) = matches.value_of("config_file") {
|
||||||
|
let default_wallet_config = WalletConfig::default();
|
||||||
let config = Config::load(config_file).unwrap_or_default();
|
let config = Config::load(config_file).unwrap_or_default();
|
||||||
if let Some(field) = subcommand_matches.value_of("specific_setting") {
|
if let Some(field) = subcommand_matches.value_of("specific_setting") {
|
||||||
let value = match field {
|
let (value, default_value) = match field {
|
||||||
"url" => config.url,
|
"url" => (config.url, default_wallet_config.json_rpc_url),
|
||||||
"keypair" => config.keypair,
|
"keypair" => (config.keypair, default_wallet_config.keypair_path),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
println_name_value(&format!("* {}:", field), &value);
|
println_name_value_or(&format!("* {}:", field), &value, &default_value);
|
||||||
} else {
|
} else {
|
||||||
println_name_value("Wallet Config:", config_file);
|
println_name_value("Wallet Config:", config_file);
|
||||||
println_name_value("* url:", &config.url);
|
println_name_value_or(
|
||||||
println_name_value("* keypair:", &config.keypair);
|
"* url:",
|
||||||
|
&config.url,
|
||||||
|
&default_wallet_config.json_rpc_url,
|
||||||
|
);
|
||||||
|
println_name_value_or(
|
||||||
|
"* keypair:",
|
||||||
|
&config.keypair,
|
||||||
|
&default_wallet_config.keypair_path,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!(
|
println!(
|
||||||
@@ -75,24 +84,22 @@ pub fn parse_args(matches: &ArgMatches<'_>) -> Result<WalletConfig, Box<dyn erro
|
|||||||
default.json_rpc_url
|
default.json_rpc_url
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut path = dirs::home_dir().expect("home directory");
|
let keypair_path = if matches.is_present("keypair") {
|
||||||
let id_path = if matches.is_present("keypair") {
|
matches.value_of("keypair").unwrap().to_string()
|
||||||
matches.value_of("keypair").unwrap()
|
|
||||||
} else if config.keypair != "" {
|
} else if config.keypair != "" {
|
||||||
&config.keypair
|
config.keypair
|
||||||
} else {
|
} else {
|
||||||
path.extend(&[".config", "solana", "id.json"]);
|
let default = WalletConfig::default();
|
||||||
if !path.exists() {
|
if !std::path::Path::new(&default.keypair_path).exists() {
|
||||||
gen_keypair_file(path.to_str().unwrap())?;
|
gen_keypair_file(&default.keypair_path)?;
|
||||||
println!("New keypair generated at: {}", path.to_str().unwrap());
|
println!("New keypair generated at: {}", default.keypair_path);
|
||||||
}
|
}
|
||||||
|
default.keypair_path
|
||||||
path.to_str().unwrap()
|
|
||||||
};
|
};
|
||||||
let keypair = read_keypair(id_path).or_else(|err| {
|
let keypair = read_keypair(&keypair_path).or_else(|err| {
|
||||||
Err(WalletError::BadParameter(format!(
|
Err(WalletError::BadParameter(format!(
|
||||||
"{}: Unable to open keypair file: {}",
|
"{}: Unable to open keypair file: {}",
|
||||||
err, id_path
|
err, keypair_path
|
||||||
)))
|
)))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@@ -102,7 +109,7 @@ pub fn parse_args(matches: &ArgMatches<'_>) -> Result<WalletConfig, Box<dyn erro
|
|||||||
command,
|
command,
|
||||||
json_rpc_url,
|
json_rpc_url,
|
||||||
keypair,
|
keypair,
|
||||||
keypair_path: id_path.to_string(),
|
keypair_path: keypair_path.to_string(),
|
||||||
rpc_client: None,
|
rpc_client: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -130,11 +130,14 @@ pub struct WalletConfig {
|
|||||||
|
|
||||||
impl Default for WalletConfig {
|
impl Default for WalletConfig {
|
||||||
fn default() -> WalletConfig {
|
fn default() -> WalletConfig {
|
||||||
|
let mut keypair_path = dirs::home_dir().expect("home directory");
|
||||||
|
keypair_path.extend(&[".config", "solana", "id.json"]);
|
||||||
|
|
||||||
WalletConfig {
|
WalletConfig {
|
||||||
command: WalletCommand::Balance(Pubkey::default()),
|
command: WalletCommand::Balance(Pubkey::default()),
|
||||||
json_rpc_url: "http://127.0.0.1:8899".to_string(),
|
json_rpc_url: "http://127.0.0.1:8899".to_string(),
|
||||||
keypair: Keypair::new(),
|
keypair: Keypair::new(),
|
||||||
keypair_path: "".to_string(),
|
keypair_path: keypair_path.to_str().unwrap().to_string(),
|
||||||
rpc_client: None,
|
rpc_client: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user