Skip to content

Allow some env vars to override config variables, but not command line arguments - #2381

Merged
sharkdp merged 9 commits into
sharkdp:masterfrom
aaronkollasch:env-override-config-not-flags
Nov 2, 2022
Merged

sharkdp merged 9 commits into
sharkdp:masterfrom
aaronkollasch:env-override-config-not-flags

Conversation

@aaronkollasch

@aaronkollasch aaronkollasch commented Oct 25, 2022 •

Copy link
Copy Markdown
Contributor

Giving #1281 a go.

This PR allows certain environment variables to take precedence over config settings, while allowing them to be overridden by their respective command line flags.

It does this by formatting those variables as a vector of command line args, and then inserting them between the config variables and command line args in App::matches(). Parsing the env variables in that way allows some env variable-specific code in App to be removed as well.

Right now, the env variables I picked are BAT_TABS, BAT_THEME, BAT_PAGER, and BAT_STYLE as I think those make the most sense to replace. It is straightforward to add more if needed. Note that BAT_PAGER takes precedence over config variables, but PAGER does not.

Also adds tests for the cache subcommand to make sure the changes don't break it (e.g. by inserting flags before cache).

Fixes #1152

Comment thread src/bin/bat/app.rs Outdated
@aaronkollasch
aaronkollasch force-pushed the env-override-config-not-flags branch 3 times, most recently from d806d9b to 386ac65 Compare October 28, 2022 19:31
Comment thread tests/integration_tests.rs Outdated
Comment thread tests/integration_tests.rs
Comment thread tests/integration_tests.rs
Comment thread tests/integration_tests.rs
Comment thread tests/integration_tests.rs
Comment thread CHANGELOG.md Outdated

@sharkdp sharkdp left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What an excellent PR. Good description, a ChangeLog entry, useful and sensible unit tests. And a really cool idea for implementing this feature that I hadn't thought of. Thank you very much!

Added some minor remarks as inline comments.

aaronkollasch and others added 9 commits November 2, 2022 16:48
Treat the cache subcommand differently from --no-config:
For --no-config, insert args from selected environment variables
For cache, don't insert args
Co-authored-by: David Peter <sharkdp@users.noreply.github.com>
Co-authored-by: David Peter <sharkdp@users.noreply.github.com>
@aaronkollasch

aaronkollasch commented Nov 2, 2022 •

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing @sharkdp! I'm happy with how this one came together.

Should be ready to go.

@sharkdp
sharkdp merged commit 78a67ac into sharkdp:master Nov 2, 2022
@sharkdp

sharkdp commented Nov 2, 2022

Copy link
Copy Markdown
Owner

Thank you for the updates!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Env var should take precedence over config

2 participants