From 210b6341137016b902b20736f76c358e47d53c97 Mon Sep 17 00:00:00 2001 From: orhun Date: Wed, 18 Aug 2021 23:46:37 +0300 Subject: [PATCH] feat(config): support a global location for configuration file (#2) --- Cargo.lock | 41 +++++++++++++++++++++++++++++++++++++++++ README.md | 8 +++++++- git-cliff/Cargo.toml | 1 + git-cliff/src/lib.rs | 14 ++++++++++++-- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b5832271a..598afda68a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -245,6 +245,27 @@ dependencies = [ "generic-array 0.14.4", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -326,6 +347,7 @@ dependencies = [ name = "git-cliff" version = "0.1.2" dependencies = [ + "dirs-next", "git-cliff-core", "log", "pretty_assertions", @@ -832,6 +854,25 @@ dependencies = [ "rand_core", ] +[[package]] +name = "redox_syscall" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom", + "redox_syscall", +] + [[package]] name = "regex" version = "1.5.4" diff --git a/README.md b/README.md index fd01969d2e..8db18f4398 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,13 @@ Also, see the [continuous deployment workflow](./.github/workflows/cd.yml) of th **git-cliff** configuration file supports [TOML](https://github.com/toml-lang/toml) (preferred) and [YAML](https://yaml.org) formats. -See [config/cliff.toml](./config/cliff.toml) for an example. +The configuration file is read from `$HOME/git-cliff/cliff.toml` if the file exists. This location depends on the platform, for example: + +- on Linux: `/home//.config/git-cliff/cliff.toml` +- on Windows: `C:\Users\\AppData\Roaming\git-cliff\cliff.toml` +- on macOS: `/Users//Library/Application Support/git-cliff/cliff.toml` + +See [config/cliff.toml](./config/cliff.toml) for the default configuration values. ### changelog diff --git a/git-cliff/Cargo.toml b/git-cliff/Cargo.toml index f5c068a086..5af7c4dd73 100644 --- a/git-cliff/Cargo.toml +++ b/git-cliff/Cargo.toml @@ -19,6 +19,7 @@ path = "src/bin/completions.rs" [dependencies] pretty_env_logger = "0.4.0" log = "0.4.14" +dirs-next = "2.0.0" [dependencies.git-cliff-core] version = "0.1.2" # managed by release.sh diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index 961e29a067..329477e0e4 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -46,14 +46,24 @@ pub fn run(mut args: Opt) -> Result<()> { } } - // Parse configuration file. - let path = match args.config.to_str() { + // 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)) + .map(|path| path.to_str().map(String::from)) + .flatten() + { + if fs::metadata(&config_path).is_ok() { + path = config_path; + } + } + let mut config = if fs::metadata(&path).is_ok() { Config::parse(path)? } else {