Skip to content

fix(cli): format nested values as YAML and add --json flag to get#32

Merged
f3rdy merged 1 commit intomasterfrom
fix/get-json-output
Mar 14, 2026
Merged

fix(cli): format nested values as YAML and add --json flag to get#32
f3rdy merged 1 commit intomasterfrom
fix/get-json-output

Conversation

@f3rdy
Copy link
Copy Markdown
Contributor

@f3rdy f3rdy commented Mar 14, 2026

Problem

vaultctl get on structured entries (dicts, lists) outputs Python repr() format — single quotes, no indentation, not parseable by jq or other tools. Example:

Type: secretText
  domains: [{'name': 'docker build hosts', 'credentials': [{'type': 'x509ClientCert', ...

This makes credentialStore entries with 50+ nested credentials completely unreadable.

Solution

1. Human-readable output: YAML formatting

Added _format_value() helper (cli.py:35-46) that formats nested values:

  • Strings: returned as-is (no change to existing behavior)
  • Dicts/Lists: formatted as YAML via yaml.dump(default_flow_style=False)
  • Other types: converted via str()

The get command now calls _format_value(value[f]) instead of directly printing value[f], so nested structures render as readable YAML with proper indentation.

2. Machine-readable output: --json flag

New --json flag on the get command outputs the value as JSON:

vaultctl get vault_jenkins_credentials --json | jq '.global.credentials | length'

Works with --field too:

vaultctl get db_creds --field username --json

Uses json.dumps(indent=2, ensure_ascii=False) for readable JSON that pipes cleanly to jq.

Files changed

  • src/vaultctl/cli.py:
    • Added _format_value() helper (lines 35-46)
    • Added --json / output_json option to get command
    • Changed dict field output from value[f] to _format_value(value[f])
    • JSON output path for both full value and --field access

Test plan

  • vaultctl get <dict-key> shows readable YAML (not Python repr)
  • vaultctl get <dict-key> --json | jq . parses correctly
  • vaultctl get <string-key> unchanged (plain string output)
  • vaultctl get <dict-key> --field username unchanged
  • All 298 existing tests pass

…mand

Nested dict/list values were printed as Python repr() which is
unreadable and not parseable. Now outputs YAML by default and
JSON with --json flag for piping to jq etc.
@f3rdy f3rdy force-pushed the fix/get-json-output branch from bdc9957 to 634f720 Compare March 14, 2026 13:23
@f3rdy f3rdy merged commit 899ab2d into master Mar 14, 2026
2 checks passed
@f3rdy f3rdy deleted the fix/get-json-output branch March 14, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant