Skip to content

Commit

Permalink
Allow configuring protofetch with a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimush committed Nov 2, 2023
1 parent 3b28e2b commit a937d08
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 22 deletions.
58 changes: 48 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ anyhow = "1.0.75"
clap = { version = "4.3.2", features = ["derive"] }
config = "0.13.3"
derive-new = "0.5.9"
dirs = "5.0.1"
env_logger = "0.10.0"
git2 = "0.17.2"
home = "0.5.5"
log = "0.4.18"
regex = "1.8.4"
serde = { version = "1.0.163", features = ["derive"] }
Expand Down
4 changes: 1 addition & 3 deletions src/api/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{env, error::Error, path::PathBuf};

use home::home_dir;

use crate::{config::ProtofetchConfig, git::cache::ProtofetchGitCache, Protofetch};

#[derive(Default)]
Expand Down Expand Up @@ -95,7 +93,7 @@ impl ProtofetchBuilder {

fn default_cache_directory() -> PathBuf {
let mut cache_directory =
home_dir().expect("Could not find home dir. Please define $HOME env variable.");
dirs::home_dir().expect("Could not find home dir. Please define $HOME env variable.");
cache_directory.push(".protofetch");
cache_directory.push("cache");
cache_directory
Expand Down
62 changes: 54 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashMap, path::PathBuf};

use config::{Config, ConfigError, Environment};
use config::{Config, ConfigError, Environment, File, FileFormat};
use log::debug;
use serde::Deserialize;

pub struct ProtofetchConfig {
Expand All @@ -9,7 +10,7 @@ pub struct ProtofetchConfig {

impl ProtofetchConfig {
pub fn load() -> anyhow::Result<Self> {
let raw_config = RawConfig::load(None)?;
let raw_config = RawConfig::load(None, None)?;

Ok(Self {
cache_dir: raw_config.cache.dir,
Expand All @@ -29,12 +30,34 @@ struct CacheConfig {
}

impl RawConfig {
fn load(env: Option<HashMap<String, String>>) -> Result<Self, ConfigError> {
Config::builder()
fn load(
config_override: Option<toml::Table>,
env_override: Option<HashMap<String, String>>,
) -> Result<Self, ConfigError> {
let mut builder = Config::builder();
if let Some(config_override) = config_override {
builder = builder.add_source(File::from_str(
&config_override.to_string(),
FileFormat::Toml,
));
} else {
let config_path = std::env::var("PROTOFETCH_CONFIG_DIR")
.ok()
.map(PathBuf::from)
.or_else(dirs::config_dir);
if let Some(mut config_path) = config_path {
config_path.push("protofetch");
config_path.push("config.toml");
debug!("Loading configuration from {}", config_path.display());
builder = builder.add_source(File::from(config_path).required(false));
}
}

builder
.add_source(
Environment::with_prefix("PROTOFETCH")
.separator("_")
.source(env),
.source(env_override),
)
.build()?
.try_deserialize()
Expand All @@ -43,14 +66,16 @@ impl RawConfig {

#[cfg(test)]
mod tests {
use toml::toml;

use super::*;

use pretty_assertions::assert_eq;

#[test]
fn load_empty() {
let env = HashMap::from([]);
let config = RawConfig::load(Some(env)).unwrap();
let env = HashMap::new();
let config = RawConfig::load(Some(Default::default()), Some(env)).unwrap();
assert_eq!(
config,
RawConfig {
Expand All @@ -62,7 +87,28 @@ mod tests {
#[test]
fn load_environment() {
let env = HashMap::from([("PROTOFETCH_CACHE_DIR".to_owned(), "/cache".to_owned())]);
let config = RawConfig::load(Some(env)).unwrap();
let config = RawConfig::load(Some(Default::default()), Some(env)).unwrap();
assert_eq!(
config,
RawConfig {
cache: CacheConfig {
dir: Some("/cache".into())
}
}
)
}

#[test]
fn load_config_file() {
let env = HashMap::new();
let config = RawConfig::load(
Some(toml! {
[cache]
dir = "/cache"
}),
Some(env),
)
.unwrap();
assert_eq!(
config,
RawConfig {
Expand Down

0 comments on commit a937d08

Please sign in to comment.