Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config)!: nested environment config overrides #157

Merged
merged 7 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ impl Config {
config::Config::builder().add_source(config::File::from(path))
};
Ok(config_builder
.add_source(config::Environment::with_prefix("CLIFF").separator("_"))
.add_source(
config::Environment::with_prefix("GIT_CLIFF").separator("__"),
)
.build()?
.try_deserialize()?)
}
Expand All @@ -164,9 +166,29 @@ mod test {
.to_path_buf()
.join("config")
.join(crate::DEFAULT_CONFIG);
env::set_var("CLIFF_CHANGELOG_FOOTER", "test");

const FOOTER_VALUE: &str = "test";
const TAG_PATTERN_VALUE: &str = "*[0-9]*";
const IGNORE_TAGS_VALUE: &str = "v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+";

env::set_var("GIT_CLIFF__CHANGELOG__FOOTER", FOOTER_VALUE);
env::set_var("GIT_CLIFF__GIT__TAG_PATTERN", TAG_PATTERN_VALUE);
env::set_var("GIT_CLIFF__GIT__IGNORE_TAGS", IGNORE_TAGS_VALUE);

let config = Config::parse(&path)?;
assert_eq!(Some(String::from("test")), config.changelog.footer);

assert_eq!(Some(String::from(FOOTER_VALUE)), config.changelog.footer);
assert_eq!(
Some(String::from(TAG_PATTERN_VALUE)),
config.git.tag_pattern
);
assert_eq!(
Some(String::from(IGNORE_TAGS_VALUE)),
config
.git
.ignore_tags
.map(|ignore_tags| ignore_tags.to_string())
);
Ok(())
}
}
23 changes: 23 additions & 0 deletions website/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,26 @@ Examples:
`limit_commits` is a **optional** positive integer number that limits the number of included commits in the generated changelog.

`limit_commits` is not part of the default configuration.

### Environment Configuration Overrides

It's possible to use environment variables to override configuration elements. If an environment variable matches a configuration element the variable's value will be used instead of the element's.

Format:
```
[PREFIX]__[CONFIG SECTION]__[FIELD NAME]
```

Examples:

To override the `footer` element:

```bash
export GIT_CLIFF__CHANGELOG__FOOTER="<!-- footer from env -->"
```

To override the `ignore_tags` element:

```bash
export GIT_CLIFF__GIT__IGNORE_TAGS="v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+"
```