(hostname, user string)
| 589 | } |
| 590 | |
| 591 | func (c *AuthConfig) SwitchUser(hostname, user string) error { |
| 592 | previouslyActiveUser, err := c.ActiveUser(hostname) |
| 593 | if err != nil { |
| 594 | return fmt.Errorf("failed to get active user: %s", err) |
| 595 | } |
| 596 | |
| 597 | previousSource := c.ActiveToken(hostname).Source |
| 598 | if previousSource != gh.TokenSourceKeyring && |
| 599 | previousSource != gh.TokenSourceOAuthToken && |
| 600 | previousSource != gh.TokenSourceRefreshableOAuthToken { |
| 601 | return fmt.Errorf("currently active token for %s is from %s", hostname, previousSource) |
| 602 | } |
| 603 | |
| 604 | err = c.activateUser(hostname, user) |
| 605 | if err != nil { |
| 606 | // activateUser writes the config file only as its final step, so any failure means the on-disk config is |
| 607 | // still in its previous clean state and no disk write is needed to undo it. What may have changed is the |
| 608 | // in-memory config and the keyring: the keyring is persistent, so a stray active token from the failed |
| 609 | // switch is real, while the in-memory config only needs repairing so a later write persists the old data. |
| 610 | // setActiveCredential handles both, clearing every active representation and reinstalling the previously |
| 611 | // active user's credential. |
| 612 | if _, restoreErr := c.setActiveCredential(hostname, previouslyActiveUser); restoreErr != nil { |
| 613 | err = errors.Join(err, restoreErr) |
| 614 | } |
| 615 | c.cfg.Set([]string{hostsKey, hostname, userKey}, previouslyActiveUser) |
| 616 | |
| 617 | return err |
| 618 | } |
| 619 | |
| 620 | return nil |
| 621 | } |
| 622 | |
| 623 | // Logout will remove user, git protocol, and auth token for the given hostname. |
| 624 | // It will remove the auth token from the encrypted storage if it exists there. |
nothing calls this directly
no test coverage detected