duplicate config check, change default config if Windows

This commit is contained in:
Bryson Steck 2025-03-02 15:03:49 -07:00
parent 03aef02abc
commit d7a84d5471
2 changed files with 27 additions and 4 deletions

View file

@ -118,17 +118,32 @@ pub fn read_config(paths: Vec<PathBuf>, refractr: &common::Refractr) -> Vec<Conf
}
let config_file = ConfigFile {
path: String::from(path.to_string_lossy()),
path: match fs::canonicalize(&path) {
Err(_) => 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())
};
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;
}

View file

@ -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<PathBuf>,
#[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 {