Add systemwide config file support

There is now support for a systemwide config file. The location of the
system wide config file is `$(BAT_SYSTEM_CONFIG_PREFIX)/bat/config`.
`$(BAT_SYSTEM_CONFIG_PREFIX)` has to be provided at compile time as an
environment variable. If the environment variable is not set, a default
is used. This default is `C:\ProgramData` for windows and `/etc` for
every other os.
This commit is contained in:
Patrick Pichler 2021-10-06 20:32:47 +02:00
parent 9ab378b5dc
commit 5ed64444e3
4 changed files with 37 additions and 5 deletions

View File

@ -64,6 +64,7 @@
- Support for `x:+delta` syntax in line ranges (e.g. `20:+10`). See #1810 (@bojan88)
- Add new `--acknowledgements` option that gives credit to theme and syntax definition authors. See #1971 (@Enselic)
- Include git hash in `bat -V` and `bat --version` output if present. See #1921 (@Enselic)
- Support for separate system and user config files. See #668 (@patrickpichler)
## Bugfixes

View File

@ -621,6 +621,10 @@ A default configuration file can be created with the `--generate-config-file` op
bat --generate-config-file
```
There is also now a systemwide configuration file, which is located under `/etc/bat/config` on
Linux and Mac OS and `C:\ProgramData\bat\config` on windows. If the system wide configuration
file is present, the content of the user configuration will simply be appended to it.
### Format
The configuration file is a simple list of command line arguments. Use `bat --help` to see a full list of possible options and values. In addition, you can add comments by prepending a line with the `#` character.

View File

@ -6,6 +6,22 @@ use std::path::PathBuf;
use crate::directories::PROJECT_DIRS;
#[cfg(not(target_os = "windows"))]
const DEFAULT_SYSTEM_CONFIG_PREFIX: &str = "/etc";
#[cfg(target_os = "windows")]
const DEFAULT_SYSTEM_CONFIG_PREFIX: &str = "C:\\ProgramData";
pub fn system_config_file() -> PathBuf {
let folder = option_env!("BAT_SYSTEM_CONFIG_PREFIX").unwrap_or(DEFAULT_SYSTEM_CONFIG_PREFIX);
let mut path = PathBuf::from(folder);
path.push("bat");
path.push("config");
path
}
pub fn config_file() -> PathBuf {
env::var("BAT_CONFIG_PATH")
.ok()
@ -87,11 +103,18 @@ pub fn generate_config_file() -> bat::error::Result<()> {
}
pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
Ok(fs::read_to_string(config_file())
.ok()
.map(|content| get_args_from_str(&content))
.transpose()?
.unwrap_or_else(Vec::new))
let mut config = String::new();
if let Ok(c) = fs::read_to_string(system_config_file()) {
config.push_str(&c);
config.push('\n');
}
if let Ok(c) = fs::read_to_string(config_file()) {
config.push_str(&c);
}
get_args_from_str(&config)
}
pub fn get_args_from_env_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {

View File

@ -21,6 +21,9 @@ use crate::{
config::{config_file, generate_config_file},
};
#[cfg(feature = "bugreport")]
use crate::config::system_config_file;
use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
use directories::PROJECT_DIRS;
use globset::GlobMatcher;
@ -256,6 +259,7 @@ fn invoke_bugreport(app: &App) {
"NO_COLOR",
"MANPAGER",
]))
.info(FileContent::new("System Config file", system_config_file()))
.info(FileContent::new("Config file", config_file()))
.info(FileContent::new(
"Custom assets metadata",