Skip to content

Commit

Permalink
atlasexec: return ErrRequireLogin explicit for downstream consume
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Oct 3, 2024
1 parent ab6583b commit 03a5d1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ var defaultEnvs = map[string]string{
"ATLAS_NO_UPGRADE_SUGGESTIONS": "1",
}

// ErrRequireLogin is returned when a command requires the user to be logged in.
// It exists here to be shared between the different packages that require login.
var ErrRequireLogin = errors.New("command requires 'atlas login'")

// runCommand runs the given command and returns its output.
func (c *Client) runCommand(ctx context.Context, args []string) (io.Reader, error) {
var stdout, stderr bytes.Buffer
Expand All @@ -260,6 +264,11 @@ func (c *Client) runCommand(ctx context.Context, args []string) (io.Reader, erro
cmd.Stderr = &stderr
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
e := strings.TrimSpace(stderr.String())
// Explicit check the stderr for the login error.
if e == "Error: command requires 'atlas login'" {
return nil, ErrRequireLogin
}
return nil, &Error{
err: err,
Stderr: strings.TrimSpace(stderr.String()),
Expand Down
3 changes: 2 additions & 1 deletion atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func TestWhoAmI(t *testing.T) {
t.Setenv("TEST_STDOUT", "")
t.Setenv("TEST_STDERR", `Error: command requires 'atlas login'`)
_, err = c.WhoAmI(context.Background())
require.EqualError(t, err, "Error: command requires 'atlas login'")
require.EqualError(t, err, "command requires 'atlas login'")
require.ErrorIs(t, err, atlasexec.ErrRequireLogin)
}

func TestVars2(t *testing.T) {
Expand Down

0 comments on commit 03a5d1f

Please sign in to comment.