Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read bat config from multiple config files #691

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/syntaxes/TOML
18 changes: 16 additions & 2 deletions src/bin/bat/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use shell_words;

use bat::{dirs::PROJECT_DIRS, util::transpose};

pub fn config_file() -> PathBuf {
pub fn default_config_path() -> PathBuf {
env::var("BAT_CONFIG_PATH")
.ok()
.map(PathBuf::from)
Expand All @@ -16,8 +16,22 @@ pub fn config_file() -> PathBuf {
}

pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
let mut config_file_path = vec![PathBuf::from("/etc/bat/config")];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config_file_path => config_file_paths?

config_file_path.push(default_config_path());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so how does that work?

/etc/bat/config is always included but ~/.config/bat/config might be overwritten by BAT_CONFIG_PATH? Is that a good idea?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sharkdp Thanks for the review. I'm thinking of moving the logic for getting path from env var BAT_CONFIG_PATH to a separate function (something like get_config_file_path_from_env and default_config_path() will just return ~/.config/bat/config.


let mut config_args = vec![];

for path in config_file_path.iter() {
let args = read_args_from_config_file(path.to_path_buf());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could avoid the .to_path_buf() if read_args_from_config_file simply takes a &Path.

config_args.append(&mut args.unwrap());
}
Copy link
Owner

@sharkdp sharkdp Oct 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary, but the whole loop could probably be written in functional style with .map(…) and .collect(). This would also avoid the mut on config_args.

Something like (not tested and most probably wrong):

let config_args = config_file_path
  .iter()
  .map(read_args_from_config_file)
  .transpose()?
  .flatten()
  .collect();


Ok(config_args)
}

fn read_args_from_config_file(config_file_path: PathBuf) -> Result<Vec<OsString>, shell_words::ParseError> {
Ok(transpose(
fs::read_to_string(config_file())
fs::read_to_string(config_file_path)
.ok()
.map(|content| get_args_from_str(&content)),
)?
Expand Down
4 changes: 2 additions & 2 deletions src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::process;
use ansi_term::Colour::Green;
use ansi_term::Style;

use crate::{app::App, config::config_file};
use crate::{app::App, config::default_config_path};
use bat::controller::Controller;

use bat::{
Expand Down Expand Up @@ -177,7 +177,7 @@ fn run() -> Result<bool> {

Ok(true)
} else if app.matches.is_present("config-file") {
println!("{}", config_file().to_string_lossy());
println!("{}", default_config_path().to_string_lossy());

Ok(true)
} else if app.matches.is_present("config-dir") {
Expand Down