Skip to content

Commit

Permalink
Add systemwide config file support
Browse files Browse the repository at this point in the history
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.

fixes sharkdp#668
  • Loading branch information
Patrick Pichler committed Oct 6, 2021
1 parent 5543746 commit bbc366b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/bin/bat/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -87,11 +103,17 @@ 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);
}

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>> {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ansi_term::Style;

use crate::{
app::App,
config::{config_file, generate_config_file},
config::{config_file, system_config_file, generate_config_file},
};

use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
Expand Down Expand Up @@ -252,6 +252,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(CompileTimeInformation::default());

Expand Down

0 comments on commit bbc366b

Please sign in to comment.