diff options
| author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-14 20:01:28 +0100 | 
|---|---|---|
| committer | Daniel Mueller <deso@posteo.net> | 2020-01-15 12:08:15 -0800 | 
| commit | 85ad47d13615d2ab6ed1124272cec7d33517dac1 (patch) | |
| tree | 8aabb0fee21a9657850b45b31d3d771dddab8552 | |
| parent | a9af2d4295c1c0467ffd65a264559c7b4fe38d11 (diff) | |
| download | nitrocli-85ad47d13615d2ab6ed1124272cec7d33517dac1.tar.gz nitrocli-85ad47d13615d2ab6ed1124272cec7d33517dac1.tar.bz2 | |
Implement list command
This patch implements the list command that lists all attached Nitrokey
devices.  Currently the Nitrokey Storage does not report its serial
number during HID enumeration, see [0].  So if we detect a Nitrokey
Storage device, we connect to it and use the get_serial_number function
to query its serial number.  This can be disabled using the --no-connect
option.
Note that even the get_serial_number function reports a wrong serial
number for the Nitrokey Storage, see [1].
[0] https://github.com/Nitrokey/nitrokey-storage-firmware/issues/88
[1] https://github.com/Nitrokey/nitrokey-storage-firmware/issues/76
| -rw-r--r-- | nitrocli/src/commands.rs | 37 | 
1 files changed, 35 insertions, 2 deletions
| diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 3bc7300..e361509 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -377,8 +377,41 @@ pub fn status(ctx: &mut args::ExecCtx<'_>) -> Result<()> {  }  /// List the attached Nitrokey devices. -pub fn list(_ctx: &mut args::ExecCtx<'_>, _no_connect: bool) -> Result<()> { -  unimplemented!(); +pub fn list(ctx: &mut args::ExecCtx<'_>, no_connect: bool) -> Result<()> { +  set_log_level(ctx); + +  let device_infos = nitrokey::list_devices()?; +  if device_infos.is_empty() { +    println!(ctx, "No Nitrokey device connected")?; +  } else { +    println!(ctx, "device path\tmodel\tserial number")?; +    let mut manager = nitrokey::take()?; + +    for device_info in device_infos { +      let model = device_info +        .model +        .map(|m| m.to_string()) +        .unwrap_or_else(|| "unknown".into()); +      let serial_number = match device_info.serial_number { +        Some(serial_number) => format!("0x{}", serial_number), +        None => { +          // Storage devices do not have the serial number present in +          // the device information. We have to connect to them to +          // retrieve the information. +          if no_connect { +            "N/A".to_string() +          } else { +            let device = manager.connect_path(device_info.path.clone())?; +            format!("0x{}", device.get_serial_number()?) +          } +        } +      }; + +      println!(ctx, "{}\t{}\t{}", device_info.path, model, serial_number)?; +    } +  } + +  Ok(())  }  /// Perform a factory reset. | 
