From 7075c2cc67af3ae487b7ba89da5b104175f24446 Mon Sep 17 00:00:00 2001 From: dorav Date: Tue, 26 Sep 2023 15:09:40 +0300 Subject: [PATCH] added MigrateLintError --- atlasexec/atlas.go | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/atlasexec/atlas.go b/atlasexec/atlas.go index f9521fc..777d901 100644 --- a/atlasexec/atlas.go +++ b/atlasexec/atlas.go @@ -68,6 +68,7 @@ type ( Web bool Latest uint64 Vars Vars + Writer io.Writer } // SchemaApplyParams are the parameters for the `schema apply` command. SchemaApplyParams struct { @@ -283,9 +284,13 @@ func (c *Client) SchemaInspect(ctx context.Context, params *SchemaInspectParams) return stringVal(c.runCommand(ctx, args)) } -// MigrateLint runs the 'migrate lint' command. -func (c *Client) MigrateLint(ctx context.Context, params *MigrateLintParams) (*SummaryReport, error) { - args := []string{"migrate", "lint", "--format", "{{ json . }}"} +func lintArgs(params *MigrateLintParams) []string { + args := []string{"migrate", "lint"} + if params.Web { + args = append(args, "-w") + } else { + args = append(args, "--format", "{{ json . }}") + } if params.Env != "" { args = append(args, "--env", params.Env) } @@ -301,14 +306,34 @@ func (c *Client) MigrateLint(ctx context.Context, params *MigrateLintParams) (*S if params.BaseURL != "" { args = append(args, "--base", params.BaseURL) } - if params.Web { - args = append(args, "-w") - } if params.Latest > 0 { args = append(args, "--latest", strconv.FormatUint(params.Latest, 10)) } args = append(args, params.Vars.AsArgs()...) - return jsonDecode[SummaryReport](c.runCommand(ctx, args, validJSON)) + return args +} + +// MigrateLint runs the 'migrate lint' command. +func (c *Client) MigrateLint(ctx context.Context, params *MigrateLintParams) (*SummaryReport, error) { + if params.Writer != nil { + return nil, errors.New("migrate lint params do not support a custom Writer, consider using MigrateLintErr") + } + newParams := *params + var buf bytes.Buffer + newParams.Writer = &buf + err := c.MigrateLintError(ctx, &newParams) + return jsonDecode[SummaryReport](&buf, err) +} + +// MigrateLintError runs the 'migrate lint' command, the output is written to params.Writer +func (c *Client) MigrateLintError(ctx context.Context, params *MigrateLintParams) error { + args := lintArgs(params) + reader, err := c.runCommand(ctx, args, validJSON) + if err != nil { + return err + } + _, err = io.Copy(params.Writer, reader) + return err } // MigrateStatus runs the 'migrate status' command.