Skip to content

Commit

Permalink
Adds json output to namespace list (#123)
Browse files Browse the repository at this point in the history
* Adds json output to namespace list

Co-authored-by: Brandon Squizzato <[email protected]>
  • Loading branch information
skarekrow and bsquizz authored Oct 5, 2021
1 parent 4802588 commit 2aa5b5d
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions bonfire/bonfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,23 +577,44 @@ def inner(func):
default=False,
help="show only namespaces reserved in your name",
)
def _list_namespaces(available, mine):
@click.option(
"--output",
"-o",
default="cli",
help="which output format to return the data in",
type=click.Choice(['cli', 'json'], case_sensitive=False)
)
def _list_namespaces(available, mine, output):
"""Get list of ephemeral namespaces"""
namespaces = get_namespaces(available=available, mine=mine)
if not available and not mine and not namespaces:
_error(NO_RESERVATION_SYS)
elif not namespaces:
click.echo("no namespaces found")
if output == 'json':
click.echo("{}")
else:
click.echo("no namespaces found")
else:
data = {
"NAME": [ns.name for ns in namespaces],
"RESERVED": [str(ns.reserved).lower() for ns in namespaces],
"READY": [str(ns.ready).lower() for ns in namespaces],
"REQUESTER": [ns.requester_name for ns in namespaces],
"EXPIRES IN": [ns.expires_in for ns in namespaces],
}
tabulated = tabulate(data, headers="keys")
click.echo(tabulated)
if output == 'json':
data = {}
for ns in namespaces:
data[ns.name] = {
"reserved": ns.reserved,
"ready": ns.ready,
"requester": ns.requester_name,
"expires_in": ns.expires_in,
}
click.echo(json.dumps(data, indent=2))
else:
data = {
"NAME": [ns.name for ns in namespaces],
"RESERVED": [str(ns.reserved).lower() for ns in namespaces],
"READY": [str(ns.ready).lower() for ns in namespaces],
"REQUESTER": [ns.requester_name for ns in namespaces],
"EXPIRES IN": [ns.expires_in for ns in namespaces],
}
tabulated = tabulate(data, headers="keys")
click.echo(tabulated)


@namespace.command("reserve")
Expand Down

0 comments on commit 2aa5b5d

Please sign in to comment.