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

Method SwitchUser

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

Source from the content-addressed store, hash-verified

565}
566
567func (c *AuthConfig) SwitchUser(hostname, user string) error {
568 previouslyActiveUser, err := c.ActiveUser(hostname)
569 if err != nil {
570 return fmt.Errorf("failed to get active user: %s", err)
571 }
572
573 previousSource := c.ActiveToken(hostname).Source
574 if previousSource != gh.TokenSourceKeyring &&
575 previousSource != gh.TokenSourceOAuthToken &&
576 previousSource != gh.TokenSourceRefreshableOAuthToken {
577 return fmt.Errorf("currently active token for %s is from %s", hostname, previousSource)
578 }
579
580 err = c.activateUser(hostname, user)
581 if err != nil {
582 // activateUser writes the config file only as its final step, so any failure means the on-disk config is
583 // still in its previous clean state and no disk write is needed to undo it. What may have changed is the
584 // in-memory config and the keyring: the keyring is persistent, so a stray active token from the failed
585 // switch is real, while the in-memory config only needs repairing so a later write persists the old data.
586 // setActiveCredential handles both, clearing every active representation and reinstalling the previously
587 // active user's credential.
588 if _, restoreErr := c.setActiveCredential(hostname, previouslyActiveUser); restoreErr != nil {
589 err = errors.Join(err, restoreErr)
590 }
591 c.cfg.Set([]string{hostsKey, hostname, userKey}, previouslyActiveUser)
592
593 return err
594 }
595
596 return nil
597}
598
599// Logout will remove user, git protocol, and auth token for the given hostname.
600// 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