feat: pgp card admin

This commit is contained in:
2022-03-27 15:30:35 +08:00
parent 4804f30b69
commit 6419df04f2

View File

@@ -1,4 +1,5 @@
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use openpgp_card::Sex;
use rust_util::util_clap::{Command, CommandError}; use rust_util::util_clap::{Command, CommandError};
pub struct CommandImpl; pub struct CommandImpl;
@@ -9,16 +10,55 @@ impl Command for CommandImpl {
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("OpenPGP Card Admin subcommand") SubCommand::with_name(self.name()).about("OpenPGP Card Admin subcommand")
.arg(Arg::with_name("pin").short("p").long("pin").takes_value(true).default_value("12345678").help("OpenPGP card admin pin")) .arg(Arg::with_name("pin").short("p").long("pin").takes_value(true).default_value("12345678").help("OpenPGP card admin pin"))
.arg(Arg::with_name("name").short("n").long("name").takes_value(true).required(false).help("Set name"))
.arg(Arg::with_name("url").long("url").takes_value(true).required(false).help("Set URL"))
.arg(Arg::with_name("lang").long("lang").takes_value(true).required(false).help("Set lang"))
.arg(Arg::with_name("sex").long("sex").takes_value(true).required(false).help("Set sex, f or m"))
} }
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let pin = opt_value_result!(sub_arg_matches.value_of("pin"), "Pass must be assigned"); let pin = opt_value_result!(sub_arg_matches.value_of("pin"), "Pass must be assigned");
if pin.len() < 8 { return simple_error!("Admin pin length:{}, must >= 8!", pin.len()); } if pin.len() < 8 { return simple_error!("Admin pin length:{}, must >= 8!", pin.len()); }
let _card_admin = crate::pgpcardutil::get_card_admin(pin)?; let card_admin = crate::pgpcardutil::get_card_admin(pin)?;
success!("Admin pin verify success!");
information!("Admin pin verify success!"); if let Some(name) = sub_arg_matches.value_of("name") {
// card_admin.get_aid() information!("Set name to: {}", name);
let response = opt_result!(card_admin.set_name(name), "Set name failed: {}");
success!("Set name success: {:?}", response);
}
if let Some(url) = sub_arg_matches.value_of("url") {
information!("Set URL to: {}", url);
let response = opt_result!(card_admin.set_url(url), "Set URL failed: {}");
success!("Set URL success: {:?}", response);
}
if let Some(lang) = sub_arg_matches.value_of("lang") {
information!("Set lang to: {}", lang);
let response = opt_result!(card_admin.set_lang(lang), "Set lang failed: {}");
success!("Set lang success: {:?}", response);
}
if let Some(sex) = sub_arg_matches.value_of("sex") {
let sex = sex.to_lowercase();
let s = if "f" == sex || "female" == sex {
Some(Sex::Female)
} else if "m" == sex || "male" == sex {
Some(Sex::Male)
} else {
warning!("Invalid sex: {}", sex);
None
};
if let Some(s) = s {
information!("Set sex to: {:?}", s);
let response = opt_result!(card_admin.set_sex(s), "Set sex failed: {}");
success!("Set sex success: {:?}", response);
}
}
// TODO Upload Key!
Ok(None) Ok(None)
} }