Refactor authCfg.UsersForHost to not return an error

This commit is contained in:
Sam Coe 2023-12-04 14:52:11 -04:00 committed by William Martin
parent 92a902e453
commit 8cdbc1a8ca
7 changed files with 18 additions and 27 deletions

View file

@ -379,8 +379,7 @@ func TestLogoutOfActiveUserSwitchesUserIfPossible(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "test-token-1", token) require.Equal(t, "test-token-1", token)
usersForHost, err := authCfg.UsersForHost("github.com") usersForHost := authCfg.UsersForHost("github.com")
require.NoError(t, err)
require.NotContains(t, "active-user", usersForHost) require.NotContains(t, "active-user", usersForHost)
} }
@ -581,10 +580,10 @@ func TestUsersForHostNoHost(t *testing.T) {
authCfg := newTestAuthConfig(t) authCfg := newTestAuthConfig(t)
// When we get the users for a host that doesn't exist // When we get the users for a host that doesn't exist
_, err := authCfg.UsersForHost("github.com") users := authCfg.UsersForHost("github.com")
// Then it returns an error // Then it returns nil
require.EqualError(t, err, "unknown host: github.com") require.Nil(t, users)
} }
func TestUsersForHostWithUsers(t *testing.T) { func TestUsersForHostWithUsers(t *testing.T) {
@ -596,10 +595,9 @@ func TestUsersForHostWithUsers(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// When we get the users for that host // When we get the users for that host
users, err := authCfg.UsersForHost("github.com") users := authCfg.UsersForHost("github.com")
// Then it succeeds and returns the users // Then it succeeds and returns the users
require.NoError(t, err)
require.Equal(t, []string{"test-user-1", "test-user-2"}, users) require.Equal(t, []string{"test-user-1", "test-user-2"}, users)
} }

View file

@ -388,8 +388,7 @@ func (c *AuthConfig) SwitchUser(hostname, user string) error {
// Logout will remove user, git protocol, and auth token for the given hostname. // Logout will remove user, git protocol, and auth token for the given hostname.
// It will remove the auth token from the encrypted storage if it exists there. // It will remove the auth token from the encrypted storage if it exists there.
func (c *AuthConfig) Logout(hostname, username string) error { func (c *AuthConfig) Logout(hostname, username string) error {
// This error is ignorable because if there is no host then no logout is required users := c.UsersForHost(hostname)
users, _ := c.UsersForHost(hostname)
// If there is only one (or zero) users, then we remove the host // If there is only one (or zero) users, then we remove the host
// and unset the keyring tokens. // and unset the keyring tokens.
@ -453,13 +452,13 @@ func (c *AuthConfig) activateUser(hostname, user string) error {
return ghConfig.Write(c.cfg) return ghConfig.Write(c.cfg)
} }
func (c *AuthConfig) UsersForHost(hostname string) ([]string, error) { func (c *AuthConfig) UsersForHost(hostname string) []string {
users, err := c.cfg.Keys([]string{hostsKey, hostname, usersKey}) users, err := c.cfg.Keys([]string{hostsKey, hostname, usersKey})
if err != nil { if err != nil {
return nil, fmt.Errorf("unknown host: %s", hostname) return nil
} }
return users, nil return users
} }
func (c *AuthConfig) TokenForUser(hostname, user string) (string, string, error) { func (c *AuthConfig) TokenForUser(hostname, user string) (string, string, error) {

View file

@ -84,7 +84,7 @@ func logoutRun(opts *LogoutOptions) error {
} }
if username != "" { if username != "" {
knownUsers, _ := cfg.Authentication().UsersForHost(hostname) knownUsers := cfg.Authentication().UsersForHost(hostname)
if !slices.Contains(knownUsers, username) { if !slices.Contains(knownUsers, username) {
return fmt.Errorf("not logged in to %s account %s", hostname, username) return fmt.Errorf("not logged in to %s account %s", hostname, username)
} }
@ -101,10 +101,7 @@ func logoutRun(opts *LogoutOptions) error {
if hostname != "" && host != hostname { if hostname != "" && host != hostname {
continue continue
} }
knownUsers, err := cfg.Authentication().UsersForHost(host) knownUsers := cfg.Authentication().UsersForHost(host)
if err != nil {
return err
}
for _, user := range knownUsers { for _, user := range knownUsers {
if username != "" && user != username { if username != "" && user != username {
continue continue

View file

@ -24,7 +24,7 @@ const defaultSSHKeyTitle = "GitHub CLI"
type iconfig interface { type iconfig interface {
Login(string, string, string, string, bool) (bool, error) Login(string, string, string, string, bool) (bool, error)
UsersForHost(string) ([]string, error) UsersForHost(string) []string
} }
type LoginOptions struct { type LoginOptions struct {
@ -191,7 +191,7 @@ func Login(opts *LoginOptions) error {
// In this case we ignore the error if the host doesn't exist // In this case we ignore the error if the host doesn't exist
// because that can occur when the user is logging into a host // because that can occur when the user is logging into a host
// for the first time. // for the first time.
usersForHost, _ := cfg.UsersForHost(hostname) usersForHost := cfg.UsersForHost(hostname)
userWasAlreadyLoggedIn := slices.Contains(usersForHost, username) userWasAlreadyLoggedIn := slices.Contains(usersForHost, username)
if gitProtocol != "" { if gitProtocol != "" {

View file

@ -25,8 +25,8 @@ func (c tinyConfig) Login(host, username, token, gitProtocol string, encrypt boo
return false, nil return false, nil
} }
func (c tinyConfig) UsersForHost(hostname string) ([]string, error) { func (c tinyConfig) UsersForHost(hostname string) []string {
return nil, nil return nil
} }
func TestLogin_ssh(t *testing.T) { func TestLogin_ssh(t *testing.T) {

View file

@ -205,7 +205,7 @@ func statusRun(opts *StatusOptions) error {
}) })
statuses[hostname] = append(statuses[hostname], entry) statuses[hostname] = append(statuses[hostname], entry)
users, _ := authCfg.UsersForHost(hostname) users := authCfg.UsersForHost(hostname)
for _, username := range users { for _, username := range users {
if username == activeUser { if username == activeUser {
continue continue

View file

@ -99,7 +99,7 @@ func switchRun(opts *SwitchOptions) error {
} }
if username != "" { if username != "" {
knownUsers, _ := cfg.Authentication().UsersForHost(hostname) knownUsers := cfg.Authentication().UsersForHost(hostname)
if !slices.Contains(knownUsers, username) { if !slices.Contains(knownUsers, username) {
return fmt.Errorf("not logged in to %s account %s", hostname, username) return fmt.Errorf("not logged in to %s account %s", hostname, username)
} }
@ -116,10 +116,7 @@ func switchRun(opts *SwitchOptions) error {
if err != nil { if err != nil {
return err return err
} }
knownUsers, err := cfg.Authentication().UsersForHost(host) knownUsers := cfg.Authentication().UsersForHost(host)
if err != nil {
return err
}
for _, user := range knownUsers { for _, user := range knownUsers {
if username != "" && user != username { if username != "" && user != username {
continue continue