diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-12 11:14:27 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-12 11:14:27 -0800 |
commit | 4f2416011a565e5e579f1f3e6387a7344f89d96d (patch) | |
tree | ac7c2f677457264d08c3fde1f6720960528699ea /nitrocli/src/commands.rs | |
parent | e977016e668a8db528d67d77c4e3dc7fe62423e5 (diff) | |
download | nitrocli-4f2416011a565e5e579f1f3e6387a7344f89d96d.tar.gz nitrocli-4f2416011a565e5e579f1f3e6387a7344f89d96d.tar.bz2 |
Produce JSON formatted status
Diffstat (limited to 'nitrocli/src/commands.rs')
-rw-r--r-- | nitrocli/src/commands.rs | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 86a66df..969af11 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -330,11 +330,12 @@ fn print_storage_status( } /// Query and pretty print the status that is common to all Nitrokey devices. -fn print_status( - ctx: &mut args::ExecCtx<'_>, - model: &'static str, - device: &nitrokey::DeviceWrapper<'_>, -) -> Result<()> { +fn print_status(ctx: &mut args::ExecCtx<'_>, device: &nitrokey::DeviceWrapper<'_>) -> Result<()> { + let model = match device { + nitrokey::DeviceWrapper::Pro(_) => "Pro", + nitrokey::DeviceWrapper::Storage(_) => "Storage", + }; + let serial_number = device .get_serial_number() .map_err(|err| get_error("Could not query the serial number", err))?; @@ -365,14 +366,32 @@ fn print_status( } } +fn emit_json_status( + ctx: &mut args::ExecCtx<'_>, + device: &nitrokey::DeviceWrapper<'_>, +) -> Result<()> { + let model = match device { + nitrokey::DeviceWrapper::Pro(_) => "pro", + nitrokey::DeviceWrapper::Storage(_) => "storage", + }; + let serial = device.get_serial_number()?; + + let data = json::object! { + "model" => model, + "serial" => serial, + }; + ctx.stdout.write_all(data.dump().as_bytes())?; + Ok(()) +} + /// Inquire the status of the nitrokey. -pub fn status(ctx: &mut args::ExecCtx<'_>) -> Result<()> { +pub fn status(ctx: &mut args::ExecCtx<'_>, json: bool) -> Result<()> { with_device(ctx, |ctx, device| { - let model = match device { - nitrokey::DeviceWrapper::Pro(_) => "Pro", - nitrokey::DeviceWrapper::Storage(_) => "Storage", - }; - print_status(ctx, model, &device) + if json { + emit_json_status(ctx, &device) + } else { + print_status(ctx, &device) + } }) } |