MCPcopy Create free account
hub / github.com/cli/cli / SwitchUser

Method SwitchUser

internal/config/config.go:591–621  ·  view source on GitHub ↗
(hostname, user string)

Source from the content-addressed store, hash-verified

589}
590
591func (c *AuthConfig) SwitchUser(hostname, user string) error {
592 previouslyActiveUser, err := c.ActiveUser(hostname)
593 if err != nil {
594 return fmt.Errorf("failed to get active user: %s", err)
595 }
596
597 previousSource := c.ActiveToken(hostname).Source
598 if previousSource != gh.TokenSourceKeyring &&
599 previousSource != gh.TokenSourceOAuthToken &&
600 previousSource != gh.TokenSourceRefreshableOAuthToken {
601 return fmt.Errorf("currently active token for %s is from %s", hostname, previousSource)
602 }
603
604 err = c.activateUser(hostname, user)
605 if err != nil {
606 // activateUser writes the config file only as its final step, so any failure means the on-disk config is
607 // still in its previous clean state and no disk write is needed to undo it. What may have changed is the
608 // in-memory config and the keyring: the keyring is persistent, so a stray active token from the failed
609 // switch is real, while the in-memory config only needs repairing so a later write persists the old data.
610 // setActiveCredential handles both, clearing every active representation and reinstalling the previously
611 // active user's credential.
612 if _, restoreErr := c.setActiveCredential(hostname, previouslyActiveUser); restoreErr != nil {
613 err = errors.Join(err, restoreErr)
614 }
615 c.cfg.Set([]string{hostsKey, hostname, userKey}, previouslyActiveUser)
616
617 return err
618 }
619
620 return nil
621}
622
623// Logout will remove user, git protocol, and auth token for the given hostname.
624// It will remove the auth token from the encrypted storage if it exists there.

Callers

nothing calls this directly

Calls 7

ActiveUserMethod · 0.95
ActiveTokenMethod · 0.95
activateUserMethod · 0.95
setActiveCredentialMethod · 0.95
JoinMethod · 0.80
ErrorfMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected