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