Skip to content

Commit

Permalink
atlasexec: remove extra logging details made by the SDK (#24)
Browse files Browse the repository at this point in the history
This change is introduced because we want the github action
logs to look as close as possible to the raw output of Atlas
  • Loading branch information
dorav committed Oct 1, 2023
1 parent b295faf commit 7edfcfc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
39 changes: 12 additions & 27 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ 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)
if cliErr := (cliError{}); errors.As(err, &cliErr) && cliErr.stderr == "" {
r = strings.NewReader(cliErr.stdout)
err = nil
}
return jsonDecode[SummaryReport](r, err)
Expand All @@ -333,8 +333,8 @@ func (c *Client) MigrateLint(ctx context.Context, params *MigrateLintParams) (*S
// 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 cliErr := (cliError{}); errors.As(err, &cliErr) && cliErr.summary == "" {
r = strings.NewReader(cliErr.detail)
if cliErr := (cliError{}); errors.As(err, &cliErr) && cliErr.stderr == "" {
r = strings.NewReader(cliErr.stdout)
}
if params.Writer != nil && r != nil {
if _, ioErr := io.Copy(params.Writer, r); ioErr != nil {
Expand Down Expand Up @@ -405,8 +405,8 @@ func (c *Client) runCommand(ctx context.Context, args []string) (io.Reader, erro
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return nil, cliError{
summary: strings.TrimSpace(stderr.String()),
detail: strings.TrimSpace(stdout.String()),
stderr: strings.TrimSpace(stderr.String()),
stdout: strings.TrimSpace(stdout.String()),
}
}
return &stdout, nil
Expand Down Expand Up @@ -462,30 +462,16 @@ func TempFile(content, ext string) (string, func() error, error) {
}

type cliError struct {
summary string
detail string
stdout string
stderr string
}

// Error implements the error interface.
func (e cliError) Error() string {
s, d := e.Summary(), e.Detail()
if d != "" {
return fmt.Sprintf("atlasexec: %s, %s", s, d)
if e.stderr != "" {
return e.stderr
}
return fmt.Sprintf("atlasexec: %s", s)
}

// Summary implements the diag.Diagnostic interface.
func (e cliError) Summary() string {
if strings.HasPrefix(e.summary, "Error: ") {
return e.summary[7:]
}
return e.summary
}

// Detail implements the diag.Diagnostic interface.
func (e cliError) Detail() string {
return strings.TrimSpace(e.detail)
return e.stdout
}

func (v Vars) AsArgs() []string {
Expand Down Expand Up @@ -518,8 +504,7 @@ func jsonDecode[T any](r io.Reader, err error) (*T, error) {
var dst T
if err = json.Unmarshal(buf, &dst); err != nil {
return nil, cliError{
summary: "Atlas CLI",
detail: strings.TrimSpace(string(buf)),
stdout: string(buf),
}
}
return &dst, nil
Expand Down
2 changes: 1 addition & 1 deletion 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.ErrorContains(t, err, `atlasexec: required flag "url" not set`)
require.ErrorContains(t, err, `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

0 comments on commit 7edfcfc

Please sign in to comment.