solana-keygen no longer blindly overwrites a keypair, or assumes "new" (#4599)
automerge
This commit is contained in:
@@ -1,23 +1,29 @@
|
||||
use clap::{crate_description, crate_name, crate_version, App, Arg, SubCommand};
|
||||
use clap::{
|
||||
crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgMatches, SubCommand,
|
||||
};
|
||||
use solana_sdk::pubkey::write_pubkey;
|
||||
use solana_sdk::signature::{gen_keypair_file, read_keypair, KeypairUtil};
|
||||
use std::error;
|
||||
use std::path::Path;
|
||||
use std::process::exit;
|
||||
|
||||
fn check_for_overwrite(outfile: &str, matches: &ArgMatches) {
|
||||
let force = matches.is_present("force");
|
||||
if !force && Path::new(outfile).exists() {
|
||||
eprintln!("Refusing to overwrite {} without --force flag", outfile);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
||||
let matches = App::new(crate_name!())
|
||||
.about(crate_description!())
|
||||
.version(crate_version!())
|
||||
.arg(
|
||||
Arg::with_name("outfile")
|
||||
.short("o")
|
||||
.long("outfile")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.help("Path to generated file"),
|
||||
)
|
||||
.setting(AppSettings::SubcommandRequiredElseHelp)
|
||||
.subcommand(
|
||||
SubCommand::with_name("new")
|
||||
.about("Generate new keypair file")
|
||||
.setting(AppSettings::DisableVersion)
|
||||
.arg(
|
||||
Arg::with_name("outfile")
|
||||
.short("o")
|
||||
@@ -25,11 +31,18 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.help("Path to generated file"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("force")
|
||||
.short("f")
|
||||
.long("force")
|
||||
.help("Overwrite the output file if it exists"),
|
||||
),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("pubkey")
|
||||
.about("Display the pubkey from a keypair file")
|
||||
.setting(AppSettings::DisableVersion)
|
||||
.arg(
|
||||
Arg::with_name("infile")
|
||||
.index(1)
|
||||
@@ -44,48 +57,55 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.help("Path to generated file"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("force")
|
||||
.short("f")
|
||||
.long("force")
|
||||
.help("Overwrite the output file if it exists"),
|
||||
),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
match matches.subcommand() {
|
||||
("pubkey", Some(pubkey_matches)) => {
|
||||
("pubkey", Some(matches)) => {
|
||||
let mut path = dirs::home_dir().expect("home directory");
|
||||
let infile = if pubkey_matches.is_present("infile") {
|
||||
pubkey_matches.value_of("infile").unwrap()
|
||||
let infile = if matches.is_present("infile") {
|
||||
matches.value_of("infile").unwrap()
|
||||
} else {
|
||||
path.extend(&[".config", "solana", "id.json"]);
|
||||
path.to_str().unwrap()
|
||||
};
|
||||
let keypair = read_keypair(infile)?;
|
||||
|
||||
if pubkey_matches.is_present("outfile") {
|
||||
let outfile = pubkey_matches.value_of("outfile").unwrap();
|
||||
if matches.is_present("outfile") {
|
||||
let outfile = matches.value_of("outfile").unwrap();
|
||||
check_for_overwrite(&outfile, &matches);
|
||||
write_pubkey(outfile, keypair.pubkey())?;
|
||||
} else {
|
||||
println!("{}", keypair.pubkey());
|
||||
}
|
||||
}
|
||||
match_tuple => {
|
||||
let working_matches = if let (_, Some(new_matches)) = match_tuple {
|
||||
new_matches
|
||||
} else {
|
||||
&matches
|
||||
};
|
||||
|
||||
("new", Some(matches)) => {
|
||||
let mut path = dirs::home_dir().expect("home directory");
|
||||
let outfile = if working_matches.is_present("outfile") {
|
||||
working_matches.value_of("outfile").unwrap()
|
||||
let outfile = if matches.is_present("outfile") {
|
||||
matches.value_of("outfile").unwrap()
|
||||
} else {
|
||||
path.extend(&[".config", "solana", "id.json"]);
|
||||
path.to_str().unwrap()
|
||||
};
|
||||
|
||||
if outfile != "-" {
|
||||
check_for_overwrite(&outfile, &matches);
|
||||
}
|
||||
let serialized_keypair = gen_keypair_file(outfile)?;
|
||||
if outfile == "-" {
|
||||
println!("{}", serialized_keypair);
|
||||
} else {
|
||||
println!("Wrote {}", outfile);
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
Reference in New Issue
Block a user