diff --git a/Cargo.lock b/Cargo.lock index 72fe93457..03d551922 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3658,7 +3658,6 @@ dependencies = [ "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "humantime 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "indicatif 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "pretty-hex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3666,9 +3665,9 @@ dependencies = [ "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)", "solana-budget-program 0.24.0", "solana-clap-utils 0.24.0", + "solana-cli-config 0.24.0", "solana-client 0.24.0", "solana-config-program 0.24.0", "solana-core 0.24.0", @@ -3685,6 +3684,17 @@ dependencies = [ "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "solana-cli-config" +version = "0.24.0" +dependencies = [ + "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "solana-client" version = "0.24.0" @@ -3948,6 +3958,7 @@ dependencies = [ "num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "rpassword 4.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "solana-clap-utils 0.24.0", + "solana-cli-config 0.24.0", "solana-sdk 0.24.0", "tiny-bip39 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index b39e09a54..3d38eb9a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "chacha", "chacha-cuda", "chacha-sys", + "cli-config", "client", "core", "faucet", diff --git a/cli-config/Cargo.toml b/cli-config/Cargo.toml new file mode 100644 index 000000000..478e4166b --- /dev/null +++ b/cli-config/Cargo.toml @@ -0,0 +1,16 @@ +[package] +authors = ["Solana Maintainers "] +edition = "2018" +name = "solana-cli-config" +description = "Blockchain, Rebuilt for Scale" +version = "0.24.0" +repository = "https://github.com/solana-labs/solana" +license = "Apache-2.0" +homepage = "https://solana.com/" + +[dependencies] +dirs = "2.0.2" +lazy_static = "1.4.0" +serde = "1.0.104" +serde_derive = "1.0.103" +serde_yaml = "0.8.11" diff --git a/cli/src/config.rs b/cli-config/src/config.rs similarity index 94% rename from cli/src/config.rs rename to cli-config/src/config.rs index 829b4693c..13c14de1f 100644 --- a/cli/src/config.rs +++ b/cli-config/src/config.rs @@ -1,8 +1,10 @@ // Wallet settings that can be configured for long-term use use serde_derive::{Deserialize, Serialize}; -use std::fs::{create_dir_all, File}; -use std::io::{self, Write}; -use std::path::Path; +use std::{ + fs::{create_dir_all, File}, + io::{self, Write}, + path::Path, +}; lazy_static! { pub static ref CONFIG_FILE: Option = { diff --git a/cli-config/src/lib.rs b/cli-config/src/lib.rs new file mode 100644 index 000000000..41bd5fc47 --- /dev/null +++ b/cli-config/src/lib.rs @@ -0,0 +1,4 @@ +#[macro_use] +extern crate lazy_static; + +pub mod config; diff --git a/cli/Cargo.toml b/cli/Cargo.toml index a0403530a..ff3099252 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -17,7 +17,6 @@ criterion-stats = "0.3.0" ctrlc = { version = "3.1.3", features = ["termination"] } console = "0.9.2" dirs = "2.0.2" -lazy_static = "1.4.0" log = "0.4.8" indicatif = "0.14.0" humantime = "2.0.0" @@ -27,9 +26,9 @@ reqwest = { version = "0.10.1", default-features = false, features = ["blocking" serde = "1.0.104" serde_derive = "1.0.103" serde_json = "1.0.44" -serde_yaml = "0.8.11" solana-budget-program = { path = "../programs/budget", version = "0.24.0" } solana-clap-utils = { path = "../clap-utils", version = "0.24.0" } +solana-cli-config = { path = "../cli-config", version = "0.24.0" } solana-client = { path = "../client", version = "0.24.0" } solana-config-program = { path = "../programs/config", version = "0.24.0" } solana-faucet = { path = "../faucet", version = "0.24.0" } diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 113409647..b0ad6e5f8 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1,9 +1,5 @@ -#[macro_use] -extern crate lazy_static; - pub mod cli; pub mod cluster_query; -pub mod config; pub mod display; pub mod nonce; pub mod offline; diff --git a/cli/src/main.rs b/cli/src/main.rs index dcffb4da7..ce9fdb29d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -10,9 +10,9 @@ use solana_clap_utils::{ }; use solana_cli::{ cli::{app, parse_command, process_command, CliCommandInfo, CliConfig, CliError}, - config::{self, Config}, display::{println_name_value, println_name_value_or}, }; +use solana_cli_config::config::{Config, CONFIG_FILE}; use solana_sdk::signature::read_keypair_file; use std::error; @@ -162,7 +162,7 @@ fn main() -> Result<(), Box> { .takes_value(true) .global(true) .help("Configuration file to use"); - if let Some(ref config_file) = *config::CONFIG_FILE { + if let Some(ref config_file) = *CONFIG_FILE { arg.default_value(&config_file) } else { arg diff --git a/keygen/Cargo.toml b/keygen/Cargo.toml index 944cb77b9..476481424 100644 --- a/keygen/Cargo.toml +++ b/keygen/Cargo.toml @@ -15,6 +15,7 @@ dirs = "2.0.2" num_cpus = "1.12.0" rpassword = "4.0" solana-clap-utils = { path = "../clap-utils", version = "0.24.0" } +solana-cli-config = { path = "../cli-config", version = "0.24.0" } solana-sdk = { path = "../sdk", version = "0.24.0" } tiny-bip39 = "0.7.0" diff --git a/keygen/src/keygen.rs b/keygen/src/keygen.rs index 2826d31ba..0ee3ed33b 100644 --- a/keygen/src/keygen.rs +++ b/keygen/src/keygen.rs @@ -8,6 +8,7 @@ use num_cpus; use solana_clap_utils::keypair::{ keypair_from_seed_phrase, prompt_passphrase, ASK_KEYWORD, SKIP_SEED_PHRASE_VALIDATION_ARG, }; +use solana_cli_config::config::{Config, CONFIG_FILE}; use solana_sdk::{ pubkey::write_pubkey_file, signature::{ @@ -44,23 +45,28 @@ fn check_for_overwrite(outfile: &str, matches: &ArgMatches) { } } -fn get_keypair_from_matches(matches: &ArgMatches) -> Result> { +fn get_keypair_from_matches( + matches: &ArgMatches, + config: Config, +) -> Result> { let mut path = dirs::home_dir().expect("home directory"); - let infile = if matches.is_present("infile") { - matches.value_of("infile").unwrap() + let keypair = if matches.is_present("keypair") { + matches.value_of("keypair").unwrap() + } else if config.keypair_path != "" { + &config.keypair_path } else { path.extend(&[".config", "solana", "id.json"]); path.to_str().unwrap() }; - if infile == "-" { + if keypair == "-" { let mut stdin = std::io::stdin(); read_keypair(&mut stdin) - } else if infile == ASK_KEYWORD { + } else if keypair == ASK_KEYWORD { let skip_validation = matches.is_present(SKIP_SEED_PHRASE_VALIDATION_ARG.name); keypair_from_seed_phrase("pubkey recovery", skip_validation, false) } else { - read_keypair_file(infile) + read_keypair_file(keypair) } } @@ -193,23 +199,38 @@ fn main() -> Result<(), Box> { .about(crate_description!()) .version(solana_clap_utils::version!()) .setting(AppSettings::SubcommandRequiredElseHelp) + .arg({ + let arg = Arg::with_name("config_file") + .short("C") + .long("config") + .value_name("PATH") + .takes_value(true) + .global(true) + .help("Configuration file to use"); + if let Some(ref config_file) = *CONFIG_FILE { + arg.default_value(&config_file) + } else { + arg + } + }) .subcommand( SubCommand::with_name("verify") .about("Verify a keypair can sign and verify a message.") .arg( - Arg::with_name("infile") + Arg::with_name("pubkey") .index(1) + .value_name("BASE58_PUBKEY") + .takes_value(true) + .required(true) + .help("Public key"), + ) + .arg( + Arg::with_name("keypair") + .index(2) .value_name("PATH") .takes_value(true) .help("Path to keypair file"), ) - .arg( - Arg::with_name("pubkey") - .index(2) - .value_name("BASE58_PUBKEY") - .takes_value(true) - .help("Public key"), - ) ) .subcommand( SubCommand::with_name("new") @@ -301,7 +322,7 @@ fn main() -> Result<(), Box> { .about("Display the pubkey from a keypair file") .setting(AppSettings::DisableVersion) .arg( - Arg::with_name("infile") + Arg::with_name("keypair") .index(1) .value_name("PATH") .takes_value(true) @@ -353,10 +374,15 @@ fn main() -> Result<(), Box> { ) .get_matches(); + let config = if let Some(config_file) = matches.value_of("config_file") { + Config::load(config_file).unwrap_or_default() + } else { + Config::default() + }; match matches.subcommand() { ("pubkey", Some(matches)) => { - let keypair = get_keypair_from_matches(matches)?; + let keypair = get_keypair_from_matches(matches, config)?; if matches.is_present("outfile") { let outfile = matches.value_of("outfile").unwrap(); @@ -533,7 +559,7 @@ fn main() -> Result<(), Box> { } } ("verify", Some(matches)) => { - let keypair = get_keypair_from_matches(matches)?; + let keypair = get_keypair_from_matches(matches, config)?; let test_data = b"test"; let signature = Signature::new(&keypair.sign(test_data).to_bytes()); let pubkey_bs58 = matches.value_of("pubkey").unwrap();