From 744e55e743d7ce61fae608b0a1477fccb1ba79d5 Mon Sep 17 00:00:00 2001 From: Alex Kinnane <17098249+akinnane@users.noreply.github.com> Date: Fri, 12 Mar 2021 00:14:08 +0000 Subject: [PATCH] Add a system wide configuration file #668 --- CHANGELOG.md | 2 ++ src/bin/bat/app.rs | 4 ++-- src/bin/bat/config.rs | 22 ++++++++++++++++------ src/bin/bat/directories.rs | 12 ++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a709eacfaa..76804a1bdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Features +- Support a system wide configuration file on unix platforms `/etc/bat/config` (@akinnane) + ## Bugfixes ## Other diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index ce27b35c16..4001422867 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -7,7 +7,7 @@ use atty::{self, Stream}; use crate::{ clap_app, - config::{get_args_from_config_file, get_args_from_env_var}, + config::{get_args_from_config_files, get_args_from_env_var}, }; use clap::ArgMatches; @@ -61,7 +61,7 @@ impl App { // Read arguments from bats config file let mut args = get_args_from_env_var() - .unwrap_or_else(get_args_from_config_file) + .unwrap_or_else(get_args_from_config_files) .chain_err(|| "Could not parse configuration file")?; // Put the zero-th CLI argument (program name) first diff --git a/src/bin/bat/config.rs b/src/bin/bat/config.rs index 93ce8aaaac..5cb9fc7fd4 100644 --- a/src/bin/bat/config.rs +++ b/src/bin/bat/config.rs @@ -13,6 +13,12 @@ pub fn config_file() -> PathBuf { .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config")) } +pub fn system_config_file() -> Option { + PROJECT_DIRS + .system_config_dir() + .map(|path| path.join("config")) +} + pub fn generate_config_file() -> bat::error::Result<()> { let config_file = config_file(); if config_file.is_file() { @@ -86,12 +92,16 @@ pub fn generate_config_file() -> bat::error::Result<()> { Ok(()) } -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)) +pub fn get_args_from_config_files() -> Result, shell_words::ParseError> { + let system_config = system_config_file().and_then(|path| fs::read_to_string(path).ok()); + let user_config = fs::read_to_string(config_file()).ok(); + + Ok(system_config + .iter() + .chain(user_config.iter()) + .filter_map(|content| get_args_from_str(&content).ok()) + .flatten() + .collect()) } pub fn get_args_from_env_var() -> Option, shell_words::ParseError>> { diff --git a/src/bin/bat/directories.rs b/src/bin/bat/directories.rs index 7bb3e7d5ee..2aec9a9f1e 100644 --- a/src/bin/bat/directories.rs +++ b/src/bin/bat/directories.rs @@ -9,6 +9,7 @@ use lazy_static::lazy_static; pub struct BatProjectDirs { cache_dir: PathBuf, config_dir: PathBuf, + system_config_dir: Option, } impl BatProjectDirs { @@ -26,9 +27,16 @@ impl BatProjectDirs { let config_dir = config_dir_op.map(|d| d.join("bat"))?; + #[cfg(target_family = "unix")] + let system_config_dir = Some(PathBuf::from("/etc/bat")); + + #[cfg(target_family = "windows")] + let system_config_dir = None; + Some(BatProjectDirs { cache_dir, config_dir, + system_config_dir, }) } @@ -58,6 +66,10 @@ impl BatProjectDirs { pub fn config_dir(&self) -> &Path { &self.config_dir } + + pub fn system_config_dir(&self) -> Option<&PathBuf> { + self.system_config_dir.as_ref() + } } lazy_static! {