From d7a84d547152a965aa7b561fbbf0f39cf8082f6a Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Sun, 2 Mar 2025 15:03:49 -0700 Subject: [PATCH] duplicate config check, change default config if Windows --- src/config.rs | 21 ++++++++++++++++++--- src/main.rs | 10 +++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 108126d..e760f4a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -118,15 +118,30 @@ pub fn read_config(paths: Vec, refractr: &common::Refractr) -> Vec panic!("refractr: cannot get absolute path of config file: {}", path.as_path().display()), + Ok(abs) => abs.to_string_lossy().to_string() + }, file: match fs::metadata(&path) { - Err(_) => panic!("refractr: cannot obtain metadata: {}", path.as_path().display()), + Err(_) => panic!("refractr: cannot obtain metadata for config file: {}", path.as_path().display()), Ok(metadata) => metadata }, config: verify_config(toml::from_str(&data).unwrap()) }; - config_files.push(config_file); + let mut dup = false; + for i in &config_files { + if i.path == config_file.path { + eprintln!("refractr: warning: skipping config file \"{}\" as it was already read", path.as_path().display()); + dup = true; + break; + } + } + + if !dup { + config_files.push(config_file); + } + } return config_files; diff --git a/src/main.rs b/src/main.rs index 98050f5..fa84cda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use std::process; #[command(about = "An automated push/pull/clone utility for mirroring Git repositories")] #[command(long_about = None)] struct Args { - #[arg(short, long, help = "Specify a config file", default_value = "/etc/refractr/config.toml")] + #[arg(short, long, help = "Specify a config file", default_value = get_config_default())] config: Vec, #[arg(short, long, help = "Specify the level of verbosity", action = clap::ArgAction::Count)] @@ -21,6 +21,14 @@ struct Args { create: bool, } +fn get_config_default() -> &'static str { + if cfg!(windows) { + "C:\\ProgramData\\refractr\\config.toml" + } else { + "/etc/refractr/config.toml" + } +} + fn main() { let args = Args::parse(); let refractr = common::Refractr {