Config: get from cli, then env, then yaml (#528)

This commit is contained in:
Denis Isidoro 2021-04-19 10:34:13 -03:00 committed by GitHub
parent 7a7f1a3311
commit fcd7bd80d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 21 deletions

31
src/config/env.rs Normal file
View file

@ -0,0 +1,31 @@
use crate::env_var;
use crate::finder::FinderChoice;
use std::str::FromStr;
pub struct EnvConfig {
pub config_yaml: Option<String>,
pub config_path: Option<String>,
pub path: Option<String>,
pub shell: Option<String>,
pub finder: Option<FinderChoice>,
pub fzf_overrides: Option<String>,
pub fzf_overrides_var: Option<String>,
}
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(),
}
}
}

View file

@ -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<String> {
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<String> {
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())
}

View file

@ -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<Self> {
if let Ok(yaml) = env_var::get(env_var::CONFIG_YAML) {
return Self::from_str(&yaml);
pub fn get(env: &EnvConfig) -> Result<Self> {
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(),
}
}
}