Improve missing default filepath signer error messaging

This commit is contained in:
Trent Nelson 2021-05-28 01:42:55 -06:00 committed by Trent Nelson
parent ca8c1c6c42
commit 06a926f2f4
8 changed files with 60 additions and 58 deletions

View File

@ -24,9 +24,11 @@ use {
}, },
}, },
std::{ std::{
cell::RefCell,
convert::TryFrom, convert::TryFrom,
error, error,
io::{stdin, stdout, Write}, io::{stdin, stdout, Write},
ops::Deref,
process::exit, process::exit,
str::FromStr, str::FromStr,
sync::Arc, sync::Arc,
@ -90,12 +92,48 @@ impl CliSignerInfo {
} }
} }
#[derive(Debug, Default)]
pub struct DefaultSigner { pub struct DefaultSigner {
pub arg_name: String, pub arg_name: String,
pub path: String, pub path: String,
is_path_checked: RefCell<bool>,
} }
impl DefaultSigner { impl DefaultSigner {
pub fn new<AN: AsRef<str>, P: AsRef<str>>(arg_name: AN, path: P) -> Self {
let arg_name = arg_name.as_ref().to_string();
let path = path.as_ref().to_string();
Self {
arg_name,
path,
..Self::default()
}
}
fn path(&self) -> Result<&str, Box<dyn std::error::Error>> {
if !self.is_path_checked.borrow().deref() {
parse_signer_source(&self.path)
.and_then(|s| {
if let SignerSourceKind::Filepath(path) = &s.kind {
std::fs::metadata(path).map(|_| ()).map_err(|e| e.into())
} else {
Ok(())
}
})
.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"No default signer found, run \"solana-keygen new -o {}\" to create a new one",
self.path
),
)
})?;
*self.is_path_checked.borrow_mut() = true;
}
Ok(&self.path)
}
pub fn generate_unique_signers( pub fn generate_unique_signers(
&self, &self,
bulk_signers: Vec<Option<Box<dyn Signer>>>, bulk_signers: Vec<Option<Box<dyn Signer>>>,
@ -125,7 +163,7 @@ impl DefaultSigner {
matches: &ArgMatches, matches: &ArgMatches,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>, wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
signer_from_path(matches, &self.path, &self.arg_name, wallet_manager) signer_from_path(matches, self.path()?, &self.arg_name, wallet_manager)
} }
pub fn signer_from_path_with_config( pub fn signer_from_path_with_config(
@ -134,7 +172,13 @@ impl DefaultSigner {
wallet_manager: &mut Option<Arc<RemoteWalletManager>>, wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
config: &SignerFromPathConfig, config: &SignerFromPathConfig,
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> { ) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
signer_from_path_with_config(matches, &self.path, &self.arg_name, wallet_manager, config) signer_from_path_with_config(
matches,
self.path()?,
&self.arg_name,
wallet_manager,
config,
)
} }
} }

View File

@ -2299,10 +2299,7 @@ mod tests {
let default_keypair_file = make_tmp_path("keypair_file"); let default_keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &default_keypair_file).unwrap(); write_keypair_file(&default_keypair, &default_keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("keypair", &default_keypair_file);
arg_name: "keypair".to_string(),
path: default_keypair_file,
};
let signer_info = default_signer let signer_info = default_signer
.generate_unique_signers(vec![], &matches, &mut None) .generate_unique_signers(vec![], &matches, &mut None)
@ -2380,10 +2377,7 @@ mod tests {
let keypair_file = make_tmp_path("keypair_file"); let keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &keypair_file).unwrap(); write_keypair_file(&default_keypair, &keypair_file).unwrap();
let keypair = read_keypair_file(&keypair_file).unwrap(); let keypair = read_keypair_file(&keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &keypair_file);
path: keypair_file.clone(),
arg_name: "".to_string(),
};
// Test Airdrop Subcommand // Test Airdrop Subcommand
let test_airdrop = let test_airdrop =
test_commands test_commands
@ -2911,10 +2905,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let default_keypair_file = make_tmp_path("keypair_file"); let default_keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &default_keypair_file).unwrap(); write_keypair_file(&default_keypair, &default_keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &default_keypair_file);
path: default_keypair_file.clone(),
arg_name: "".to_string(),
};
//Test Transfer Subcommand, SOL //Test Transfer Subcommand, SOL
let from_keypair = keypair_from_seed(&[0u8; 32]).unwrap(); let from_keypair = keypair_from_seed(&[0u8; 32]).unwrap();

View File

@ -2098,10 +2098,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let (default_keypair_file, mut tmp_file) = make_tmp_file(); let (default_keypair_file, mut tmp_file) = make_tmp_file();
write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap(); write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &default_keypair_file);
path: default_keypair_file,
arg_name: String::new(),
};
let test_cluster_version = test_commands let test_cluster_version = test_commands
.clone() .clone()

View File

