Skip to content

Commit

Permalink
added exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Jun 13, 2023
1 parent eae4ec3 commit ed2015d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
8 changes: 7 additions & 1 deletion common/verify/src/ipk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fw_lib::Firmware;
use ipk_lib::Package;

use crate::{bin::BinVerifyResult, VerifyWithFirmware};
use crate::{bin::BinVerifyResult, VerifyResult, VerifyWithFirmware};

pub mod component;

Expand Down Expand Up @@ -37,3 +37,9 @@ impl VerifyWithFirmware<PackageVerifyResult> for Package {
};
}
}

impl VerifyResult for PackageVerifyResult {
fn is_good(&self) -> bool {
return self.app.is_good() && self.services.iter().all(|s| s.is_good());
}
}
2 changes: 1 addition & 1 deletion packages/fw-extract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Args {
#[arg(short = 'w', long)]
rewrite: bool,
#[arg(short, long, action = clap::ArgAction::Count)]
debug: u8,
verbose: u8,
}

struct FirmwareExtractor {
Expand Down
2 changes: 2 additions & 0 deletions packages/gen-manifest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct Args {
link: String,
#[arg(short, long, value_enum)]
root: Option<RootRequired>,
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
}

#[derive(Debug, Clone, PartialEq, ValueEnum)]
Expand Down
97 changes: 59 additions & 38 deletions packages/ipk-verify/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::io::{Error, Write};
use std::iter;
use std::path::PathBuf;
use std::process::exit;

use clap::{Parser, ValueEnum};
use is_terminal::IsTerminal;
Expand All @@ -26,10 +27,14 @@ struct Args {
output: Option<PathBuf>,
#[arg(short, long, value_enum)]
format: Option<OutputFormat>,
#[arg(long)]
#[arg(short, long, required_if_eq("no_summary", "true"))]
details: bool,
#[arg(short = 'S', long)]
no_summary: bool,
#[arg(short = 'r', long)]
fw_releases: Option<VersionReq>,
#[arg(short, long, action = clap::ArgAction::Count)]
debug: u8,
verbose: u8,
}

#[derive(Debug, Clone, PartialEq, ValueEnum)]
Expand Down Expand Up @@ -73,69 +78,85 @@ fn main() {
if firmwares.is_empty() {
eprintln!("No firmware found");
}
for package in args.packages {
let mut all_good = true;
for package in &args.packages {
eprintln!("Opening package {}...", package.to_string_lossy());
let package = match Package::open(&package) {
Ok(package) => package,
Err(e) => {
eprintln!(
"Failed to open {}: {e}",
package.file_name().unwrap().to_string_lossy()
);
all_good = false;
continue;
}
};
if to_file {
eprintln!("Verifying package {}...", package.id);
}
output.h2(&format!("Package {}", package.id)).unwrap();
eprintln!("Verifying package {}...", package.id);
let results: Vec<(&Firmware, PackageVerifyResult)> = firmwares
.iter()
.map(|fw| {
let verify = package.verify(&fw);
return (fw, verify);
})
.collect();
output.h2(&format!("Package {}", package.id)).unwrap();

if all_good && !results.iter().all(|(_, r)| r.is_good()) {
all_good = false;
}
let (_, result) = results.first().unwrap();
if to_file {
eprintln!(" - App {}", result.app.id);
}
output.h3(&format!("App {}", result.app.id)).unwrap();
print_component_summary(
results.iter().map(|(fw, res)| (*fw, &res.app)).collect(),
&mut output,
&format,
)
.unwrap();
print_component_details(
results.iter().map(|(fw, res)| (*fw, &res.app)).collect(),
&mut output,
)
.unwrap();
for idx in 0..result.services.len() {
if to_file {
eprintln!(" - Service {}", result.services.get(idx).unwrap().id);
}
output
.h3(&format!("Service {}", result.services.get(idx).unwrap().id))
.unwrap();
if !args.no_summary {
print_component_summary(
results
.iter()
.map(|(fw, res)| (*fw, res.services.get(idx).unwrap()))
.collect(),
results.iter().map(|(fw, res)| (*fw, &res.app)).collect(),
&mut output,
&format,
)
.unwrap();
}
if args.details {
print_component_details(
results
.iter()
.map(|(fw, res)| (*fw, res.services.get(idx).unwrap()))
.collect(),
results.iter().map(|(fw, res)| (*fw, &res.app)).collect(),
&mut output,
)
.unwrap();
}
for idx in 0..result.services.len() {
if to_file {
eprintln!(" - Service {}", result.services.get(idx).unwrap().id);
}
output
.h3(&format!("Service {}", result.services.get(idx).unwrap().id))
.unwrap();
if !args.no_summary {
print_component_summary(
results
.iter()
.map(|(fw, res)| (*fw, res.services.get(idx).unwrap()))
.collect(),
&mut output,
&format,
)
.unwrap();
}
if args.details {
print_component_details(
results
.iter()
.map(|(fw, res)| (*fw, res.services.get(idx).unwrap()))
.collect(),
&mut output,
)
.unwrap();
}
}
}
if !all_good {
exit(1);
}
}

Expand Down Expand Up @@ -190,13 +211,13 @@ fn print_component_summary(
fn print_component_details(
results: Vec<(&Firmware, &ComponentVerifyResult)>,
out: &mut Box<dyn ReportOutput>,
) -> Result<(), Error> {
) -> Result<bool, Error> {
let (_, result) = *results.first().unwrap();
out.h4(result.exe.name())?;
if results.iter().all(|r| r.1.is_good()) {
out.write_fmt(format_args!("All OK\n"))?;
return Ok(());
return Ok(true);
}
out.h4(result.exe.name())?;
for (fw, result) in &results {
if let ComponentBinVerifyResult::Failed(result) = &result.exe {
out.h5(&format!("On {}", fw.info))?;
Expand All @@ -208,7 +229,7 @@ fn print_component_details(
if !required {
continue;
}
if results.iter().all(|(fw, result)| {
if results.iter().all(|(_, result)| {
if let ComponentBinVerifyResult::Failed(_) = result.libs.get(index).unwrap().1 {
false
} else {
Expand All @@ -226,7 +247,7 @@ fn print_component_details(
}
}
}
return Ok(());
return Ok(false);
}

fn print_bin_verify_details(
Expand Down

0 comments on commit ed2015d

Please sign in to comment.