Skip to content

Commit

Permalink
Merge pull request #33 from VolumeGraphics/config-validate-command
Browse files Browse the repository at this point in the history
Config validate command
  • Loading branch information
ChrisRega committed Mar 31, 2023
2 parents b56788b + 7adde47 commit 54ba160
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,30 @@ pub fn get_schema() -> Result<String, Error> {
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<Path>) -> 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::*;
Expand Down
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,6 +22,9 @@ enum Commands {

/// Export the JsonSchema for the config files
Schema,

/// Validate config yaml
Validate { compare_config: String },
}

#[derive(Parser)]
Expand Down Expand Up @@ -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);
}
}
};
}

0 comments on commit 54ba160

Please sign in to comment.