on master help sign in
feat(config): added recognition of $BAT_CONFIG_DIR
bf78288Bill Risher
CHANGELOG.md+1
@@ -2,6 +2,7 @@
2
2
3## Features
3## Features
4
4
5+- $BAT_CONFIG_DIR is now a recognized environment variable and has precedent over $XDG_CONFIG_HOME. (@billrisher)
5
6
6## Bugfixes
7## Bugfixes
7
8
src/bin/bat/directories.rs+19−11
@@ -4,8 +4,9 @@ use std::path::{Path, PathBuf};
4use lazy_static::lazy_static;
4use lazy_static::lazy_static;
5
5
6/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
6/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
7−/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
7+/// The `XDG_CACHE_HOME` environment variable is checked first. `BAT_CONFIG_DIR`
8+/// is then checked before the `XDG_CONFIG_HOME` environment variable.
8−/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
9+/// The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
9pub struct BatProjectDirs {
10pub struct BatProjectDirs {
10 cache_dir: PathBuf,
11 cache_dir: PathBuf,
11 config_dir: PathBuf,
12 config_dir: PathBuf,
@@ -14,17 +15,24 @@ pub struct BatProjectDirs {
14impl BatProjectDirs {
15impl BatProjectDirs {
15 fn new() -> Option<BatProjectDirs> {
16 fn new() -> Option<BatProjectDirs> {
16 let cache_dir = BatProjectDirs::get_cache_dir()?;
17 let cache_dir = BatProjectDirs::get_cache_dir()?;
18+
19+ // Checks whether or not $BAT_CONFIG_DIR exists. If it doesn't, set our config dir
20+ // to our system's default configuration home.
21+ let config_dir = if let Some(config_dir_op) = env::var_os("BAT_CONFIG_DIR")
22+ .map(PathBuf::from) {
23+ config_dir_op
24+ } else {
25+ #[cfg(target_os = "macos")]
26+ let config_dir_op = env::var_os("XDG_CONFIG_HOME")
27+ .map(PathBuf::from)
28+ .filter(|p| p.is_absolute())
29+ .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
17
30
18− #[cfg(target_os = "macos")]
19− let config_dir_op = env::var_os("XDG_CONFIG_HOME")
20− .map(PathBuf::from)
21− .filter(|p| p.is_absolute())
22− .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
23−
24− #[cfg(not(target_os = "macos"))]
31+ #[cfg(not(target_os = "macos"))]
25− let config_dir_op = dirs_next::config_dir();
32+ let config_dir_op = dirs_next::config_dir();
26
33
27− let config_dir = config_dir_op.map(|d| d.join("bat"))?;
34+ config_dir_op.map(|d| d.join("bat"))?
35+ };
28
36
29 Some(BatProjectDirs {
37 Some(BatProjectDirs {
30 cache_dir,
38 cache_dir,
tests/integration_tests.rs+19
@@ -747,6 +747,25 @@ fn config_location_when_generating() {
747 assert!(tmp_config_path.exists());
747 assert!(tmp_config_path.exists());
748}
748}
749
749
750+#[test]
751+fn config_location_from_bat_config_dir_variable() {
752+ let bat_conf_dir = tempdir().expect("can't create temporary directory.").path().join("bat_conf_dir/");
753+
754+ // Create file at BAT_CONFIG_DIR
755+ bat_with_config()
756+ .env("BAT_CONFIG_DIR", bat_conf_dir.to_str().unwrap())
757+ .arg("--generate-config-file")
758+ .assert()
759+ .success()
760+ .stdout(
761+ predicate::str::is_match("Success! Config file written to .*config\n")
762+ .unwrap()
763+ );
764+
765+ // Ensure generated config file exists.
766+ assert!(bat_conf_dir.join("config").exists());
767+}
768+
750#[test]
769#[test]
751fn config_read_arguments_from_file() {
770fn config_read_arguments_from_file() {
752 bat_with_config()
771 bat_with_config()