feat: add global --account flag to scope commands to a specific authenticated user#3
Open
galamdring wants to merge 1 commit into
Open
feat: add global --account flag to scope commands to a specific authenticated user#3galamdring wants to merge 1 commit into
galamdring wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
--accountpersistent flag to the root command so any authenticated subcommand can be scoped to a specific GitHub account without requiringgh auth switchfirst.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 switchwhich changes global state.--accountallows per-invocation identity selection with no side effects on other sessions.What Changed
pkg/cmd/root/root.go--accountas aPersistentFlagon the root command so all subcommands inherit it.PersistentPreRunE, after the existing auth check, calls the newapplyAccountFlaghelper.applyAccountFlag(cfg gh.Config, account string) error— a pure function that:accountis empty (default behaviour unchanged).DefaultHost()from the auth config.UsersForHost(hostname)to get the list of authenticated users.account "alice" is not authenticated on github.com. To add it run: gh auth login --hostname github.comTokenForUser(hostname, account)thenSetActiveToken(token, source)so all downstream HTTP calls in that invocation use the correct credential.pkg/cmd/root/root_test.go(new)Flag Name Choice
--userwas the natural first choice but conflicts with local flags already on several subcommands (codespace list,auth token,run list,secret list). Cobra panics atAddCommandwhen a persistent root flag shares a name with a local child flag.--accounthas zero conflicts acrosspkg/cmd/.Testing