1
1
mirror of https://github.com/orhun/git-cliff.git synced 2025-01-05 22:47:45 +03:00

fix(config): lower the priority of global configuration file (#51)

This commit is contained in:
Orhun Parmaksız 2022-02-04 01:35:55 +03:00
parent bedabc93dd
commit 25959529d6
No known key found for this signature in database
GPG Key ID: B928720AEC532117
2 changed files with 17 additions and 23 deletions

View File

@ -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<Config> {
pub fn parse(path: &Path) -> Result<Config> {
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(())
}

View File

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