aboutsummaryrefslogtreecommitdiff
path: root/src/commands.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-09-09 21:37:19 +0200
committerRobin Krahl <robin.krahl@ireas.org>2020-09-11 11:03:32 +0200
commitef7b6b03355b82621d16f4f02a5a1a4558ae64cc (patch)
tree97b505e2e74a4b9e8aacb6c530fd23d7244a37c2 /src/commands.rs
parent70b9a1f0287f2aeda9677110106f43d1048306f3 (diff)
downloadnitrocli-ef7b6b03355b82621d16f4f02a5a1a4558ae64cc.tar.gz
nitrocli-ef7b6b03355b82621d16f4f02a5a1a4558ae64cc.tar.bz2
List all matching devices in status commmand
Previously, the status command would fail if more than one Nitrokey device is connected. With this patch, we list all attached devices that match the specified filter (model, serial number, USB path). This also casues some changes in the test cases: Previously, we assumed that status fails for multiple attached devices. We now use the lock command to produce this behavior.
Diffstat (limited to 'src/commands.rs')
-rw-r--r--src/commands.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/commands.rs b/src/commands.rs
index 597fcd8..a4e805e 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -363,19 +363,24 @@ fn print_storage_status(
}
/// Query and pretty print the status that is common to all Nitrokey devices.
-fn print_status(ctx: &mut Context<'_>, device: &nitrokey::DeviceWrapper<'_>) -> anyhow::Result<()> {
+fn print_status(
+ ctx: &mut Context<'_>,
+ idx: usize,
+ device: &nitrokey::DeviceWrapper<'_>,
+) -> anyhow::Result<()> {
let serial_number = device
.get_serial_number()
.context("Could not query the serial number")?;
println!(
ctx,
- r#"Status:
+ r#"Device #{idx}:
model: {model}
serial number: {id}
firmware version: {fwv}
user retry count: {urc}
admin retry count: {arc}"#,
+ idx = idx,
model = args::DeviceModel::from(device.get_model()).as_user_facing_str(),
id = serial_number,
fwv = device
@@ -402,7 +407,27 @@ fn print_status(ctx: &mut Context<'_>, device: &nitrokey::DeviceWrapper<'_>) ->
/// Inquire the status of the nitrokey.
pub fn status(ctx: &mut Context<'_>) -> anyhow::Result<()> {
- with_device(ctx, |ctx, device| print_status(ctx, &device))
+ let device_infos = find_devices(&ctx.config)?;
+ anyhow::ensure!(
+ !device_infos.is_empty(),
+ "Nitrokey device not found{}",
+ format_filter(&ctx.config)
+ );
+
+ let mut manager =
+ nitrokey::take().context("Failed to acquire access to Nitrokey device manager")?;
+ for (idx, device_info) in device_infos.iter().enumerate() {
+ let device = manager
+ .connect_path(device_info.path.deref())
+ .with_context(|| {
+ format!(
+ "Failed to connect to Nitrokey device at path {}",
+ device_info.path
+ )
+ })?;
+ print_status(ctx, idx, &device)?;
+ }
+ Ok(())
}
/// List the attached Nitrokey devices.