Skip to content

Commit

Permalink
atlasexec: expose atlas exit code (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorav committed Sep 28, 2023
1 parent afd1420 commit ef784ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
35 changes: 13 additions & 22 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,23 @@ func (c *Client) MigrateLint(ctx context.Context, params *MigrateLintParams) (*S
return nil, errors.New("atlasexec: Writer or Web reporting are not supported with MigrateLint, use MigrateLintError")
}
r, err := c.runCommand(ctx, lintArgs(params))
if cliErr := (cliError{}); errors.As(err, &cliErr) && cliErr.summary == "" {
r = strings.NewReader(cliErr.detail)
err = nil
}
return jsonDecode[SummaryReport](r, err)
}

// MigrateLintError runs the 'migrate lint' command, the output is written to params.Writer
func (c *Client) MigrateLintError(ctx context.Context, params *MigrateLintParams) error {
r, err := c.runCommand(ctx, lintArgs(params))
if err != nil {
return err
if cliErr := (cliError{}); errors.As(err, &cliErr) && cliErr.summary == "" {
r = strings.NewReader(cliErr.detail)
}
if params.Writer != nil {
_, err = io.Copy(params.Writer, r)
if params.Writer != nil && r != nil {
if _, ioErr := io.Copy(params.Writer, r); ioErr != nil {
err = errors.Join(err, ioErr)
}
}
return err
}
Expand Down Expand Up @@ -394,24 +400,9 @@ func (c *Client) runCommand(ctx context.Context, args []string) (io.Reader, erro
cmd.Stderr = &stderr
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
switch {
case stderr.Len() > 0:
// Atlas CLI writes the error to stderr.
// So this's critical issue, return the error.
return nil, &cliError{
summary: strings.TrimSpace(stderr.String()),
detail: strings.TrimSpace(stdout.String()),
}
case cmd.ProcessState.ExitCode() == 1:
// When the exit code is 1, it means that the command
// failed but the output is still valid JSON.
//
// `atlas migrate lint` returns 1 when there are
// linting errors.
default:
// When the exit code is not 1, it means that the
// command wasn't executed successfully.
return nil, err
return nil, cliError{
summary: strings.TrimSpace(stderr.String()),
detail: strings.TrimSpace(stdout.String()),
}
}
return &stdout, nil
Expand Down
8 changes: 4 additions & 4 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Test_MigrateApply(t *testing.T) {
got, err := c.MigrateApply(context.Background(), &atlasexec.MigrateApplyParams{
Env: "test",
})
require.EqualError(t, err, `atlasexec: required flag "url" not set`)
require.ErrorContains(t, err, `atlasexec: required flag "url" not set`)
require.Nil(t, got)
// Set the env var and try again
os.Setenv("DB_URL", "sqlite://file?_fk=1&cache=shared&mode=memory")
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestMigrateLint(t *testing.T) {
Latest: 1,
Writer: &buf,
})
require.NoError(t, err)
require.ErrorContains(t, err, "destructive changes detected")
var raw json.RawMessage
require.NoError(t, json.NewDecoder(&buf).Decode(&raw))
})
Expand Down Expand Up @@ -242,8 +242,8 @@ func TestMigrateLintWithLogin(t *testing.T) {
c, err := atlasexec.NewClient(".", "atlas")
require.NoError(t, err)
summary, err := c.MigrateLint(context.Background(), &atlasexec.MigrateLintParams{
DevURL: "sqlite://file?mode=memory",
DirURL: "file://testdata/migrations",
DevURL: "sqlite://file?mode=memory",
DirURL: "file://testdata/migrations",
ConfigURL: atlasConfigURL,
Base: "atlas://test-dir-slug",
})
Expand Down

0 comments on commit ef784ee

Please sign in to comment.