cli: add status command

Provides equivalent functionality to `yubico-piv-tool`
This commit is contained in:
Tony Arcieri
2019-12-09 17:51:05 -08:00
parent 283e6fe363
commit 78d5f33695
12 changed files with 189 additions and 34 deletions
+27 -3
View File
@@ -1,8 +1,13 @@
//! List detected readers
use crate::terminal::STDOUT;
use gumdrop::Options;
use std::process::exit;
use yubikey_piv::readers::Readers;
use std::{
io::{self, Write},
process::exit,
};
use termcolor::{ColorSpec, StandardStreamLock, WriteColor};
use yubikey_piv::{Readers, Serial};
/// The `readers` subcommand
#[derive(Debug, Options)]
@@ -26,6 +31,9 @@ impl ReadersCmd {
exit(1);
}
let mut s = STDOUT.lock();
s.reset().unwrap();
for (i, reader) in readers_iter.enumerate() {
let name = reader.name();
let mut yubikey = match reader.open() {
@@ -34,7 +42,23 @@ impl ReadersCmd {
};
let serial = yubikey.serial();
println!("{}: {} (serial: {})", i + 1, name, serial);
self.print_reader(&mut s, i + 1, &name, serial).unwrap();
}
}
/// Print a reader
fn print_reader(
&self,
stream: &mut StandardStreamLock<'_>,
index: usize,
name: &str,
serial: Serial,
) -> Result<(), io::Error> {
stream.set_color(ColorSpec::new().set_bold(true))?;
write!(stream, "{:>3}:", index)?;
stream.reset()?;
writeln!(stream, " {} (serial: {})", name, serial)?;
stream.flush()?;
Ok(())
}
}