Skip to content

Commit

Permalink
json_only arg to avoid defining an extra function
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh committed Jan 16, 2025
1 parent cd7fec5 commit 415f3e4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
2 changes: 1 addition & 1 deletion crates/omnix-ci/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub async fn check_nix_version(cfg: &OmConfig, nix_info: &NixInfo) -> anyhow::Re
let checks = omnix_health
.nix_version
.check(nix_info, Some(&cfg.flake_url));
let exit_code = NixHealth::print_report_returning_exit_code(&checks).await?;
let exit_code = NixHealth::print_report_returning_exit_code(&checks, false).await?;

if exit_code != 0 {
std::process::exit(exit_code);
Expand Down
8 changes: 2 additions & 6 deletions crates/omnix-cli/src/command/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ impl HealthCommand {
println!("{}", NixHealth::schema()?);
return Ok(());
}
let checks_map = run_all_checks_with(self.flake_url.clone()).await?;
let exit_code = if self.json {
NixHealth::print_json_report_returning_exit_code(&checks_map).await?
} else {
NixHealth::print_report_returning_exit_code(&checks_map).await?
};
let checks = run_all_checks_with(self.flake_url.clone()).await?;
let exit_code = NixHealth::print_report_returning_exit_code(&checks, self.json).await?;
if exit_code != 0 {
std::process::exit(exit_code);
}
Expand Down
22 changes: 7 additions & 15 deletions crates/omnix-health/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,25 @@ impl NixHealth {
.collect()
}

pub async fn print_json_report_returning_exit_code(
pub async fn print_report_returning_exit_code(
checks_map: &HashMap<&'static str, Check>,
json_only: bool,
) -> anyhow::Result<i32> {
let mut res = AllChecksResult::new();
for check in checks_map.values() {
if !json_only {
check.tracing_log().await?;
}
if !check.result.green() {
res.register_failure(check.required);
};
}

let code = res.report();
println!("{}", serde_json::to_string_pretty(checks_map)?);
Ok(code)
}

pub async fn print_report_returning_exit_code(
checks_map: &HashMap<&'static str, Check>,
) -> anyhow::Result<i32> {
let mut res = AllChecksResult::new();
for check in checks_map.values() {
check.tracing_log().await?;
if !check.result.green() {
res.register_failure(check.required);
};
if json_only {
println!("{}", serde_json::to_string_pretty(checks_map)?);
}

let code = res.report();
Ok(code)
}

Expand Down

0 comments on commit 415f3e4

Please sign in to comment.