|
5 | 5 | from ...core.api.user import get_user_brief
|
6 | 6 | from .. import decorators
|
7 | 7 | from ..exceptions import handle_api_exceptions
|
8 |
| -from ..utils import maybe_spinner |
| 8 | +from ..utils import maybe_print_as_json, maybe_spinner |
9 | 9 | from .main import main
|
10 | 10 |
|
11 | 11 |
|
|
17 | 17 | @click.pass_context
|
18 | 18 | def whoami(ctx, opts):
|
19 | 19 | """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 | + ) |
21 | 28 |
|
22 | 29 | context_msg = "Failed to retrieve your authentication status!"
|
23 | 30 | with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
|
24 | 31 | with maybe_spinner(opts):
|
25 | 32 | 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 | + |
27 | 46 | click.echo("You are authenticated as:")
|
28 | 47 | if not is_auth:
|
29 | 48 | click.secho("Nobody (i.e. anonymous user)", fg="yellow")
|
|
0 commit comments