From fcd7bd80d8a02c004bb4370d299de3032d44ceec Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Mon, 19 Apr 2021 10:34:13 -0300 Subject: [PATCH] Config: get from cli, then env, then yaml (#528) --- src/config/env.rs | 31 +++++++++++++++++++++++++++++++ src/config/mod.rs | 20 +++++++++++++++++--- src/config/yaml.rs | 28 ++++++++++------------------ 3 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 src/config/env.rs diff --git a/src/config/env.rs b/src/config/env.rs new file mode 100644 index 0000000..e2b364a --- /dev/null +++ b/src/config/env.rs @@ -0,0 +1,31 @@ +use crate::env_var; + +use crate::finder::FinderChoice; + +use std::str::FromStr; + +pub struct EnvConfig { + pub config_yaml: Option, + pub config_path: Option, + pub path: Option, + pub shell: Option, + pub finder: Option, + pub fzf_overrides: Option, + pub fzf_overrides_var: Option, +} + +impl EnvConfig { + pub fn new() -> Self { + Self { + config_yaml: env_var::get(env_var::CONFIG_YAML).ok(), + config_path: env_var::get(env_var::CONFIG).ok(), + path: env_var::get(env_var::PATH).ok(), + shell: env_var::get(env_var::SHELL).ok(), + finder: env_var::get(env_var::FINDER) + .ok() + .and_then(|x| FinderChoice::from_str(&x).ok()), + fzf_overrides: env_var::get(env_var::FZF_OVERRIDES).ok(), + fzf_overrides_var: env_var::get(env_var::FZF_OVERRIDES_VAR).ok(), + } + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index f69bae0..ce814ec 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,4 +1,5 @@ mod cli; +mod env; mod yaml; use crate::finder::FinderChoice; @@ -7,6 +8,7 @@ use crate::terminal::style::Color; pub use cli::*; use std::process; +use env::EnvConfig; use yaml::YamlConfig; lazy_static! { @@ -15,13 +17,16 @@ lazy_static! { pub struct Config { yaml: YamlConfig, clap: ClapConfig, + env: EnvConfig, } impl Config { pub fn new() -> Self { - match YamlConfig::get() { + let env = EnvConfig::new(); + match YamlConfig::get(&env) { Ok(yaml) => Self { yaml, + env, clap: ClapConfig::new(), }, Err(e) => { @@ -50,17 +55,25 @@ impl Config { } pub fn path(&self) -> Option { - self.clap.path.clone().or_else(|| self.yaml.cheats.path.clone()) + self.clap + .path + .clone() + .or_else(|| self.env.path.clone()) + .or_else(|| self.yaml.cheats.path.clone()) } pub fn finder(&self) -> FinderChoice { - self.clap.finder.unwrap_or(self.yaml.finder.command) + self.clap + .finder + .or(self.env.finder) + .unwrap_or(self.yaml.finder.command) } pub fn fzf_overrides(&self) -> Option { self.clap .fzf_overrides .clone() + .or_else(|| self.env.fzf_overrides.clone()) .or_else(|| self.yaml.finder.overrides.clone()) } @@ -68,6 +81,7 @@ impl Config { self.clap .fzf_overrides_var .clone() + .or_else(|| self.env.fzf_overrides_var.clone()) .or_else(|| self.yaml.finder.overrides_var.clone()) } diff --git a/src/config/yaml.rs b/src/config/yaml.rs index 77c698b..b2c9643 100644 --- a/src/config/yaml.rs +++ b/src/config/yaml.rs @@ -1,4 +1,4 @@ -use crate::env_var; +use super::env::EnvConfig; use crate::filesystem::default_config_pathbuf; use crate::finder::FinderChoice; use crate::fs; @@ -9,7 +9,6 @@ use std::convert::TryFrom; use std::io::BufReader; use std::path::Path; use std::path::PathBuf; -use std::str::FromStr; #[derive(Deserialize)] pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor); @@ -92,11 +91,11 @@ impl YamlConfig { serde_yaml::from_reader(reader).map_err(|e| e.into()) } - pub fn get() -> Result { - if let Ok(yaml) = env_var::get(env_var::CONFIG_YAML) { - return Self::from_str(&yaml); + pub fn get(env: &EnvConfig) -> Result { + if let Some(yaml) = env.config_yaml.as_ref() { + return Self::from_str(yaml); } - if let Ok(path_str) = env_var::get(env_var::CONFIG) { + if let Some(path_str) = env.config_path.as_ref() { let p = PathBuf::from(path_str); return YamlConfig::from_path(&p); } @@ -140,21 +139,16 @@ impl Default for Style { impl Default for Finder { fn default() -> Self { Self { - command: env_var::get(env_var::FINDER) - .ok() - .and_then(|x| FinderChoice::from_str(&x).ok()) - .unwrap_or(FinderChoice::Fzf), - overrides: env_var::get(env_var::FZF_OVERRIDES).ok(), - overrides_var: env_var::get(env_var::FZF_OVERRIDES_VAR).ok(), + command: FinderChoice::Fzf, + overrides: None, + overrides_var: None, } } } impl Default for Cheats { fn default() -> Self { - Self { - path: env_var::get(env_var::PATH).ok(), - } + Self { path: None } } } @@ -167,9 +161,7 @@ impl Default for Search { impl Default for Shell { fn default() -> Self { Self { - command: env_var::get(env_var::SHELL) - .ok() - .unwrap_or_else(|| "bash".to_string()), + command: "bash".to_string(), } } }