From 897fb81d094429e71e0d93b1cbbbd4f3a2ec3a3c Mon Sep 17 00:00:00 2001 From: Nikhil B N Date: Wed, 23 Feb 2022 20:15:08 +0530 Subject: [PATCH] Use default solana config for anchor idl fetch Signed-off-by: Nikhil B N --- Cargo.lock | 15 ++++++--------- cli/Cargo.toml | 3 +++ cli/src/config.rs | 39 +++++++++++++++++++++++++++++++++++++++ cli/src/lib.rs | 5 ++++- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0cde610c44..901653aa62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,14 +155,17 @@ dependencies = [ "chrono", "clap 3.0.13", "dirs 3.0.2", + "dirs-next", "flate2", "heck 0.3.3", + "lazy_static", "pathdiff", "rand", "reqwest", "semver 1.0.4", "serde", "serde_json", + "serde_yaml", "serum-common", "shellexpand", "solana-client", @@ -1089,12 +1092,6 @@ dependencies = [ "syn 0.15.44", ] -[[package]] -name = "dtoa" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" - [[package]] name = "ed25519" version = "1.2.0" @@ -2844,12 +2841,12 @@ dependencies = [ [[package]] name = "serde_yaml" -version = "0.8.21" +version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8c608a35705a5d3cdc9fbe403147647ff34b921f8e833e49306df898f9b20af" +checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0" dependencies = [ - "dtoa", "indexmap", + "ryu", "serde", "yaml-rust", ] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 2852399845..4025d753e0 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -39,3 +39,6 @@ pathdiff = "0.2.0" cargo_toml = "0.9.2" walkdir = "2" chrono = "0.4.19" +lazy_static = "1.4.0" +serde_yaml = "0.8.23" +dirs-next = "2.0.0" diff --git a/cli/src/config.rs b/cli/src/config.rs index 0e828ff350..60214ff008 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -9,12 +9,30 @@ use solana_sdk::signature::{Keypair, Signer}; use std::collections::BTreeMap; use std::convert::TryFrom; use std::fs::{self, File}; +use std::io; use std::io::prelude::*; use std::ops::Deref; use std::path::Path; use std::path::PathBuf; use std::str::FromStr; +lazy_static! { + /// The default path to the SOLANA CLI configuration file. + /// + /// This is a [lazy_static] of `Option`, the value of which is + /// + /// > `~/.config/solana/cli/config.yml` + /// + /// It will only be `None` if it is unable to identify the user's home + /// directory, which should not happen under typical OS environments. + pub static ref SOL_CONFIG_FILE: Option = { + dirs_next::home_dir().map(|mut path| { + path.extend(&[".config", "solana", "cli", "config.yml"]); + path.to_str().unwrap().to_string() + }) + }; +} + #[derive(Default, Debug, Parser)] pub struct ConfigOverride { /// Cluster override. @@ -445,6 +463,27 @@ impl FromStr for Config { } } +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub struct SolanaConfig { + pub json_rpc_url: String, +} + +pub fn get_solana_cfg_url() -> Result { + let path_str = SOL_CONFIG_FILE.as_ref().ok_or_else(|| { + io::Error::new( + io::ErrorKind::NotFound, + "Default Solana config was not found", + ) + })?; + + let config_file = Path::new(path_str); + let file = File::open(config_file)?; + let config: SolanaConfig = serde_yaml::from_reader(file) + .map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?; + let url = config.json_rpc_url; + Ok(url) +} + fn ser_programs( programs: &BTreeMap>, ) -> BTreeMap> { diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 34528e4797..49cac5fffb 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -41,6 +41,9 @@ use std::process::{Child, Stdio}; use std::string::ToString; use tar::Archive; +#[macro_use] +extern crate lazy_static; + pub mod config; pub mod template; @@ -1410,7 +1413,7 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result { false => cluster.url().to_string(), } } else { - return Err(anyhow!("provider.cluster option required")); + config::get_solana_cfg_url()? } } };