From 55d077dd80d280bbb910322804597450cccdbdf6 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 9 Dec 2019 09:23:40 -0800 Subject: [PATCH] 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. --- Cargo.toml | 2 +- cli/Cargo.toml | 5 +-- cli/src/commands.rs | 48 +++++++++++++++++++----- cli/src/commands/{list.rs => readers.rs} | 8 ++-- 4 files changed, 45 insertions(+), 18 deletions(-) rename cli/src/commands/{list.rs => readers.rs} (89%) diff --git a/Cargo.toml b/Cargo.toml index dbc77f7..a04d58e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ application providing general-purpose public-key signing and encryption with hardware-backed private keys for RSA (2048/1024) and ECC (P-256/P-384) algorithms (e.g, PKCS#1v1.5, ECDSA) """ -authors = ["Tony Arcieri ", "Yubico AB"] +authors = ["Tony Arcieri ", "Yubico AB"] edition = "2018" license = "BSD-2-Clause" repository = "https://github.com/iqlusioninc/yubikey-piv.rs" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index e1f3a30..0b3041b 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -2,10 +2,9 @@ name = "yubikey-cli" version = "0.0.1" description = """ -Command-line interface for performing encryption and signing using RSA and/or -ECC keys stored on YubiKey devices. +Command-line interface for performing encryption and signing using RSA/ECC keys stored on YubiKey devices. """ -authors = ["Tony Arcieri "] +authors = ["Tony Arcieri "] edition = "2018" license = "BSD-2-Clause" repository = "https://github.com/iqlusioninc/yubikey-piv.rs" diff --git a/cli/src/commands.rs b/cli/src/commands.rs index 02124df..5ec26b6 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -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(), } } } diff --git a/cli/src/commands/list.rs b/cli/src/commands/readers.rs similarity index 89% rename from cli/src/commands/list.rs rename to cli/src/commands/readers.rs index a30059f..7db161f 100644 --- a/cli/src/commands/list.rs +++ b/cli/src/commands/readers.rs @@ -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);