Skip to content

feat: add global --account flag to scope commands to a specific authenticated user#3

Open
galamdring wants to merge 1 commit into
trunkfrom
feat/global-user-flag
Open

feat: add global --account flag to scope commands to a specific authenticated user#3
galamdring wants to merge 1 commit into
trunkfrom
feat/global-user-flag

Conversation

@galamdring

Copy link
Copy Markdown
Owner

Summary

Adds a --account persistent flag to the root command so any authenticated subcommand can be scoped to a specific GitHub account without requiring gh auth switch first.

Motivation

Users with multiple GitHub accounts (personal + work, multiple orgs) frequently need to run a single command as a different identity from the currently active one. Today that requires an explicit gh auth switch which changes global state. --account allows per-invocation identity selection with no side effects on other sessions.

What Changed

pkg/cmd/root/root.go

  • Added --account as a PersistentFlag on the root command so all subcommands inherit it.
  • In PersistentPreRunE, after the existing auth check, calls the new applyAccountFlag helper.
  • Added applyAccountFlag(cfg gh.Config, account string) error — a pure function that:
    1. No-ops when account is empty (default behaviour unchanged).
    2. Looks up DefaultHost() from the auth config.
    3. Calls UsersForHost(hostname) to get the list of authenticated users.
    4. Returns a descriptive error if the requested account is not in that list, with guidance:
      account "alice" is not authenticated on github.com. To add it run: gh auth login --hostname github.com
    5. On success, calls TokenForUser(hostname, account) then SetActiveToken(token, source) so all downstream HTTP calls in that invocation use the correct credential.

pkg/cmd/root/root_test.go (new)

  • 4 table-driven tests covering:
    • empty account (no-op)
    • account present and authenticated (token installed)
    • account not in the authenticated list (descriptive error)
    • multiple accounts, correct one selected

Flag Name Choice

--user was the natural first choice but conflicts with local flags already on several subcommands (codespace list, auth token, run list, secret list). Cobra panics at AddCommand when a persistent root flag shares a name with a local child flag. --account has zero conflicts across pkg/cmd/.

Testing

go test ./pkg/cmd/root/... -run Test_applyAccountFlag   # 4/4 pass
go test ./...                                           # zero failures

…ection

Adds a --account flag to the root command that is inherited by every
authenticated subcommand. When specified, the flag validates that the
given account is currently authenticated for the default host and
overrides the active token for the duration of that command.

If the account is not authenticated, an error is returned with direction
to add it via: gh auth login --hostname <host>

Changes:
- pkg/cmd/root/root.go: register --account as a persistent flag,
  invoke applyAccountFlag from PersistentPreRunE after auth check,
  add applyAccountFlag helper that validates + overrides the active token
- pkg/cmd/root/root_test.go: table-driven tests for applyAccountFlag
  covering missing account, error message content, single valid account,
  and multi-user disambiguation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant