Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust): Add JSON output support for vault list and vault show #6184

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};
adrianbenavides marked this conversation as resolved.
Show resolved Hide resolved

opts.terminal
.stdout()
.json(json)
.plain(plain)
.write_line()?;

Ok(())
}
15 changes: 7 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,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}\""
solidiquis marked this conversation as resolved.
Show resolved Hide resolved
assert_output --partial "\"aws_kms\": false"
}

@test "vault - CRUD" {
Expand Down
Loading