pub mod readers;
pub mod status;
use self::{readers::ReadersCmd, status::StatusCmd};
use crate::terminal;
use clap::Parser;
use std::{env, process::exit};
use termcolor::ColorChoice;
use yubikey::{Serial, YubiKey};
#[derive(Debug, Parser)]
pub struct YubiKeyCli {
#[clap(short = 's', long = "serial")]
pub serial: Option<Serial>,
#[clap(subcommand)]
pub command: Commands,
}
impl YubiKeyCli {
pub fn run(&self) {
terminal::set_color_choice(ColorChoice::Auto);
if env::var("RUST_LOG").is_ok() {
env_logger::builder().format_timestamp(None).init();
}
self.command.run(self.yubikey_init())
}
fn yubikey_init(&self) -> YubiKey {
match self.serial {
Some(serial) => YubiKey::open_by_serial(serial).unwrap_or_else(|e| {
status_err!("couldn't open YubiKey (serial #{}): {}", serial, e);
exit(1);
}),
None => YubiKey::open().unwrap_or_else(|e| {
status_err!("couldn't open default YubiKey: {}", e);
exit(1);
}),
}
}
}
#[derive(Debug, Parser)]
pub enum Commands {
#[clap(about = "display version information")]
Version(VersionOpts),
#[clap(about = "list detected readers")]
Readers(ReadersCmd),
#[clap(about = "show yubikey status")]
Status(StatusCmd),
}
impl Commands {
pub fn run(&self, yubikey: YubiKey) {
match self {
Commands::Version(version) => version.run(),
Commands::Readers(list) => list.run(),
Commands::Status(status) => status.run(yubikey),
}
}
}
#[derive(Debug, Parser)]
pub struct VersionOpts {}
impl VersionOpts {
pub fn run(&self) {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
}
}