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:
+32
-13
@@ -1,33 +1,50 @@
|
|||||||
|
use crate::config::VeilConfig;
|
||||||
|
use anyhow::{Context, Result as AnyhowResult};
|
||||||
use std::{
|
use std::{
|
||||||
fs::{File, create_dir_all, read_to_string},
|
fs::{File, create_dir_all, read_to_string},
|
||||||
io::{Error, ErrorKind, Result, Write},
|
io::{Error, ErrorKind, Result, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct VeilFile;
|
#[derive(Debug)]
|
||||||
|
pub struct VeilFile {
|
||||||
|
pub config: VeilConfig,
|
||||||
|
}
|
||||||
|
|
||||||
impl VeilFile {
|
impl VeilFile {
|
||||||
pub fn load(debug: bool) -> Option<PathBuf> {
|
pub fn load(debug: bool) -> AnyhowResult<Self> {
|
||||||
|
let mut path: Option<PathBuf> = None;
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
let debug_path: &Path = Path::new("./local-config/veil.yaml");
|
let debug_path: &Path = Path::new("./local-config/veil.yaml");
|
||||||
if debug_path.exists() {
|
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");
|
||||||
if let Some(base) = dirs::config_dir() {
|
for name in &candidates {
|
||||||
let veil_dir: PathBuf = base.join("veil");
|
let candidate: PathBuf = veil_dir.join(name);
|
||||||
for name in &candidates {
|
if candidate.exists() {
|
||||||
let candidate: PathBuf = veil_dir.join(name);
|
path = Some(candidate);
|
||||||
if candidate.exists() {
|
}
|
||||||
return 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<()> {
|
pub fn init_config(debug: bool) -> Result<()> {
|
||||||
@@ -67,4 +84,6 @@ impl VeilFile {
|
|||||||
println!("✅ Created new config at {}", dest_file.display());
|
println!("✅ Created new config at {}", dest_file.display());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn list_config() {}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-2
@@ -1,12 +1,20 @@
|
|||||||
use std::io::Result;
|
use std::{io::Result, process::exit};
|
||||||
use veil_rs::{
|
use veil_rs::{
|
||||||
cli::{Cli, Cmds},
|
cli::{Cli, Cmds},
|
||||||
config::VeilFile,
|
config::{VeilConfig, VeilFile},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let args: Cli = Cli::get_args();
|
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 {
|
match args.command {
|
||||||
Cmds::Init => VeilFile::init_config(args.debug),
|
Cmds::Init => VeilFile::init_config(args.debug),
|
||||||
Cmds::Sync => Ok(()),
|
Cmds::Sync => Ok(()),
|
||||||
|
|||||||
Reference in New Issue
Block a user