diff --git a/README.md b/README.md index 496c062..cc9b95c 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,7 @@ rules: - Add external checking - Add and adjust reports for file metadata comparison and external checking - Add csv header comparison. Previously, the report only marks the differences but not returning any error. +- Added config yaml validation command to check whether file can be loaded or not ### 0.2.4 - add check for row lines of both compared csv files, and throw error if they are unequal diff --git a/src/lib.rs b/src/lib.rs index b42678c..7a41f1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -346,6 +346,30 @@ pub fn get_schema() -> Result { Ok(serde_json::to_string_pretty(&schema)?) } +/// Try to load config yaml and check whether it is a valid one. Returns true if file can be loaded, otherwise false +pub fn validate_config(config_file: impl AsRef) -> bool { + let config_file = config_file.as_ref(); + let config_file_string = config_file.to_string_lossy(); + if !config_file.exists() { + error!("Could not find config file: {config_file_string}"); + return false; + } + + match ConfigurationFile::from_file(config_file) { + Ok(_) => { + info!("Config file {config_file_string} loaded successfully"); + true + } + Err(e) => { + error!( + "Could not load config file {config_file_string}: {}", + e.to_string() + ); + false + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index db2ed29..885008b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use clap::Parser; -use havocompare::get_schema; +use havocompare::{compare_folders, get_schema, validate_config}; use tracing::Level; use tracing_subscriber::FmtSubscriber; @@ -22,6 +22,9 @@ enum Commands { /// Export the JsonSchema for the config files Schema, + + /// Validate config yaml + Validate { compare_config: String }, } #[derive(Parser)] @@ -65,13 +68,19 @@ fn main() { report_config, } => { let result = - havocompare::compare_folders(nominal, actual, compare_config, report_config) - .unwrap_or(false); + compare_folders(nominal, actual, compare_config, report_config).unwrap_or(false); if result { std::process::exit(0); } else { std::process::exit(1); } } + Commands::Validate { compare_config } => { + if validate_config(compare_config) { + std::process::exit(0); + } else { + std::process::exit(1); + } + } }; }