use crate::config::Config;
use ansi_term::Colour::{Green, Red};
use anyhow::{anyhow, Result};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
pub fn validate_target(target: &Path) -> Result<()> {
let target = PathBuf::from(target);
if !target.exists() {
return Err(anyhow!(
"{:?} ... {} - file/directory does not exist",
target,
Red.paint("Failed")
));
}
if target.is_dir() {
let mut files = vec![];
for entry in WalkDir::new(target) {
let entry = entry?;
if entry.path().is_file() {
let file_name = entry.path().file_name().unwrap();
if file_name.to_str().unwrap().ends_with(".yaml")
|| file_name.to_str().unwrap().ends_with(".yml")
{
let path = entry.path().to_path_buf();
files.push(path);
}
}
}
for file in files {
validate_file(&file).unwrap_or_else(|e| {
println!("{}", e);
});
}
return Ok(());
}
validate_file(&target)
}
pub fn validate_file(file: &Path) -> Result<()> {
let file = PathBuf::from(file);
let value = Config::new(&file)
.map_err(|e| anyhow!("{:?} ... {} - {}", file, Red.paint("invalid"), e))?;
value
.validate()
.map_err(|e| anyhow!("{:?} ... {} - {}", file, Red.paint("invalid"), e))?;
println!("{:?} ... {}", file, Green.paint("ok"));
Ok(())
}