MCPcopy
hub / github.com/cli/cli / SwitchUser

Method SwitchUser

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

Source from the content-addressed store, hash-verified

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