secrets/show: pretty print json

This commit is contained in:
Jörg Thalheim
2024-10-01 12:30:51 +02:00
committed by Mic92
parent 4a3030d6ed
commit be5f10e241
2 changed files with 11 additions and 4 deletions

View File

@@ -1,5 +1,7 @@
import argparse import argparse
import json
import logging import logging
import sys
from pathlib import Path from pathlib import Path
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
@@ -54,9 +56,13 @@ def generate_command(args: argparse.Namespace) -> None:
def show_command(args: argparse.Namespace) -> None: def show_command(args: argparse.Namespace) -> None:
key, key_type = sops.maybe_get_admin_public_key() key = sops.maybe_get_admin_public_key()
type_or_null = f'"{key_type.name.lower()}"' if key_type else "null" if not key:
print(f'{{"key": "{key}", "type": {type_or_null}}}') msg = "No public key found"
raise ClanError(msg)
json.dump(
{"key": key.pubkey, "type": str(key.key_type)}, sys.stdout, indent=2, sort_keys=True
)
def update_command(args: argparse.Namespace) -> None: def update_command(args: argparse.Namespace) -> None:

View File

@@ -1,3 +1,4 @@
import json
import logging import logging
import os import os
from collections.abc import Iterator from collections.abc import Iterator
@@ -253,7 +254,7 @@ def test_secrets(
cli.run(["secrets", "key", "generate", "--flake", str(test_flake.path)]) cli.run(["secrets", "key", "generate", "--flake", str(test_flake.path)])
with capture_output as output: with capture_output as output:
cli.run(["secrets", "key", "show", "--flake", str(test_flake.path)]) cli.run(["secrets", "key", "show", "--flake", str(test_flake.path)])
key = output.out key = json.loads(output.out)["key"]
assert key.startswith("age1") assert key.startswith("age1")
cli.run( cli.run(
["secrets", "users", "add", "--flake", str(test_flake.path), "testuser", key] ["secrets", "users", "add", "--flake", str(test_flake.path), "testuser", key]