feat(auth): add JSON output to auth status
Description
Adds --output json (-F json) and --jq to glab auth status, using the existing cmdutils.EnableJSONOutput helper. Tools that drive glab (editor extensions, desktop apps, scripts) can now find which GitLab instances the user is signed in to, and whether those credentials still work, without parsing the human-readable text on stderr.
$ glab auth status --all --output json
{"hosts":[{"host":"gitlab.com","state":"success","user":"jdoe","token_source":"keyring","git_protocol":"ssh","api_protocol":"https","api_endpoint":"https://gitlab.com/api/v4/","graphql_endpoint":"https://gitlab.com/api/graphql/"}]}Each checked instance reports host, state (success or error), user, error, token_source (keyring, environment, config_file, or none when no token is stored), git_protocol, api_protocol, api_endpoint, graphql_endpoint, subfolder, ssh_host, and warnings, as proposed in the issue. warnings lists hints that text mode prints, such as an OAuth host with no refresh token, and is omitted when empty.
Behavior, following the proposal:
- Host selection is unchanged:
--hostname,--all, and the current context work the same as in text mode. - The JSON never contains the token.
--show-tokentogether with--output jsonis rejected. - The exit code is unchanged: it is 1 when any instance fails to authenticate. In JSON mode, stdout holds a single JSON document (the failure is signaled with
cmdutils.SilentError, so the error handler does not append a second JSON object). - Text output is unchanged. In JSON mode, the text hints on stderr are not printed; the incomplete-OAuth hint is reported in
warningsinstead. tokenSourceKindis the single token-source classifier, and the text output (tokenStorageDescription,isPlaintextTokenSource) is built on it, so text and JSON cannot drift apart.
Related Issues
Closes #8576 (closed)
How has this been tested?
- New unit tests in
internal/commands/auth/status/status_test.go:- mixed success and failure across two hosts: asserts each
hostStatusfield afterjson.Unmarshal, theSilentErrorreturn (exit 1), empty stderr, and no token in stdout - single host with a token from
GITLAB_TOKEN: assertstoken_sourceisenvironmentand the command succeeds --show-token --output jsonis rejected- error branches in JSON mode: keyring read failure, API client setup failure, and
token_source: "none"when no token is stored warningsis set for an OAuth host with nooauth2_refresh_token- table test for
tokenSourceKindcovering every branch, includingkeyring
- mixed success and failure across two hosts: asserts each
- Existing text-mode tests pass unchanged.
go test ./internal/commands/auth/... ./internal/commands/ ./cmd/...golangci-lint run ./internal/commands/auth/...go run ./scripts/comment-overlapmake gen-docs(regenerateddocs/source/auth/status.mdis included in the commit)
To try it locally:
make build
./bin/glab auth status --all --output json
./bin/glab auth status --output json --jq '.hosts[] | select(.state == "error") | .host'
./bin/glab auth status --show-token --output json # rejectedScreenshots (if appropriate):
Not applicable.