feat: add json output for pgp card list

This commit is contained in:
2021-07-11 10:12:40 +08:00
parent 41724fd08e
commit cdf14ff1b6

View File

@@ -1,6 +1,7 @@
use clap::{ArgMatches, SubCommand, App};
use clap::{ArgMatches, SubCommand, App, Arg};
use crate::cmd::{Command, CommandError};
use openpgp_card::{OpenPGPCard, DecryptMe, Hash};
use std::collections::BTreeMap;
pub struct CommandImpl;
@@ -9,10 +10,16 @@ impl Command for CommandImpl {
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("OpenPGP Card List subcommand")
// .arg(Arg::with_name("json").long("json").help("JSON output"))
.arg(Arg::with_name("json").long("json").help("JSON output"))
}
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");
if json_output {
rust_util::util_msg::set_logger_std_out(false);
}
let mut json = BTreeMap::new();
match OpenPGPCard::list_cards() {
Err(e) => return simple_error!("Failed to list OpenPGP cards: {}", e),
Ok(list) => {
@@ -22,18 +29,31 @@ impl Command for CommandImpl {
if let Ok(fingerprints) = card.get_fingerprints() {
if let Some(a) = fingerprints.authentication() {
information!("Authentication fingerprint: {}", a);
if json_output {
json.insert("authentication_fingerprint", a.to_string());
}
}
if let Some(d) = fingerprints.decryption() {
information!("Decryption fingerprint: {}", d);
information!("Encryption fingerprint: {}", d);
if json_output {
json.insert("encryption_fingerprint", d.to_string());
}
}
if let Some(s) = fingerprints.signature() {
information!("Signature fingerprint: {}", s);
if json_output {
json.insert("signature_fingerprint", s.to_string());
}
}
}
}
}
}
if json_output {
println!("{}", serde_json::to_string_pretty(&json).unwrap());
}
if let Ok(pass) = std::env::var("PASS") {
if let Ok(list) = OpenPGPCard::list_cards() {
// pw1_81 for signature