mirror of https://gitlab.com/gitlab-org/cli.git
fix(auth status): exit with code 1 when auth fails
This commit is contained in:
parent
9ed43f6507
commit
30f0bb189e
|
@ -64,12 +64,12 @@ 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
|
||||
statusCode := 0
|
||||
|
||||
if opts.Hostname != "" {
|
||||
hostNotAuthenticated = true
|
||||
}
|
||||
|
@ -92,6 +92,7 @@ func statusRun(opts *StatusOpts) error {
|
|||
if err == nil {
|
||||
user, err := api.CurrentUser(apiClient.Lab())
|
||||
if err != nil {
|
||||
statusCode = 1
|
||||
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)
|
||||
|
@ -130,8 +131,7 @@ 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
|
||||
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 +144,10 @@ func statusRun(opts *StatusOpts) error {
|
|||
fmt.Fprintf(stderr, " %s\n", line)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
if statusCode != 0 {
|
||||
return fmt.Errorf("\n%s could not authenticate to one or more of the configured GitLab instances", c.FailedIcon())
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,19 @@ 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.Equal(t, err.Error(), tt.stderr)
|
||||
} else {
|
||||
assert.Equal(t, err, nil)
|
||||
assert.Equal(t, stderr.String(), tt.stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -261,7 +269,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 +281,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 +291,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())
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue