diff --git a/src/config/loader.rs b/src/config/loader.rs index 3508c62..dfbe0b2 100644 --- a/src/config/loader.rs +++ b/src/config/loader.rs @@ -1,33 +1,50 @@ +use crate::config::VeilConfig; +use anyhow::{Context, Result as AnyhowResult}; use std::{ fs::{File, create_dir_all, read_to_string}, io::{Error, ErrorKind, Result, Write}, path::{Path, PathBuf}, }; -pub struct VeilFile; +#[derive(Debug)] +pub struct VeilFile { + pub config: VeilConfig, +} impl VeilFile { - pub fn load(debug: bool) -> Option { + pub fn load(debug: bool) -> AnyhowResult { + let mut path: Option = None; + if debug { let debug_path: &Path = Path::new("./local-config/veil.yaml"); if debug_path.exists() { - return Some(debug_path.to_path_buf()); + path = Some(debug_path.to_path_buf()); } - } + } else { + let candidates: [&str; 3] = ["veil.yaml", ".veil.yaml", "veil.yml"]; - let candidates: [&str; 3] = ["veil.yaml", ".veil.yaml", "veil.yml"]; - - if let Some(base) = dirs::config_dir() { - let veil_dir: PathBuf = base.join("veil"); - for name in &candidates { - let candidate: PathBuf = veil_dir.join(name); - if candidate.exists() { - return Some(candidate); + if let Some(base) = dirs::config_dir() { + let veil_dir: PathBuf = base.join("veil"); + for name in &candidates { + let candidate: PathBuf = veil_dir.join(name); + if candidate.exists() { + path = Some(candidate); + } } } } - None + let config: VeilConfig = if let Some(p) = path { + let raw: String = + read_to_string(&p).with_context(|| format!("cannot read config file {:?}", p))?; + + serde_yaml::from_str::(&raw) + .with_context(|| format!("cannot parse YAML in {:?}", p))? + } else { + VeilConfig::new(Vec::new()) + }; + + Ok(Self { config }) } pub fn init_config(debug: bool) -> Result<()> { @@ -67,4 +84,6 @@ impl VeilFile { println!("✅ Created new config at {}", dest_file.display()); Ok(()) } + + pub fn list_config() {} } diff --git a/src/main.rs b/src/main.rs index f2c693c..9588020 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,20 @@ -use std::io::Result; +use std::{io::Result, process::exit}; use veil_rs::{ cli::{Cli, Cmds}, - config::VeilFile, + config::{VeilConfig, VeilFile}, }; fn main() -> Result<()> { let args: Cli = Cli::get_args(); + let _config: VeilConfig = match VeilFile::load(args.debug) { + Ok(v) => v.config, + Err(e) => { + eprintln!("❌ Failed to load Veil configuration:\n{:#}", e); + exit(1); + } + }; + match args.command { Cmds::Init => VeilFile::init_config(args.debug), Cmds::Sync => Ok(()),