1use crate::config::Config;
2use ansi_term::Colour::{Green, Red};
3use anyhow::{anyhow, Result};
4use std::path::{Path, PathBuf};
5use walkdir::WalkDir;
6
7pub fn validate_target(target: &Path) -> Result<()> {
9 let target = PathBuf::from(target);
10
11 if !target.exists() {
12 return Err(anyhow!(
13 "{:?} ... {} - file/directory does not exist",
14 target,
15 Red.paint("Failed")
16 ));
17 }
18
19 if target.is_dir() {
22 let mut files = vec![];
23 for entry in WalkDir::new(target) {
24 let entry = entry?;
25 if entry.path().is_file() {
26 if let Some(file_name) = entry.path().file_name() {
27 if let Some(name_str) = file_name.to_str() {
28 if name_str.ends_with(".yaml") || name_str.ends_with(".yml") {
29 let path = entry.path().to_path_buf();
30 files.push(path);
31 }
32 }
33 }
34 }
35 }
36
37 for file in files {
38 validate_file(&file).unwrap_or_else(|e| {
40 println!("{}", e);
41 });
42 }
43
44 return Ok(());
45 }
46
47 validate_file(&target)
49}
50
51pub fn validate_file(file: &Path) -> Result<()> {
53 let file = PathBuf::from(file);
54 let value = Config::new(&file)
55 .map_err(|e| anyhow!("{:?} ... {} - {}", file, Red.paint("invalid"), e))?;
56
57 value
58 .validate()
59 .map_err(|e| anyhow!("{:?} ... {} - {}", file, Red.paint("invalid"), e))?;
60
61 println!("{:?} ... {}", file, Green.paint("ok"));
63
64 Ok(())
65}