Move cli-config default out of cli/ into cli-config/

This commit is contained in:
Michael Vines 2020-03-09 12:29:31 -07:00
parent 5c236fd06c
commit 756ba07b16
7 changed files with 54 additions and 42 deletions

1
Cargo.lock generated
View File

@ -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]]

View File

@ -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"

View File

@ -5,6 +5,7 @@ use std::{
io::{self, Write},
path::Path,
};
use url::Url;
lazy_static! {
pub static ref CONFIG_FILE: Option<String> = {
@ -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<Self, io::Error> {
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<Url> = 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()
}
}

View File

@ -1,4 +1,5 @@
#[macro_use]
extern crate lazy_static;
pub mod config;
mod config;
pub use config::{Config, CONFIG_FILE};

View File

@ -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<Url> = 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()

View File

@ -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};

View File

@ -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},