chore: reorg code

This commit is contained in:
2022-03-27 12:40:20 +08:00
parent b9dbcb8f80
commit 8ab2126034
11 changed files with 30 additions and 36 deletions

View File

@@ -17,8 +17,8 @@ impl Command for CommandImpl {
fn name(&self) -> &str { "pgp" } fn name(&self) -> &str { "pgp" }
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("OpenPGP Card List subcommand") SubCommand::with_name(self.name()).about("OpenPGP subcommand")
.arg(Arg::with_name("in").short("i").long("in").takes_value(true).help("File input")) .arg(Arg::with_name("in").short("i").long("in").takes_value(true).help("File input, *.pgp or *.asc"))
.arg(Arg::with_name("detail").long("detail").help("Detail output")) .arg(Arg::with_name("detail").long("detail").help("Detail output"))
.arg(Arg::with_name("json").long("json").help("JSON output")) .arg(Arg::with_name("json").long("json").help("JSON output"))
} }
@@ -27,10 +27,7 @@ impl Command for CommandImpl {
let in_file = sub_arg_matches.value_of("in"); let in_file = sub_arg_matches.value_of("in");
let show_detail = sub_arg_matches.is_present("detail"); let show_detail = sub_arg_matches.is_present("detail");
let in_file = match in_file { let in_file = opt_value_result!(in_file, "Input file must assined");
Some(i) => i,
None => return simple_error!("Input file must assined"),
};
let in_file_bytes = opt_result!(std::fs::read(in_file), "Read file: {}, failed: {}", in_file); let in_file_bytes = opt_result!(std::fs::read(in_file), "Read file: {}, failed: {}", in_file);
let p = PacketParser::from_bytes(&in_file_bytes); let p = PacketParser::from_bytes(&in_file_bytes);

View File

@@ -15,9 +15,7 @@ impl Command for CommandImpl {
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let json_output = sub_arg_matches.is_present("json"); let json_output = sub_arg_matches.is_present("json");
if json_output { if json_output { rust_util::util_msg::set_logger_std_out(false); }
rust_util::util_msg::set_logger_std_out(false);
}
let mut json = BTreeMap::new(); let mut json = BTreeMap::new();
match OpenPGPCard::list_cards() { match OpenPGPCard::list_cards() {

View File

@@ -11,10 +11,10 @@ use crate::fido::{U2fV2Challenge, U2fRegistrationData};
pub struct CommandImpl; pub struct CommandImpl;
impl Command for CommandImpl { impl Command for CommandImpl {
fn name(&self) -> &str { "register" } fn name(&self) -> &str { "u2f-register" }
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Register subcommand") SubCommand::with_name(self.name()).about("FIDO U2F Register subcommand")
.arg(Arg::with_name("app-id").short("a").long("app-id").default_value("https://example.com").help("App id")) .arg(Arg::with_name("app-id").short("a").long("app-id").default_value("https://example.com").help("App id"))
.arg(Arg::with_name("timeout").short("t").long("timeout").default_value("10").help("Timeout in seconds")) .arg(Arg::with_name("timeout").short("t").long("timeout").default_value("10").help("Timeout in seconds"))
.arg(Arg::with_name("json").long("json").help("JSON output")) .arg(Arg::with_name("json").long("json").help("JSON output"))
@@ -22,9 +22,8 @@ impl Command for CommandImpl {
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let json_output = sub_arg_matches.is_present("json"); let json_output = sub_arg_matches.is_present("json");
if json_output { if json_output { rust_util::util_msg::set_logger_std_out(false); }
rust_util::util_msg::set_logger_std_out(false);
}
let app_id = sub_arg_matches.value_of("app-id").unwrap(); let app_id = sub_arg_matches.value_of("app-id").unwrap();
let timeout_ms = match sub_arg_matches.value_of("timeout").unwrap().parse::<u32>() { let timeout_ms = match sub_arg_matches.value_of("timeout").unwrap().parse::<u32>() {
Ok(t) => (t * 1000) as u64, Ok(t) => (t * 1000) as u64,

View File

@@ -11,9 +11,9 @@ use crate::fido::U2fV2Challenge;
pub struct CommandImpl; pub struct CommandImpl;
impl Command for CommandImpl { impl Command for CommandImpl {
fn name(&self) -> &str { "sign" } fn name(&self) -> &str { "u2f-sign" }
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Sign subcommand") SubCommand::with_name(self.name()).about("FIDO U2F Sign subcommand")
.arg(Arg::with_name("app-id").short("a").long("app-id").default_value("https://example.com").help("App id")) .arg(Arg::with_name("app-id").short("a").long("app-id").default_value("https://example.com").help("App id"))
.arg(Arg::with_name("timeout").short("t").long("timeout").default_value("10").help("Timeout in seconds")) .arg(Arg::with_name("timeout").short("t").long("timeout").default_value("10").help("Timeout in seconds"))
.arg(Arg::with_name("key-handle").short("k").long("key-handle").takes_value(true).multiple(true).help("Key handle")) .arg(Arg::with_name("key-handle").short("k").long("key-handle").takes_value(true).multiple(true).help("Key handle"))

View File

@@ -3,17 +3,17 @@ extern crate rust_util;
mod fido; mod fido;
mod digest; mod digest;
mod register; mod cmd_register;
mod sign; mod cmd_sign;
mod pgp; mod cmd_pgp;
mod pgpcardutil; mod pgpcardutil;
mod pgpcardlist; mod cmd_pgpcardlist;
mod pgpcardsign; mod cmd_pgpcardsign;
mod pgpcarddecrypt; mod cmd_pgpcarddecrypt;
mod piv; mod cmd_piv;
mod pivsign; mod cmd_pivsign;
mod chall; mod cmd_chall;
mod challconfig; mod cmd_challconfig;
use clap::{App, AppSettings, ArgMatches}; use clap::{App, AppSettings, ArgMatches};
use rust_util::util_clap::{CommandError, Command}; use rust_util::util_clap::{CommandError, Command};
@@ -38,16 +38,16 @@ fn main() {
fn inner_main() -> CommandError { fn inner_main() -> CommandError {
let commands: Vec<Box<dyn Command>> = vec![ let commands: Vec<Box<dyn Command>> = vec![
Box::new(register::CommandImpl), Box::new(cmd_register::CommandImpl),
Box::new(sign::CommandImpl), Box::new(cmd_sign::CommandImpl),
Box::new(pgp::CommandImpl), Box::new(cmd_pgp::CommandImpl),
Box::new(pgpcardlist::CommandImpl), Box::new(cmd_pgpcardlist::CommandImpl),
Box::new(pgpcardsign::CommandImpl), Box::new(cmd_pgpcardsign::CommandImpl),
Box::new(pgpcarddecrypt::CommandImpl), Box::new(cmd_pgpcarddecrypt::CommandImpl),
Box::new(piv::CommandImpl), Box::new(cmd_piv::CommandImpl),
Box::new(pivsign::CommandImpl), Box::new(cmd_pivsign::CommandImpl),
Box::new(chall::CommandImpl), Box::new(cmd_chall::CommandImpl),
Box::new(challconfig::CommandImpl), Box::new(cmd_challconfig::CommandImpl),
]; ];
let mut app = App::new(env!("CARGO_PKG_NAME")) let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))