Skip to content

Commit

Permalink
Merge pull request #2381 from aaronkollasch/env-override-config-not-f…
Browse files Browse the repository at this point in the history
…lags

Allow some env vars to override config variables, but not command line arguments
  • Loading branch information
sharkdp committed Nov 2, 2022
2 parents 5652038 + 017e830 commit 78a67ac
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Features

- Implemented `-S` and `--chop-long-lines` flags as aliases for `--wrap=never`. See #2309 (@johnmatthiggins)
- Breaking change: Environment variables can now override config file settings (but command-line arguments still have the highest precedence), see #1152, #1281, and #2381 (@aaronkollasch)

## Bugfixes

Expand Down
40 changes: 20 additions & 20 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::collections::HashSet;
use std::env;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use atty::{self, Stream};

use crate::{
clap_app,
config::{get_args_from_config_file, get_args_from_env_var},
config::{get_args_from_config_file, get_args_from_env_opts_var, get_args_from_env_vars},
};
use clap::ArgMatches;

Expand Down Expand Up @@ -50,20 +49,34 @@ impl App {
}

fn matches(interactive_output: bool) -> Result<ArgMatches> {
let args = if wild::args_os().nth(1) == Some("cache".into())
|| wild::args_os().any(|arg| arg == "--no-config")
{
// Skip the arguments in bats config file
let args = if wild::args_os().nth(1) == Some("cache".into()) {
// Skip the config file and env vars

wild::args_os().collect::<Vec<_>>()
} else if wild::args_os().any(|arg| arg == "--no-config") {
// Skip the arguments in bats config file

let mut cli_args = wild::args_os();
let mut args = get_args_from_env_vars();

// Put the zero-th CLI argument (program name) first
args.insert(0, cli_args.next().unwrap());

// .. and the rest at the end
cli_args.for_each(|a| args.push(a));

args
} else {
let mut cli_args = wild::args_os();

// Read arguments from bats config file
let mut args = get_args_from_env_var()
let mut args = get_args_from_env_opts_var()
.unwrap_or_else(get_args_from_config_file)
.map_err(|_| "Could not parse configuration file")?;

// Selected env vars supersede config vars
args.extend(get_args_from_env_vars());

// Put the zero-th CLI argument (program name) first
args.insert(0, cli_args.next().unwrap());

Expand Down Expand Up @@ -203,7 +216,6 @@ impl App {
.matches
.get_one::<String>("tabs")
.map(String::from)
.or_else(|| env::var("BAT_TABS").ok())
.and_then(|t| t.parse().ok())
.unwrap_or(
if style_components.plain() && paging_mode == PagingMode::Never {
Expand All @@ -216,7 +228,6 @@ impl App {
.matches
.get_one::<String>("theme")
.map(String::from)
.or_else(|| env::var("BAT_THEME").ok())
.map(|s| {
if s == "default" {
String::from(HighlightingAssets::default_theme())
Expand Down Expand Up @@ -321,16 +332,6 @@ impl App {
} else if 0 < matches.get_count("plain") {
[StyleComponent::Plain].iter().cloned().collect()
} else {
let env_style_components: Option<Vec<StyleComponent>> = env::var("BAT_STYLE")
.ok()
.map(|style_str| {
style_str
.split(',')
.map(StyleComponent::from_str)
.collect::<Result<Vec<StyleComponent>>>()
})
.transpose()?;

matches
.get_one::<String>("style")
.map(|styles| {
Expand All @@ -340,7 +341,6 @@ impl App {
.filter_map(|style| style.ok())
.collect::<Vec<_>>()
})
.or(env_style_components)
.unwrap_or_else(|| vec![StyleComponent::Default])
.into_iter()
.map(|style| style.components(self.interactive_output))
Expand Down
16 changes: 15 additions & 1 deletion src/bin/bat/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseEr
get_args_from_str(&config)
}

pub fn get_args_from_env_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {
pub fn get_args_from_env_opts_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {
env::var("BAT_OPTS").ok().map(|s| get_args_from_str(&s))
}

Expand All @@ -137,6 +137,20 @@ fn get_args_from_str(content: &str) -> Result<Vec<OsString>, shell_words::ParseE
.collect())
}

pub fn get_args_from_env_vars() -> Vec<OsString> {
[
("--tabs", "BAT_TABS"),
("--theme", "BAT_THEME"),
("--pager", "BAT_PAGER"),
("--style", "BAT_STYLE"),
]
.iter()
.filter_map(|(flag, key)| env::var(key).ok().map(|var| [flag.to_string(), var]))
.flatten()
.map(|a| a.into())
.collect()
}

#[test]
fn empty() {
let args = get_args_from_str("").unwrap();
Expand Down
1 change: 1 addition & 0 deletions tests/examples/bat-tabs.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--tabs=8
1 change: 1 addition & 0 deletions tests/examples/bat-theme.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--theme=TwoDark
10 changes: 10 additions & 0 deletions tests/examples/cache_source/syntaxes/c.sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%YAML 1.2
---
name: C
file_extensions: [c, h]
scope: source.c

contexts:
main:
- match: \b(if|else|for|while)\b
scope: keyword.control.c
45 changes: 45 additions & 0 deletions tests/examples/cache_source/themes/example.tmTheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>example</string>
<key>settings</key>
<array>
<dict>
<key>settings</key>
<dict>
<key>background</key>
<string>#222222</string>
<key>caret</key>
<string>#979797</string>
<key>foreground</key>
<string>#F8F8F8</string>
<key>invisibles</key>
<string>#777777</string>
<key>lineHighlight</key>
<string>#000000</string>
<key>selection</key>
<string>#57CCBF</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Comment</string>
<key>scope</key>
<string>comment</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#777777</string>
</dict>
</dict>
</array>
<key>uuid</key>
<string>0123-4567-89AB-CDEF</string>
<key>colorSpaceName</key>
<string>sRGB</string>
<key>semanticClass</key>
<string>theme</string>
</dict>
</plist>

0 comments on commit 78a67ac

Please sign in to comment.