fix(auth status): exit with code 1 when auth fails

This commit is contained in:
Jay McCure 2024-04-29 11:11:24 +10:00
parent 9ed43f6507
commit dc62272105
No known key found for this signature in database
2 changed files with 30 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package status
import (
"fmt"
"slices"
"gitlab.com/gitlab-org/cli/pkg/iostreams"
@ -64,21 +65,18 @@ func statusRun(opts *StatusOpts) error {
instances, err := cfg.Hosts()
if len(instances) == 0 || err != nil {
fmt.Fprintf(stderr,
"No GitLab instance has been authenticated with glab. Run `%s` to authenticate.\n", c.Bold("glab auth login"))
return cmdutils.SilentError
return fmt.Errorf("No GitLab instance has been authenticated with glab. Run `%s` to authenticate.\n", c.Bold("glab auth login"))
}
var hostNotAuthenticated bool
if opts.Hostname != "" {
hostNotAuthenticated = true
if opts.Hostname != "" && !slices.Contains(instances, opts.Hostname) {
return fmt.Errorf("%s %s not authenticated with glab. Run `%s %s` to authenticate", c.FailedIcon(), opts.Hostname, c.Bold("glab auth login --hostname"), c.Bold(opts.Hostname))
}
var failedAuth = false
for _, instance := range instances {
if opts.Hostname != "" && opts.Hostname != instance {
continue
}
hostNotAuthenticated = false
statusInfo[instance] = []string{}
addMsg := func(x string, ys ...interface{}) {
statusInfo[instance] = append(statusInfo[instance], fmt.Sprintf(x, ys...))
@ -92,11 +90,13 @@ func statusRun(opts *StatusOpts) error {
if err == nil {
user, err := api.CurrentUser(apiClient.Lab())
if err != nil {
failedAuth = true
addMsg("%s %s: api call failed: %s", c.FailedIcon(), instance, err)
} else {
addMsg("%s Logged in to %s as %s (%s)", c.GreenCheck(), instance, c.Bold(user.Username), tokenSource)
}
} else {
failedAuth = true
addMsg("%s %s: failed to initialize api client: %s", c.FailedIcon(), instance, err)
}
proto, _ := cfg.Get(instance, "git_protocol")
@ -129,9 +129,8 @@ func statusRun(opts *StatusOpts) error {
}
}
if opts.Hostname != "" && hostNotAuthenticated {
fmt.Fprintf(stderr, "%s %s not authenticated with glab. Run `%s %s` to authenticate\n", c.FailedIcon(), opts.Hostname, c.Bold("glab auth login --hostname"), c.Bold(opts.Hostname))
return cmdutils.SilentError
if opts.Hostname != "" && failedAuth {
return fmt.Errorf("%s %s not authenticated with glab. Run `%s %s` to authenticate", c.FailedIcon(), opts.Hostname, c.Bold("glab auth login --hostname"), c.Bold(opts.Hostname))
}
for _, instance := range instances {
@ -144,5 +143,10 @@ func statusRun(opts *StatusOpts) error {
fmt.Fprintf(stderr, " %s\n", line)
}
}
return nil
if failedAuth {
return fmt.Errorf("\n%s could not authenticate to one or more of the configured GitLab instances", c.FailedIcon())
} else {
return nil
}
}

View File

@ -137,7 +137,7 @@ hosts:
Hostname: "invalid.instance",
},
wantErr: true,
stderr: "x invalid.instance not authenticated with glab. Run `glab auth login --hostname invalid.instance` to authenticate\n",
stderr: "x invalid.instance not authenticated with glab. Run `glab auth login --hostname invalid.instance` to authenticate",
},
}
@ -171,11 +171,20 @@ hosts:
tt.opts.IO = io
tt.opts.HttpClientOverride = client
t.Run(tt.name, func(t *testing.T) {
if err := statusRun(tt.opts); (err != nil) != tt.wantErr {
err := statusRun(tt.opts)
if (err != nil) != tt.wantErr {
t.Errorf("statusRun() error = %v, wantErr %v", err, tt.wantErr)
}
assert.Equal(t, stdout.String(), "")
assert.Equal(t, tt.stderr, stderr.String())
if tt.wantErr {
assert.NotNil(t, err)
assert.Equal(t, tt.stderr, err.Error())
} else {
assert.Equal(t, err, nil)
assert.Equal(t, stderr.String(), tt.stderr)
}
})
}
}
@ -261,7 +270,7 @@ gl.io
}
err = statusRun(opts)
assert.Equal(t, err, nil)
assert.Equal(t, "\nx could not authenticate to one or more of the configured GitLab instances", err.Error())
assert.Equal(t, stdout.String(), "")
assert.Equal(t, expectedOutput, stderr.String())
}
@ -273,7 +282,7 @@ git_protocol: ssh
configs, err := config.ParseConfig("config.yml")
assert.Nil(t, err)
io, _, stdout, stderr := iostreams.Test()
io, _, stdout, _ := iostreams.Test()
opts := &StatusOpts{
Config: func() (config.Config, error) {
@ -283,8 +292,7 @@ git_protocol: ssh
}
t.Run("no instance authenticated", func(t *testing.T) {
err := statusRun(opts)
assert.Equal(t, err, cmdutils.SilentError)
assert.Equal(t, "No GitLab instance has been authenticated with glab. Run `glab auth login` to authenticate.\n", err.Error())
assert.Equal(t, stdout.String(), "")
assert.Equal(t, "No GitLab instance has been authenticated with glab. Run `glab auth login` to authenticate.\n", stderr.String())
})
}