cli: add status command
Provides equivalent functionality to `yubico-piv-tool`
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
//! Print device status
|
||||
|
||||
use crate::terminal::STDOUT;
|
||||
use gumdrop::Options;
|
||||
use std::io::{self, Write};
|
||||
use termcolor::{ColorSpec, StandardStreamLock, WriteColor};
|
||||
use yubikey_piv::YubiKey;
|
||||
|
||||
// String to use for `None`
|
||||
const NONE_STR: &str = "<none>";
|
||||
|
||||
/// The `status` subcommand
|
||||
#[derive(Debug, Options)]
|
||||
pub struct StatusCmd {}
|
||||
|
||||
impl StatusCmd {
|
||||
/// Run the `status` subcommand
|
||||
pub fn run(&self, mut yk: YubiKey) {
|
||||
let mut s = STDOUT.lock();
|
||||
s.reset().unwrap();
|
||||
|
||||
self.attr(&mut s, "version", yk.version()).unwrap();
|
||||
self.attr(&mut s, "serial", yk.serial()).unwrap();
|
||||
|
||||
if let Ok(chuid) = yk.chuid() {
|
||||
self.attr(&mut s, "CHUID", chuid).unwrap();
|
||||
} else {
|
||||
self.attr(&mut s, "CHUID", NONE_STR).unwrap();
|
||||
}
|
||||
|
||||
if let Ok(chuid) = yk.cccid() {
|
||||
self.attr(&mut s, "CCC", chuid).unwrap();
|
||||
} else {
|
||||
self.attr(&mut s, "CCC", NONE_STR).unwrap();
|
||||
}
|
||||
|
||||
self.attr(&mut s, "PIN retries", yk.get_pin_retries().unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Print a status attribute
|
||||
fn attr(
|
||||
&self,
|
||||
stream: &mut StandardStreamLock<'_>,
|
||||
name: &str,
|
||||
value: impl ToString,
|
||||
) -> Result<(), io::Error> {
|
||||
stream.set_color(ColorSpec::new().set_bold(true))?;
|
||||
write!(stream, "{:>12}:", name)?;
|
||||
stream.reset()?;
|
||||
writeln!(stream, " {}", value.to_string())?;
|
||||
stream.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user