From fb235d0cf88687eb214b3ec7cb5fab596609d2b6 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Fri, 28 Dec 2018 11:01:08 -0800 Subject: Move printing of storage related status into 'storage status' sub-command The 'status' command has traditionally printed information about the connected Nitrokey and that included storage specific data if the device present is a Nitrokey Storage. Given that we have a root-level 'storage' command it arguably makes sense to move the printing of the storage related status information into a 'status' sub-command of the said command, which makes the output more predictable. --- nitrocli/src/args.rs | 15 +++++++++- nitrocli/src/commands.rs | 76 +++++++++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 37 deletions(-) (limited to 'nitrocli/src') diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index e7e7717..e533c82 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -286,7 +286,7 @@ fn parse(parser: &argparse::ArgumentParser<'_>, args: Vec) -> Result<()> /// Inquire the status of the nitrokey. fn status(args: Vec) -> Result<()> { let mut parser = argparse::ArgumentParser::new(); - parser.set_description("Print the status of the connected Nitrokey device"); + parser.set_description("Prints the status of the connected Nitrokey device"); parse(&parser, args)?; commands::status() @@ -296,6 +296,7 @@ fn status(args: Vec) -> Result<()> { enum StorageCommand { Close, Open, + Status, } impl StorageCommand { @@ -303,6 +304,7 @@ impl StorageCommand { match *self { StorageCommand::Close => storage_close(args), StorageCommand::Open => storage_open(args), + StorageCommand::Status => storage_status(args), } } } @@ -315,6 +317,7 @@ impl fmt::Display for StorageCommand { match *self { StorageCommand::Close => "close", StorageCommand::Open => "open", + StorageCommand::Status => "status", } ) } @@ -327,6 +330,7 @@ impl str::FromStr for StorageCommand { match s { "close" => Ok(StorageCommand::Close), "open" => Ok(StorageCommand::Open), + "status" => Ok(StorageCommand::Status), _ => Err(()), } } @@ -374,6 +378,15 @@ fn storage_close(args: Vec) -> Result<()> { commands::storage_close() } +/// Print the status of the nitrokey's storage. +fn storage_status(args: Vec) -> Result<()> { + let mut parser = argparse::ArgumentParser::new(); + parser.set_description("Prints the status of the Nitrokey's storage"); + parse(&parser, args)?; + + commands::storage_status() +} + /// Clear the PIN as cached by various other commands. fn clear(args: Vec) -> Result<()> { let mut parser = argparse::ArgumentParser::new(); diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index fdfe049..17426cd 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -197,34 +197,6 @@ fn print_status(model: &'static str, device: &nitrokey::DeviceWrapper) -> Result Ok(()) } -/// Pretty print the status of a Nitrokey Storage. -fn print_storage_status(status: &nitrokey::StorageStatus) { - println!( - r#" - SD card ID: {id:#x} - firmware: {fw} - storage keys: {sk} - volumes: - unencrypted: {vu} - encrypted: {ve} - hidden: {vh}"#, - id = status.serial_number_sd_card, - fw = if status.firmware_locked { - "locked" - } else { - "unlocked" - }, - sk = if status.stick_initialized { - "created" - } else { - "not created" - }, - vu = get_volume_status(&status.unencrypted_volume), - ve = get_volume_status(&status.encrypted_volume), - vh = get_volume_status(&status.hidden_volume), - ); -} - /// Inquire the status of the nitrokey. pub fn status() -> Result<()> { let device = get_device()?; @@ -232,14 +204,7 @@ pub fn status() -> Result<()> { nitrokey::DeviceWrapper::Pro(_) => "Pro", nitrokey::DeviceWrapper::Storage(_) => "Storage", }; - print_status(model, &device)?; - if let nitrokey::DeviceWrapper::Storage(storage) = device { - let status = storage - .get_status() - .map_err(|err| get_error("Getting Storage status failed", &err))?; - print_storage_status(&status); - } - Ok(()) + print_status(model, &device) } /// Open the encrypted volume on the nitrokey. @@ -270,6 +235,45 @@ pub fn storage_close() -> Result<()> { .map_err(|err| get_error("Closing encrypted volume failed", &err)) } +/// Pretty print the status of a Nitrokey Storage. +fn print_storage_status(status: &nitrokey::StorageStatus) { + println!( + r#"Status: + SD card ID: {id:#x} + firmware: {fw} + storage keys: {sk} + volumes: + unencrypted: {vu} + encrypted: {ve} + hidden: {vh}"#, + id = status.serial_number_sd_card, + fw = if status.firmware_locked { + "locked" + } else { + "unlocked" + }, + sk = if status.stick_initialized { + "created" + } else { + "not created" + }, + vu = get_volume_status(&status.unencrypted_volume), + ve = get_volume_status(&status.encrypted_volume), + vh = get_volume_status(&status.hidden_volume), + ); +} + +/// Connect to and pretty print the status of a Nitrokey Storage. +pub fn storage_status() -> Result<()> { + let device = get_storage_device()?; + let status = device + .get_status() + .map_err(|err| get_error("Getting Storage status failed", &err))?; + + print_storage_status(&status); + Ok(()) +} + /// Clear the PIN stored when opening the nitrokey's encrypted volume. pub fn clear() -> Result<()> { pinentry::clear_passphrase(pinentry::PinType::Admin)?; -- cgit v1.2.1