Skip to content

Commit

Permalink
Use default solana config for anchor idl fetch
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil B N <nikhilbn365@gmail.com>
  • Loading branch information
NBNARADHYA committed Feb 23, 2022
1 parent b643ca8 commit 897fb81
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
15 changes: 6 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
39 changes: 39 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>`, 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<String> = {
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.
Expand Down Expand Up @@ -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<String, io::Error> {
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<Cluster, BTreeMap<String, ProgramDeployment>>,
) -> BTreeMap<String, BTreeMap<String, serde_json::Value>> {
Expand Down
5 changes: 4 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1410,7 +1413,7 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result<Idl> {
false => cluster.url().to_string(),
}
} else {
return Err(anyhow!("provider.cluster option required"));
config::get_solana_cfg_url()?
}
}
};
Expand Down

0 comments on commit 897fb81

Please sign in to comment.