Skip to content

Commit

Permalink
feat(config): support a global location for configuration file (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Aug 18, 2021
1 parent 45889c0 commit 210b634
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
41 changes: 41 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<user>/.config/git-cliff/cliff.toml`
- on Windows: `C:\Users\<user>\AppData\Roaming\git-cliff\cliff.toml`
- on macOS: `/Users/<user>/Library/Application Support/git-cliff/cliff.toml`

See [config/cliff.toml](./config/cliff.toml) for the default configuration values.

### changelog

Expand Down
1 change: 1 addition & 0 deletions git-cliff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 210b634

Please sign in to comment.