push config option

This commit is contained in:
n4n5
2024-12-30 10:06:03 +01:00
committed by andy.boot
parent e0eaeccc0b
commit eeb686562d
9 changed files with 73 additions and 25 deletions
+10 -1
View File
@@ -24,6 +24,15 @@ pub fn build_cli() -> Command {
.help("Number of threads to use")
.num_args(1)
)
.arg(
Arg::new("config")
.long("config")
.help("Specify a config file to use")
.value_name("FILE")
.value_hint(clap::ValueHint::FilePath)
.value_parser(value_parser!(String))
.num_args(1)
)
.arg(
Arg::new("number_of_lines")
.short('n')
@@ -47,7 +56,7 @@ pub fn build_cli() -> Command {
.value_name("PATH")
.value_hint(clap::ValueHint::AnyPath)
.action(clap::ArgAction::Append)
.help("Exclude any file or directory with this name"),
.help("Exclude any file or directory with this path"),
)
.arg(
Arg::new("ignore_all_in_file")
+23 -6
View File
@@ -15,7 +15,6 @@ pub static DAY_SECONDS: i64 = 24 * 60 * 60;
#[derive(Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
#[serde(deny_unknown_fields)]
pub struct Config {
pub display_full_paths: Option<bool>,
pub display_apparent_size: Option<bool>,
@@ -254,12 +253,30 @@ fn get_config_locations(base: &Path) -> Vec<PathBuf> {
]
}
pub fn get_config() -> Config {
if let Some(home) = directories::BaseDirs::new() {
for path in get_config_locations(home.home_dir()) {
pub fn get_config(conf_path: Option<String>) -> Config {
match conf_path {
Some(path_str) => {
let path = Path::new(&path_str);
if path.exists() {
if let Ok(config) = Config::from_config_file(path) {
return config;
match Config::from_config_file(&path) {
Ok(config) => return config,
Err(e) => eprintln!(
"Ignoring invalid config file ({}): {:?}",
&path.display(),
e
),
}
}
eprintln!("Config file {:?} doesn't exists", &path.display());
}
None => {
if let Some(home) = directories::BaseDirs::new() {
for path in get_config_locations(home.home_dir()) {
if path.exists() {
if let Ok(config) = Config::from_config_file(&path) {
return config;
}
}
}
}
}
+1 -1
View File
@@ -112,7 +112,7 @@ fn get_regex_value(maybe_value: Option<ValuesRef<String>>) -> Vec<Regex> {
fn main() {
let options = build_cli().get_matches();
let config = get_config();
let config = get_config(options.get_one::<String>("config").cloned());
let errors = RuntimeErrors::default();
let error_listen_for_ctrlc = Arc::new(Mutex::new(errors));