cli: rename 'list' command to 'readers'; improve usage

There are going to be several `list` commands (e.g. `yubikey keys list`)
so this is a confusing name.

If we need more than one `readers` subcommand we can change this to be
`readers list` eventually.

Separately (in what probably should've been its own commit, mea culpa)
this adds slightly better usage.
This commit is contained in:
Tony Arcieri
2019-12-09 09:23:40 -08:00
parent fd77ba6e74
commit 55d077dd80
4 changed files with 45 additions and 18 deletions
+38 -10
View File
@@ -1,13 +1,16 @@
//! Commands of the CLI application
pub mod list;
pub mod readers;
use self::list::ListCmd;
use crate::status;
use self::readers::ReadersCmd;
use crate::status::{self, STDOUT};
use gumdrop::Options;
use std::env;
use std::process::exit;
use termcolor::ColorChoice;
use std::{
env,
io::{self, Write},
process::exit,
};
use termcolor::{ColorChoice, ColorSpec, WriteColor};
/// The `yubikey` CLI utility
#[derive(Debug, Options)]
@@ -34,9 +37,34 @@ impl YubikeyCli {
match &self.command {
Some(cmd) => cmd.run(),
None => println!("{}", Commands::usage()),
None => Self::print_usage().unwrap(),
}
}
/// Print usage information
pub fn print_usage() -> Result<(), io::Error> {
let mut stdout = STDOUT.lock();
stdout.reset()?;
let mut bold = ColorSpec::new();
bold.set_bold(true);
stdout.set_color(&bold)?;
writeln!(
stdout,
"{} {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
)?;
stdout.reset()?;
writeln!(stdout, "{}", env!("CARGO_PKG_AUTHORS"))?;
writeln!(stdout, "{}", env!("CARGO_PKG_DESCRIPTION").trim())?;
writeln!(stdout)?;
writeln!(stdout, "{}", Commands::usage())?;
Ok(())
}
}
/// Subcommands of this application
@@ -50,9 +78,9 @@ pub enum Commands {
#[options(help = "display version information")]
Version(VersionOpts),
/// `list` subcommand
/// `readers` subcommand
#[options(help = "list detected readers")]
List(ListCmd),
Readers(ReadersCmd),
}
impl Commands {
@@ -61,7 +89,7 @@ impl Commands {
match self {
Commands::Help(help) => help.run(),
Commands::Version(version) => version.run(),
Commands::List(list) => list.run(),
Commands::Readers(list) => list.run(),
}
}
}
@@ -4,12 +4,12 @@ use gumdrop::Options;
use std::process::exit;
use yubikey_piv::readers::Readers;
/// The `list` subcommand
/// The `readers` subcommand
#[derive(Debug, Options)]
pub struct ListCmd {}
pub struct ReadersCmd {}
impl ListCmd {
/// Run the `list` subcommand
impl ReadersCmd {
/// Run the `readers` subcommand
pub fn run(&self) {
let mut readers = Readers::open().unwrap_or_else(|e| {
status_err!("couldn't open PC/SC context: {}", e);