|
| 1 | +use anyhow::{Error, Result}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use std::fs::{self, File}; |
| 4 | +use std::io::Read; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | +use std::str::FromStr; |
| 7 | +use std::string::ToString; |
| 8 | + |
| 9 | +pub struct WithPath<T> { |
| 10 | + inner: T, |
| 11 | + path: PathBuf, |
| 12 | +} |
| 13 | + |
| 14 | +impl<T> WithPath<T> { |
| 15 | + pub fn new(inner: T, path: PathBuf) -> Self { |
| 16 | + Self { inner, path } |
| 17 | + } |
| 18 | + |
| 19 | + pub fn path(&self) -> &PathBuf { |
| 20 | + &self.path |
| 21 | + } |
| 22 | + |
| 23 | + pub fn into_inner(self) -> T { |
| 24 | + self.inner |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<T> std::convert::AsRef<T> for WithPath<T> { |
| 29 | + fn as_ref(&self) -> &T { |
| 30 | + &self.inner |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<T> std::ops::Deref for WithPath<T> { |
| 35 | + type Target = T; |
| 36 | + fn deref(&self) -> &Self::Target { |
| 37 | + &self.inner |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl<T> std::ops::DerefMut for WithPath<T> { |
| 42 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 43 | + &mut self.inner |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Debug, Default, Serialize, Deserialize)] |
| 48 | +pub struct Config { |
| 49 | + pub rpc_endpoints: RPC, |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Clone, Debug, Serialize, Deserialize)] |
| 53 | +pub struct RPC { |
| 54 | + pub mainnet: String, |
| 55 | + pub devnet: String, |
| 56 | + pub testnet: String, |
| 57 | + pub localnet: String, |
| 58 | + pub debug: String, |
| 59 | +} |
| 60 | + |
| 61 | +impl Default for RPC { |
| 62 | + fn default() -> Self { |
| 63 | + Self { |
| 64 | + mainnet: "https://api.mainnet-beta.solana.com".to_string(), |
| 65 | + devnet: "https://api.devnet.solana.com".to_string(), |
| 66 | + testnet: "https://api.testnet.solana.com".to_string(), |
| 67 | + localnet: "http://127.0.0.1:8899".to_string(), |
| 68 | + debug: "http://34.90.18.145:8899".to_string(), |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl Config { |
| 74 | + // Climbs each parent directory until we find an Goki.toml. |
| 75 | + pub fn discover() -> Result<Option<WithPath<Config>>> { |
| 76 | + let _cwd = std::env::current_dir()?; |
| 77 | + let mut cwd_opt = Some(_cwd.as_path()); |
| 78 | + |
| 79 | + while let Some(cwd) = cwd_opt { |
| 80 | + for f in fs::read_dir(cwd)? { |
| 81 | + let p = f?.path(); |
| 82 | + if let Some(filename) = p.file_name() { |
| 83 | + if filename.to_str() == Some("Goki.toml") { |
| 84 | + let cfg = Config::from_path(&p)?; |
| 85 | + return Ok(Some(WithPath::new(cfg, p))); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + cwd_opt = cwd.parent(); |
| 91 | + } |
| 92 | + |
| 93 | + Ok(None) |
| 94 | + } |
| 95 | + |
| 96 | + fn from_path(p: impl AsRef<Path>) -> Result<Self> { |
| 97 | + let mut cfg_file = File::open(&p)?; |
| 98 | + let mut cfg_contents = String::new(); |
| 99 | + cfg_file.read_to_string(&mut cfg_contents)?; |
| 100 | + let cfg = cfg_contents.parse()?; |
| 101 | + |
| 102 | + Ok(cfg) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[derive(Debug, Serialize, Deserialize)] |
| 107 | +struct _Config { |
| 108 | + rpc_endpoints: Option<RPC>, |
| 109 | +} |
| 110 | + |
| 111 | +impl ToString for Config { |
| 112 | + fn to_string(&self) -> String { |
| 113 | + let cfg = _Config { |
| 114 | + rpc_endpoints: Some(RPC { |
| 115 | + ..self.rpc_endpoints.clone() |
| 116 | + }), |
| 117 | + }; |
| 118 | + |
| 119 | + toml::to_string(&cfg).expect("Must be well formed") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl FromStr for Config { |
| 124 | + type Err = Error; |
| 125 | + |
| 126 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 127 | + let cfg: _Config = toml::from_str(s) |
| 128 | + .map_err(|e| anyhow::format_err!("Unable to deserialize config: {}", e.to_string()))?; |
| 129 | + Ok(Config { |
| 130 | + rpc_endpoints: cfg.rpc_endpoints.unwrap_or_default(), |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments