(hostname, user string)
| 390 | } |
| 391 | |
| 392 | func (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. |
nothing calls this directly
no test coverage detected