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

Method SwitchUser

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

Source from the content-addressed store, hash-verified

453}
454
455func (c *AuthConfig) SwitchUser(hostname, user string) error {
456 previouslyActiveUser, err := c.ActiveUser(hostname)
457 if err != nil {
458 return fmt.Errorf("failed to get active user: %s", err)
459 }
460
461 previouslyActiveToken, previousSource := c.ActiveToken(hostname)
462 if previousSource != "keyring" && previousSource != "oauth_token" {
463 return fmt.Errorf("currently active token for %s is from %s", hostname, previousSource)
464 }
465
466 err = c.activateUser(hostname, user)
467 if err != nil {
468 // Given that activateUser can only fail before the config is written, or when writing the config
469 // we know for sure that the config has not been written. However, we still should restore it back
470 // to its previous clean state just in case something else tries to make use of the config, or tries
471 // to write it again.
472 if previousSource == "keyring" {
473 if setErr := keyring.Set(keyringServiceName(hostname), "", previouslyActiveToken); setErr != nil {
474 err = errors.Join(err, setErr)
475 }
476 }
477
478 if previousSource == "oauth_token" {
479 c.cfg.Set([]string{hostsKey, hostname, oauthTokenKey}, previouslyActiveToken)
480 }
481 c.cfg.Set([]string{hostsKey, hostname, userKey}, previouslyActiveUser)
482
483 return err
484 }
485
486 return nil
487}
488
489// Logout will remove user, git protocol, and auth token for the given hostname.
490// It will remove the auth token from the encrypted storage if it exists there.

Callers

nothing calls this directly

Calls 8

ActiveUserMethod · 0.95
ActiveTokenMethod · 0.95
activateUserMethod · 0.95
SetFunction · 0.92
JoinMethod · 0.80
keyringServiceNameFunction · 0.70
ErrorfMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected