Skip to content

Commit ed7b665

Browse files
ceng-330-add-format-flag-for-whoami
1 parent 9d8b666 commit ed7b665

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

cloudsmith_cli/cli/commands/whoami.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ...core.api.user import get_user_brief
66
from .. import decorators
77
from ..exceptions import handle_api_exceptions
8-
from ..utils import maybe_spinner
8+
from ..utils import maybe_print_as_json, maybe_spinner
99
from .main import main
1010

1111

@@ -17,13 +17,32 @@
1717
@click.pass_context
1818
def whoami(ctx, opts):
1919
"""Retrieve your current authentication status."""
20-
click.echo("Retrieving your authentication status from the API ... ", nl=False)
20+
# Use stderr for messages if the output is something else (e.g. JSON)
21+
use_stderr = opts.output != "pretty"
22+
23+
click.echo(
24+
"Retrieving your authentication status from the API ... ",
25+
nl=False,
26+
err=use_stderr,
27+
)
2128

2229
context_msg = "Failed to retrieve your authentication status!"
2330
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
2431
with maybe_spinner(opts):
2532
is_auth, username, email, name = get_user_brief()
26-
click.secho("OK", fg="green")
33+
34+
click.secho("OK", fg="green", err=use_stderr)
35+
36+
data = {
37+
"authenticated": is_auth,
38+
"username": username,
39+
"email": email,
40+
"name": name,
41+
}
42+
43+
if maybe_print_as_json(opts, data):
44+
return
45+
2746
click.echo("You are authenticated as:")
2847
if not is_auth:
2948
click.secho("Nobody (i.e. anonymous user)", fg="yellow")

0 commit comments

Comments
 (0)