From 4f2416011a565e5e579f1f3e6387a7344f89d96d Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 12 Jan 2019 11:14:27 -0800 Subject: Produce JSON formatted status --- nitrocli/Cargo.lock | 7 +++++++ nitrocli/Cargo.toml | 3 +++ nitrocli/src/args.rs | 8 +++++++- nitrocli/src/commands.rs | 41 ++++++++++++++++++++++++++++++----------- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/nitrocli/Cargo.lock b/nitrocli/Cargo.lock index 9ae9299..ec8a1bd 100644 --- a/nitrocli/Cargo.lock +++ b/nitrocli/Cargo.lock @@ -71,6 +71,11 @@ dependencies = [ "wasi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "json" +version = "0.11.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "1.2.0" @@ -94,6 +99,7 @@ version = "0.2.4" dependencies = [ "argparse 0.2.2", "base32 0.4.0", + "json 0.11.15 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62", "nitrokey 0.4.0-alpha.3", "nitrokey-test 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -329,6 +335,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum getrandom 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "fc344b02d3868feb131e8b5fe2b9b0a1cc42942679af493061fc13b853243872" +"checksum json 0.11.15 (registry+https://github.com/rust-lang/crates.io-index)" = "92c245af8786f6ac35f95ca14feca9119e71339aaab41e878e7cdd655c97e9e5" "checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" "checksum nitrokey-test 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e7e81b55db51769209e88a63cdbb4f2dc7ee9cd20ccaf32fbb940a3b0c50259" "checksum nitrokey-test-state 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a59b732ed6d5212424ed31ec9649f05652bcbc38f45f2292b27a6044e7098803" diff --git a/nitrocli/Cargo.toml b/nitrocli/Cargo.toml index f48e7ca..5dd1033 100644 --- a/nitrocli/Cargo.toml +++ b/nitrocli/Cargo.toml @@ -49,6 +49,9 @@ version = "0.2.2" version = "0.4.0" path = "../base32" +[dependencies.json] +version = "0.11" + [dependencies.libc] version = "0.2" diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index 2e56e9e..67f6abf 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -329,11 +329,17 @@ fn parse( /// Inquire the status of the Nitrokey. fn status(ctx: &mut ExecCtx<'_>, args: Vec) -> Result<()> { + let mut json = false; let mut parser = argparse::ArgumentParser::new(); parser.set_description("Prints the status of the connected Nitrokey device"); + let _ = parser.refer(&mut json).add_option( + &["--json"], + argparse::StoreTrue, + "Emit status output in JSON format", + ); parse(ctx, parser, args)?; - commands::status(ctx) + commands::status(ctx, json) } /// Perform a factory reset. 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) + } }) } -- cgit v1.2.1