From 5ed64444e3fea234a5a4145b15aeb2f7b6319f1f Mon Sep 17 00:00:00 2001 From: Patrick Pichler Date: Wed, 6 Oct 2021 20:32:47 +0200 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + README.md | 4 ++++ src/bin/bat/config.rs | 33 ++++++++++++++++++++++++++++----- src/bin/bat/main.rs | 4 ++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1c0c0e4..9c193aee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 7c1611ad..95b67bd0 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/bin/bat/config.rs b/src/bin/bat/config.rs index 659f67b3..696edf9e 100644 --- a/src/bin/bat/config.rs +++ b/src/bin/bat/config.rs @@ -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, 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, shell_words::ParseError>> { diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 7275c94a..0369e436 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -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",