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

Method SwitchUser

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

Source from the content-addressed store, hash-verified

403}
404
405func (c *AuthConfig) SwitchUser(hostname, user string) error {
406 previouslyActiveUser, err := c.ActiveUser(hostname)
407 if err != nil {
408 return fmt.Errorf("failed to get active user: %s", err)
409 }
410
411 previouslyActiveToken, previousSource := c.ActiveToken(hostname)
412 if previousSource != "keyring" && previousSource != "oauth_token" {
413 return fmt.Errorf("currently active token for %s is from %s", hostname, previousSource)
414 }
415
416 err = c.activateUser(hostname, user)
417 if err != nil {
418 // Given that activateUser can only fail before the config is written, or when writing the config
419 // we know for sure that the config has not been written. However, we still should restore it back
420 // to its previous clean state just in case something else tries to make use of the config, or tries
421 // to write it again.
422 if previousSource == "keyring" {
423 if setErr := keyring.Set(keyringServiceName(hostname), "", previouslyActiveToken); setErr != nil {
424 err = errors.Join(err, setErr)
425 }
426 }
427
428 if previousSource == "oauth_token" {
429 c.cfg.Set([]string{hostsKey, hostname, oauthTokenKey}, previouslyActiveToken)
430 }
431 c.cfg.Set([]string{hostsKey, hostname, userKey}, previouslyActiveUser)
432
433 return err
434 }
435
436 return nil
437}
438
439// Logout will remove user, git protocol, and auth token for the given hostname.
440// 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