Skip to content

Commit

Permalink
fix(config): lower the priority of global configuration file (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Feb 3, 2022
1 parent bedabc9 commit 2595952
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
14 changes: 6 additions & 8 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::Result;
use regex::Regex;
use std::path::Path;

/// Configuration values.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -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()?)
}
Expand All @@ -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(())
}
Expand Down
26 changes: 11 additions & 15 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 2595952

Please sign in to comment.