Skip to content

Commit

Permalink
feat(rust): Add JSON output support for vault list and vault show
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Oct 1, 2023
1 parent d32cfe2 commit 0d676c0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl VaultsState {
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct VaultState {
name: String,
path: PathBuf,
Expand Down
13 changes: 11 additions & 2 deletions implementations/rust/ockam/ockam_command/src/vault/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Args;
use miette::IntoDiagnostic;

use ockam_api::cli_state::traits::StateDirTrait;

Expand Down Expand Up @@ -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(())
}
27 changes: 23 additions & 4 deletions implementations/rust/ockam/ockam_command/src/vault/show.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(())
}
14 changes: 6 additions & 8 deletions implementations/rust/ockam/ockam_command/tests/bats/vault.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ 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\":"

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\":"

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}\""
}

@test "vault - CRUD" {
Expand Down

0 comments on commit 0d676c0

Please sign in to comment.