From 25959529d60340caac668e0dd3e5c5b105ab4290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Fri, 4 Feb 2022 01:35:55 +0300 Subject: [PATCH] fix(config): lower the priority of global configuration file (#51) --- git-cliff-core/src/config.rs | 14 ++++++-------- git-cliff/src/lib.rs | 26 +++++++++++--------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/git-cliff-core/src/config.rs b/git-cliff-core/src/config.rs index 6dfd0cfd78..cc6423ba91 100644 --- a/git-cliff-core/src/config.rs +++ b/git-cliff-core/src/config.rs @@ -1,5 +1,6 @@ use crate::error::Result; use regex::Regex; +use std::path::Path; /// Configuration values. #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] @@ -83,10 +84,10 @@ pub struct LinkParser { impl Config { /// Parses the config file and returns the values. - pub fn parse(file_name: String) -> Result { + pub fn parse(path: &Path) -> Result { let mut config = config::Config::default(); config - .merge(config::File::with_name(&file_name))? + .merge(config::File::from(path))? .merge(config::Environment::with_prefix("CLIFF").separator("_"))?; Ok(config.try_into()?) } @@ -100,17 +101,14 @@ mod test { use std::path::PathBuf; #[test] fn parse_config() -> Result<()> { - let file_name = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) .parent() .unwrap() .to_path_buf() .join("config") - .join(crate::DEFAULT_CONFIG) - .to_str() - .unwrap() - .to_string(); + .join(crate::DEFAULT_CONFIG); env::set_var("CLIFF_CHANGELOG_FOOTER", "test"); - let config = Config::parse(file_name)?; + let config = Config::parse(&path)?; assert_eq!("test", config.changelog.footer.unwrap()); Ok(()) } diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index c79d702b76..6542ef743d 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -52,27 +52,23 @@ pub fn run(mut args: Opt) -> Result<()> { } // Parse the configuration file. - let mut path = match args.config.to_str() { - Some(v) => Ok(v.to_string()), - None => Err(Error::IoError(io::Error::new( - io::ErrorKind::Other, - "path contains invalid characters", - ))), - }?; - if let Some(config_path) = dirs_next::config_dir() - .map(|dir| dir.join(env!("CARGO_PKG_NAME")).join(DEFAULT_CONFIG)) - .and_then(|path| path.to_str().map(String::from)) - { - if fs::metadata(&config_path).is_ok() { + let mut path = args.config.clone(); + if !path.exists() { + if let Some(config_path) = dirs_next::config_dir() + .map(|dir| dir.join(env!("CARGO_PKG_NAME")).join(DEFAULT_CONFIG)) + { path = config_path; } } // Load the default configuration if necessary. - let mut config = if fs::metadata(&path).is_ok() { - Config::parse(path)? + let mut config = if path.exists() { + Config::parse(&path)? } else { - warn!("{:?} is not found, using the default configuration.", path); + warn!( + "{:?} is not found, using the default configuration.", + args.config + ); EmbeddedConfig::parse()? }; if config.changelog.body.is_none() {