From 7c42d2f78e2a0d91e584626ff80a5d0bfb81cc07 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Fri, 29 Sep 2023 11:46:42 -0700 Subject: [PATCH] feat(rust): Add JSON output support for vault list and vault show --- .../ockam/ockam_api/src/cli_state/vaults.rs | 2 +- .../ockam/ockam_command/src/vault/list.rs | 13 +++++++-- .../ockam/ockam_command/src/vault/show.rs | 27 ++++++++++++++++--- .../ockam/ockam_command/tests/bats/vault.bats | 15 +++++------ 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/implementations/rust/ockam/ockam_api/src/cli_state/vaults.rs b/implementations/rust/ockam/ockam_api/src/cli_state/vaults.rs index a1ed13af63b..0636762f0b6 100644 --- a/implementations/rust/ockam/ockam_api/src/cli_state/vaults.rs +++ b/implementations/rust/ockam/ockam_api/src/cli_state/vaults.rs @@ -34,7 +34,7 @@ impl VaultsState { } } -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq, Serialize)] pub struct VaultState { name: String, path: PathBuf, diff --git a/implementations/rust/ockam/ockam_command/src/vault/list.rs b/implementations/rust/ockam/ockam_command/src/vault/list.rs index 282effcdd3e..85671977d7d 100644 --- a/implementations/rust/ockam/ockam_command/src/vault/list.rs +++ b/implementations/rust/ockam/ockam_command/src/vault/list.rs @@ -1,4 +1,5 @@ use clap::Args; +use miette::IntoDiagnostic; use ockam_api::cli_state::traits::StateDirTrait; @@ -26,9 +27,17 @@ impl ListCommand { fn run_impl(opts: CommandGlobalOpts) -> miette::Result<()> { let vaults = opts.state.vaults.list()?; - let list = opts + let plain = opts .terminal .build_list(&vaults, "Vaults", "No vaults found on this system.")?; - opts.terminal.stdout().plain(list).write_line()?; + + let json = serde_json::to_string_pretty(&vaults).into_diagnostic()?; + + opts.terminal + .stdout() + .plain(plain) + .json(json) + .write_line()?; + Ok(()) } diff --git a/implementations/rust/ockam/ockam_command/src/vault/show.rs b/implementations/rust/ockam/ockam_command/src/vault/show.rs index cd95869353c..4e99de4dd81 100644 --- a/implementations/rust/ockam/ockam_command/src/vault/show.rs +++ b/implementations/rust/ockam/ockam_command/src/vault/show.rs @@ -1,4 +1,8 @@ +use std::fmt::Write; + use clap::Args; +use miette::IntoDiagnostic; + use ockam_api::cli_state::traits::StateDirTrait; use crate::util::local_cmd; @@ -31,9 +35,24 @@ fn run_impl(opts: CommandGlobalOpts, cmd: ShowCommand) -> miette::Result<()> { .name .unwrap_or(opts.state.vaults.default()?.name().to_string()); let state = opts.state.vaults.get(name)?; - println!("Vault:"); - for line in state.to_string().lines() { - println!("{:2}{}", "", line) - } + + let json = serde_json::to_string_pretty(&state).into_diagnostic()?; + + let plain = { + let mut buf = String::new(); + + writeln!(buf, "Vault:").into_diagnostic()?; + for line in state.to_string().lines() { + writeln!(buf, "{:2}{}", "", line).into_diagnostic()?; + } + buf + }; + + opts.terminal + .stdout() + .json(json) + .plain(plain) + .write_line()?; + Ok(()) } diff --git a/implementations/rust/ockam/ockam_command/tests/bats/vault.bats b/implementations/rust/ockam/ockam_command/tests/bats/vault.bats index 7527fb801d8..59a2356f656 100644 --- a/implementations/rust/ockam/ockam_command/tests/bats/vault.bats +++ b/implementations/rust/ockam/ockam_command/tests/bats/vault.bats @@ -19,21 +19,20 @@ teardown() { run_success "$OCKAM" vault create "${v1}" run_success "$OCKAM" vault show "${v1}" - assert_output --partial "Name: ${v1}" - assert_output --partial "Type: OCKAM" + assert_output --partial "\"name\": \"${v1}\"" + assert_output --partial "\"aws_kms\": false" v2=$(random_str) run_success "$OCKAM" vault create "${v2}" run_success "$OCKAM" vault show "${v2}" - assert_output --partial "Name: ${v2}" - assert_output --partial "Type: OCKAM" + assert_output --partial "\"name\": \"${v2}\"" + assert_output --partial "\"aws_kms\": false" run_success "$OCKAM" vault list - assert_output --partial "Vault ${v1}" - assert_output --partial "Type OCKAM" - assert_output --partial "Vault ${v2}" - assert_output --partial "Type OCKAM" + assert_output --partial "\"name\": \"${v1}\"" + assert_output --partial "\"name\": \"${v2}\"" + assert_output --partial "\"aws_kms\": false" } @test "vault - CRUD" {