Skip to content

Commit

Permalink
Add a system wide configuration file sharkdp#668
Browse files Browse the repository at this point in the history
  • Loading branch information
akinnane committed Mar 12, 2021
1 parent db57454 commit 744e55e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Support a system wide configuration file on unix platforms `/etc/bat/config` (@akinnane)

## Bugfixes

## Other
Expand Down
4 changes: 2 additions & 2 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions src/bin/bat/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub fn config_file() -> PathBuf {
.unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
}

pub fn system_config_file() -> Option<PathBuf> {
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() {
Expand Down Expand Up @@ -86,12 +92,16 @@ pub fn generate_config_file() -> bat::error::Result<()> {
Ok(())
}

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))
pub fn get_args_from_config_files() -> Result<Vec<OsString>, 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<Result<Vec<OsString>, shell_words::ParseError>> {
Expand Down
12 changes: 12 additions & 0 deletions src/bin/bat/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use lazy_static::lazy_static;
pub struct BatProjectDirs {
cache_dir: PathBuf,
config_dir: PathBuf,
system_config_dir: Option<PathBuf>,
}

impl BatProjectDirs {
Expand All @@ -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,
})
}

Expand Down Expand Up @@ -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! {
Expand Down

0 comments on commit 744e55e

Please sign in to comment.