Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Jun 10, 2023
1 parent 0965c1a commit b4671ee
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 59 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ipk-verify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ tempfile = "3.5.0"
path-slash = "0.2.1"
prettytable-rs = "^0.10"
semver = "1.0.17"
is-terminal = "0.4.7"
term = "0.7.0"

[dependencies.clap]
version = "4.2.4"
Expand Down
92 changes: 35 additions & 57 deletions ipk-verify/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ use std::iter;
use std::path::PathBuf;

use clap::Parser;
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
use prettytable::{color, Attr, Cell, Row, Table};
use is_terminal::IsTerminal;
use prettytable::{Cell, Row, Table};
use semver::VersionReq;
use serde::Deserialize;

use common::{
BinVerifyResult, BinaryInfo, Firmware, LibraryInfo, VerifyResult, VerifyWithFirmware,
};

use crate::output::ReportOutput;

mod component;
mod links;
mod output;
mod package;

#[derive(Parser, Debug)]
Expand All @@ -24,8 +27,8 @@ struct Args {
packages: Vec<PathBuf>,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(short, long, value_enum, default_value = "plain")]
format: OutputFormat,
#[arg(short, long, value_enum)]
format: Option<OutputFormat>,
#[arg(long)]
fw_releases: Option<VersionReq>,
#[arg(short, long, action = clap::ArgAction::Count)]
Expand All @@ -39,13 +42,26 @@ enum OutputFormat {
Plain,
}

impl Args {
fn report_output(&self) -> Box<dyn ReportOutput> {
return if let Some(path) = &self.output {
Box::new(File::create(path).unwrap())
} else {
Box::new(std::io::stdout())
};
}
}

fn main() {
let args = Args::parse();
let to_file: bool = args.output.is_some();
let mut output: Box<dyn Write> = if let Some(path) = args.output {
Box::new(File::create(path).unwrap())
let mut output = args.report_output();
let format = if let Some(format) = args.format {
format
} else if std::io::stdout().is_terminal() {
OutputFormat::Terminal
} else {
Box::new(std::io::stdout())
OutputFormat::Plain
};
let firmwares: Vec<Firmware> = Firmware::list(Firmware::data_path())
.unwrap()
Expand Down Expand Up @@ -94,7 +110,7 @@ fn main() {
print_component_results(
results.iter().map(|(fw, res)| (*fw, &res.app)).collect(),
&mut output,
&args.format,
&format,
)
.unwrap();
for idx in 0..result.services.len() {
Expand All @@ -113,53 +129,22 @@ fn main() {
.map(|(fw, res)| (*fw, res.services.get(idx).unwrap()))
.collect(),
&mut output,
&args.format,
&format,
)
.unwrap();
}
}
}

fn print_component_results<Output>(
fn print_component_results(
results: Vec<(&Firmware, &ComponentVerifyResult)>,
out: &mut Output,
out: &mut Box<dyn ReportOutput>,
out_fmt: &OutputFormat,
) -> Result<(), Error>
where
Output: Write + ?Sized,
{
) -> Result<(), Error> {
let (_, result) = *results.first().unwrap();
fn result_cell(result: &BinVerifyResult) -> Cell {
return if result.is_good() {
let mut cell = Cell::new("OK");
cell.style(Attr::ForegroundColor(color::BRIGHT_GREEN));
cell
} else {
let mut cell = Cell::new("NG");
cell.style(Attr::ForegroundColor(color::BRIGHT_RED));
cell
};
}
if let Some(exe) = &result.exe {
let mut table = Table::new();
match out_fmt {
OutputFormat::Markdown => {
table.set_format(
FormatBuilder::new()
.column_separator('|')
.borders('|')
.padding(1, 1)
.separator(LinePosition::Title, LineSeparator::new('-', '|', '|', '|'))
.build(),
);
}
OutputFormat::Terminal => {
table.set_format(*prettytable::format::consts::FORMAT_BOX_CHARS);
}
OutputFormat::Plain => {
table.set_format(*prettytable::format::consts::FORMAT_DEFAULT);
}
}
table.set_format(out.table_format(out_fmt));
table.set_titles(Row::from_iter(
iter::once(String::new()).chain(
results
Expand All @@ -172,7 +157,7 @@ where
.chain(
results
.iter()
.map(|(_, result)| result_cell(result.exe.as_ref().unwrap())),
.map(|(_, result)| out.result_cell(result.exe.as_ref().unwrap(), out_fmt)),
)
.collect(),
));
Expand All @@ -184,22 +169,15 @@ where
};
table.add_row(Row::new(
iter::once(name)
.chain(
results
.iter()
.map(|(_, result)| result_cell(&result.libs.get(idx).unwrap().1)),
)
.chain(results.iter().map(|(_, result)| {
out.result_cell(&result.libs.get(idx).unwrap().1, out_fmt)
}))
.collect(),
));
}
if *out_fmt == OutputFormat::Terminal {
table.print_tty(true)?;
} else {
table.print(out)?;
}
out.write_all(b"\n")?;
out.print_table(&table)?;
} else {
println!("Skip because this component is not native");
out.write_fmt(format_args!("Skip because this component is not native\n"))?;
}
return Ok(());
}
Expand Down
69 changes: 69 additions & 0 deletions ipk-verify/src/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::fs::File;
use std::io::{Error, Stdout, Write};

use crate::OutputFormat;
use common::{BinVerifyResult, VerifyResult};
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator, TableFormat};
use prettytable::{Cell, Table};
use term::{color, Attr};

pub trait PrintTable {
fn result_cell(&self, result: &BinVerifyResult, out_fmt: &OutputFormat) -> Cell {
return if result.is_good() {
let mut cell = Cell::new(if *out_fmt == OutputFormat::Markdown {
":ok:"
} else {
"OK"
});
cell.style(Attr::ForegroundColor(color::BRIGHT_GREEN));
cell
} else {
let mut cell = Cell::new(if *out_fmt == OutputFormat::Markdown {
":x:"
} else {
"FAIL"
});
cell.style(Attr::ForegroundColor(color::BRIGHT_RED));
cell
};
}

fn table_format(&self, out_fmt: &OutputFormat) -> TableFormat {
match out_fmt {
OutputFormat::Markdown => FormatBuilder::new()
.column_separator('|')
.borders('|')
.padding(1, 1)
.separator(LinePosition::Title, LineSeparator::new('-', '|', '|', '|'))
.build(),
OutputFormat::Terminal => *prettytable::format::consts::FORMAT_BOX_CHARS,
OutputFormat::Plain => *prettytable::format::consts::FORMAT_DEFAULT,
}
}

fn print_table(&mut self, table: &Table) -> Result<(), Error>;

fn print_details(&mut self, );
}

pub trait ReportOutput: PrintTable + Write {}

impl PrintTable for Stdout {
fn print_table(&mut self, table: &Table) -> Result<(), Error> {
table.print_tty(false)?;
println!("\n");
return Ok(());
}
}

impl PrintTable for File {
fn print_table(&mut self, table: &Table) -> Result<(), Error> {
table.print(self)?;
self.write_all(b"\n")?;
return Ok(());
}
}

impl ReportOutput for Stdout {}

impl ReportOutput for File {}

0 comments on commit b4671ee

Please sign in to comment.