@ -179,10 +179,7 @@ pub fn parse_args<'a>(
&config.keypair_path, &config.keypair_path,
); );
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new(default_signer_arg_name, &default_signer_path);
arg_name: default_signer_arg_name,
path: default_signer_path.clone(),
};
let CliCommandInfo { let CliCommandInfo {
command, command,

View File

@ -596,10 +596,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let (default_keypair_file, mut tmp_file) = make_tmp_file(); let (default_keypair_file, mut tmp_file) = make_tmp_file();
write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap(); write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &default_keypair_file);
path: default_keypair_file.clone(),
arg_name: String::new(),
};
let (keypair_file, mut tmp_file) = make_tmp_file(); let (keypair_file, mut tmp_file) = make_tmp_file();
let nonce_account_keypair = Keypair::new(); let nonce_account_keypair = Keypair::new();
write_keypair(&nonce_account_keypair, tmp_file.as_file_mut()).unwrap(); write_keypair(&nonce_account_keypair, tmp_file.as_file_mut()).unwrap();

View File

@ -2134,10 +2134,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let keypair_file = make_tmp_path("keypair_file"); let keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &keypair_file).unwrap(); write_keypair_file(&default_keypair, &keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &keypair_file);
path: keypair_file.clone(),
arg_name: "".to_string(),
};
let test_command = test_commands.clone().get_matches_from(vec![ let test_command = test_commands.clone().get_matches_from(vec![
"test", "test",
@ -2345,10 +2342,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let keypair_file = make_tmp_path("keypair_file"); let keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &keypair_file).unwrap(); write_keypair_file(&default_keypair, &keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &keypair_file);
path: keypair_file.clone(),
arg_name: "".to_string(),
};
// defaults // defaults
let test_command = test_commands.clone().get_matches_from(vec![ let test_command = test_commands.clone().get_matches_from(vec![
@ -2496,10 +2490,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let keypair_file = make_tmp_path("keypair_file"); let keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &keypair_file).unwrap(); write_keypair_file(&default_keypair, &keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &keypair_file);
path: keypair_file.clone(),
arg_name: "".to_string(),
};
let program_pubkey = Pubkey::new_unique(); let program_pubkey = Pubkey::new_unique();
let new_authority_pubkey = Pubkey::new_unique(); let new_authority_pubkey = Pubkey::new_unique();
@ -2607,10 +2598,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let keypair_file = make_tmp_path("keypair_file"); let keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &keypair_file).unwrap(); write_keypair_file(&default_keypair, &keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &keypair_file);
path: keypair_file.clone(),
arg_name: "".to_string(),
};
let buffer_pubkey = Pubkey::new_unique(); let buffer_pubkey = Pubkey::new_unique();
let new_authority_pubkey = Pubkey::new_unique(); let new_authority_pubkey = Pubkey::new_unique();
@ -2667,10 +2655,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let keypair_file = make_tmp_path("keypair_file"); let keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &keypair_file).unwrap(); write_keypair_file(&default_keypair, &keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &keypair_file);
path: keypair_file,
arg_name: "".to_string(),
};
// defaults // defaults
let buffer_pubkey = Pubkey::new_unique(); let buffer_pubkey = Pubkey::new_unique();
@ -2769,10 +2754,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let keypair_file = make_tmp_path("keypair_file"); let keypair_file = make_tmp_path("keypair_file");
write_keypair_file(&default_keypair, &keypair_file).unwrap(); write_keypair_file(&default_keypair, &keypair_file).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &keypair_file);
path: keypair_file.clone(),
arg_name: "".to_string(),
};
// defaults // defaults
let buffer_pubkey = Pubkey::new_unique(); let buffer_pubkey = Pubkey::new_unique();

View File

@ -2117,10 +2117,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let (default_keypair_file, mut tmp_file) = make_tmp_file(); let (default_keypair_file, mut tmp_file) = make_tmp_file();
write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap(); write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &default_keypair_file);
path: default_keypair_file.clone(),
arg_name: String::new(),
};
let (keypair_file, mut tmp_file) = make_tmp_file(); let (keypair_file, mut tmp_file) = make_tmp_file();
let stake_account_keypair = Keypair::new(); let stake_account_keypair = Keypair::new();
write_keypair(&stake_account_keypair, tmp_file.as_file_mut()).unwrap(); write_keypair(&stake_account_keypair, tmp_file.as_file_mut()).unwrap();

View File

@ -826,10 +826,7 @@ mod tests {
let default_keypair = Keypair::new(); let default_keypair = Keypair::new();
let (default_keypair_file, mut tmp_file) = make_tmp_file(); let (default_keypair_file, mut tmp_file) = make_tmp_file();
write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap(); write_keypair(&default_keypair, tmp_file.as_file_mut()).unwrap();
let default_signer = DefaultSigner { let default_signer = DefaultSigner::new("", &default_keypair_file);
path: default_keypair_file.clone(),
arg_name: String::new(),
};
let test_authorize_voter = test_commands.clone().get_matches_from(vec![ let test_authorize_voter = test_commands.clone().get_matches_from(vec![
"test", "test",