diff --git a/Cargo.lock b/Cargo.lock index 6f4f18d3a0..0ee5af99bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3764,6 +3764,7 @@ 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_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/cli-config/Cargo.toml b/cli-config/Cargo.toml index 57ecbef0c6..837e00e0d0 100644 --- a/cli-config/Cargo.toml +++ b/cli-config/Cargo.toml @@ -14,3 +14,4 @@ lazy_static = "1.4.0" serde = "1.0.104" serde_derive = "1.0.103" serde_yaml = "0.8.11" +url = "2.1.1" diff --git a/cli-config/src/config.rs b/cli-config/src/config.rs index 6004c47bac..e060fe64fb 100644 --- a/cli-config/src/config.rs +++ b/cli-config/src/config.rs @@ -5,6 +5,7 @@ use std::{ io::{self, Write}, path::Path, }; +use url::Url; lazy_static! { pub static ref CONFIG_FILE: Option = { @@ -15,22 +16,32 @@ lazy_static! { }; } -#[derive(Serialize, Deserialize, Default, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct Config { pub json_rpc_url: String, pub websocket_url: String, pub keypair_path: String, } -impl Config { - pub fn new(json_rpc_url: &str, websocket_url: &str, keypair_path: &str) -> Self { +impl Default for Config { + fn default() -> Self { + let keypair_path = { + let mut keypair_path = dirs::home_dir().expect("home directory"); + keypair_path.extend(&[".config", "solana", "id.json"]); + keypair_path.to_str().unwrap().to_string() + }; + let json_rpc_url = "http://127.0.0.1:8899".to_string(); + let websocket_url = Self::compute_websocket_url(&json_rpc_url); + Self { - json_rpc_url: json_rpc_url.to_string(), - websocket_url: websocket_url.to_string(), - keypair_path: keypair_path.to_string(), + json_rpc_url, + websocket_url, + keypair_path, } } +} +impl Config { pub fn load(config_file: &str) -> Result { let file = File::open(config_file.to_string())?; let config = serde_yaml::from_reader(file) @@ -50,4 +61,29 @@ impl Config { Ok(()) } + + pub fn compute_websocket_url(json_rpc_url: &str) -> String { + let json_rpc_url: Option = json_rpc_url.parse().ok(); + if json_rpc_url.is_none() { + return "".to_string(); + } + let json_rpc_url = json_rpc_url.unwrap(); + let is_secure = json_rpc_url.scheme().to_ascii_lowercase() == "https"; + let mut ws_url = json_rpc_url.clone(); + ws_url + .set_scheme(if is_secure { "wss" } else { "ws" }) + .expect("unable to set scheme"); + let ws_port = match json_rpc_url.port() { + Some(port) => port + 1, + None => { + if is_secure { + 8901 + } else { + 8900 + } + } + }; + ws_url.set_port(Some(ws_port)).expect("unable to set port"); + ws_url.to_string() + } } diff --git a/cli-config/src/lib.rs b/cli-config/src/lib.rs index 41bd5fc47c..80853fe50d 100644 --- a/cli-config/src/lib.rs +++ b/cli-config/src/lib.rs @@ -1,4 +1,5 @@ #[macro_use] extern crate lazy_static; -pub mod config; +mod config; +pub use config::{Config, CONFIG_FILE}; diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 7cd1ac024c..6e43f97ee4 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -462,42 +462,15 @@ pub struct CliConfig<'a> { impl CliConfig<'_> { fn default_keypair_path() -> String { - let mut keypair_path = dirs::home_dir().expect("home directory"); - keypair_path.extend(&[".config", "solana", "id.json"]); - keypair_path.to_str().unwrap().to_string() + solana_cli_config::Config::default().keypair_path } fn default_json_rpc_url() -> String { - "http://127.0.0.1:8899".to_string() + solana_cli_config::Config::default().json_rpc_url } fn default_websocket_url() -> String { - Self::compute_ws_url(&Self::default_json_rpc_url()) - } - - fn compute_ws_url(rpc_url: &str) -> String { - let rpc_url: Option = rpc_url.parse().ok(); - if rpc_url.is_none() { - return "".to_string(); - } - let rpc_url = rpc_url.unwrap(); - let is_secure = rpc_url.scheme().to_ascii_lowercase() == "https"; - let mut ws_url = rpc_url.clone(); - ws_url - .set_scheme(if is_secure { "wss" } else { "ws" }) - .expect("unable to set scheme"); - let ws_port = match rpc_url.port() { - Some(port) => port + 1, - None => { - if is_secure { - 8901 - } else { - 8900 - } - } - }; - ws_url.set_port(Some(ws_port)).expect("unable to set port"); - ws_url.to_string() + solana_cli_config::Config::default().websocket_url } fn first_nonempty_setting( @@ -520,11 +493,11 @@ impl CliConfig<'_> { (SettingType::Explicit, websocket_cfg_url.to_string()), ( SettingType::Computed, - Self::compute_ws_url(json_rpc_cmd_url), + solana_cli_config::Config::compute_websocket_url(json_rpc_cmd_url), ), ( SettingType::Computed, - Self::compute_ws_url(json_rpc_cfg_url), + solana_cli_config::Config::compute_websocket_url(json_rpc_cfg_url), ), (SettingType::SystemDefault, Self::default_websocket_url()), ]) @@ -1981,7 +1954,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult { } => { let faucet_addr = SocketAddr::new( faucet_host.unwrap_or_else(|| { - let faucet_host = url::Url::parse(&config.json_rpc_url) + let faucet_host = Url::parse(&config.json_rpc_url) .unwrap() .host() .unwrap() diff --git a/cli/src/main.rs b/cli/src/main.rs index d704d6958b..0607b4a734 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -10,7 +10,7 @@ use solana_cli::{ cli::{app, parse_command, process_command, CliCommandInfo, CliConfig, CliSigners}, display::{println_name_value, println_name_value_or}, }; -use solana_cli_config::config::{Config, CONFIG_FILE}; +use solana_cli_config::{Config, CONFIG_FILE}; use solana_remote_wallet::remote_wallet::{maybe_wallet_manager, RemoteWalletManager}; use std::{error, sync::Arc}; diff --git a/keygen/src/keygen.rs b/keygen/src/keygen.rs index 8da3c5e8cc..57c85e7541 100644 --- a/keygen/src/keygen.rs +++ b/keygen/src/keygen.rs @@ -12,7 +12,7 @@ use solana_clap_utils::{ SKIP_SEED_PHRASE_VALIDATION_ARG, }, }; -use solana_cli_config::config::{Config, CONFIG_FILE}; +use solana_cli_config::{Config, CONFIG_FILE}; use solana_remote_wallet::remote_wallet::{maybe_wallet_manager, RemoteWalletManager}; use solana_sdk::{ instruction::{AccountMeta, Instruction},