generated from GarandPLG/rust-flake-template
Load VeilConfig in VeilFile and handle errors
- VeilFile now stores a VeilConfig and implements Debug. - load() returns a Result<Self> that reads, parses, or creates a default config using anyhow for context. - Main function loads the configuration, prints detailed errors, and exits on failure.
This commit is contained in:
+26
-7
@@ -1,20 +1,26 @@
|
||||
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<PathBuf> {
|
||||
pub fn load(debug: bool) -> AnyhowResult<Self> {
|
||||
let mut path: Option<PathBuf> = 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"];
|
||||
|
||||
if let Some(base) = dirs::config_dir() {
|
||||
@@ -22,12 +28,23 @@ impl VeilFile {
|
||||
for name in &candidates {
|
||||
let candidate: PathBuf = veil_dir.join(name);
|
||||
if candidate.exists() {
|
||||
return Some(candidate);
|
||||
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::<VeilConfig>(&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() {}
|
||||
}
|
||||
|
||||
+10
-2
@@ -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(()),
|
||||
|
||||
Reference in New Issue
Block a user