Skip to content

Commit

Permalink
atlasexec/runCommand: removed the concept of validators (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorav committed Sep 28, 2023
1 parent 9a5834e commit afd1420
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *Client) MigrateApply(ctx context.Context, params *MigrateApplyParams) (
args = append(args, strconv.FormatUint(params.Amount, 10))
}
args = append(args, params.Vars.AsArgs()...)
return jsonDecode[MigrateApply](c.runCommand(ctx, args, validJSON))
return jsonDecode[MigrateApply](c.runCommand(ctx, args))
}

// SchemaApply runs the 'schema apply' command.
Expand Down Expand Up @@ -253,7 +253,7 @@ func (c *Client) SchemaApply(ctx context.Context, params *SchemaApplyParams) (*S
args = append(args, "--exclude", strings.Join(params.Exclude, ","))
}
args = append(args, params.Vars.AsArgs()...)
return jsonDecode[SchemaApply](c.runCommand(ctx, args, validJSON))
return jsonDecode[SchemaApply](c.runCommand(ctx, args))
}

// SchemaInspect runs the 'schema inspect' command.
Expand Down Expand Up @@ -318,7 +318,7 @@ func (c *Client) MigrateLint(ctx context.Context, params *MigrateLintParams) (*S
if params.Writer != nil || params.Web {
return nil, errors.New("atlasexec: Writer or Web reporting are not supported with MigrateLint, use MigrateLintError")
}
r, err := c.runCommand(ctx, lintArgs(params), validJSON)
r, err := c.runCommand(ctx, lintArgs(params))
return jsonDecode[SummaryReport](r, err)
}

Expand Down Expand Up @@ -353,7 +353,7 @@ func (c *Client) MigrateStatus(ctx context.Context, params *MigrateStatusParams)
args = append(args, "--revisions-schema", params.RevisionsSchema)
}
args = append(args, params.Vars.AsArgs()...)
return jsonDecode[MigrateStatus](c.runCommand(ctx, args, validJSON))
return jsonDecode[MigrateStatus](c.runCommand(ctx, args))
}

var reVersion = regexp.MustCompile(`^atlas version v(\d+\.\d+.\d+)-?([a-z0-9]*)?`)
Expand Down Expand Up @@ -384,7 +384,7 @@ func (c *Client) Version(ctx context.Context) (*Version, error) {
}

// runCommand runs the given command and returns its output.
func (c *Client) runCommand(ctx context.Context, args []string, vs ...validator) (io.Reader, error) {
func (c *Client) runCommand(ctx context.Context, args []string) (io.Reader, error) {
var stdout, stderr bytes.Buffer
cmd := exec.CommandContext(ctx, c.execPath, args...)
cmd.Dir = c.workingDir
Expand Down Expand Up @@ -414,12 +414,6 @@ func (c *Client) runCommand(ctx context.Context, args []string, vs ...validator)
return nil, err
}
}
out := stdout.Bytes()
for _, v := range vs {
if err := v(out); err != nil {
return nil, err
}
}
return &stdout, nil
}

Expand Down Expand Up @@ -522,21 +516,16 @@ func jsonDecode[T any](r io.Reader, err error) (*T, error) {
if err != nil {
return nil, err
}
var dst T
if err = json.NewDecoder(r).Decode(&dst); err != nil {
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return &dst, nil
}

type validator func([]byte) error

func validJSON(d []byte) error {
if !json.Valid(d) {
return &cliError{
var dst T
if err = json.Unmarshal(buf, &dst); err != nil {
return nil, cliError{
summary: "Atlas CLI",
detail: strings.TrimSpace(string(d)),
detail: strings.TrimSpace(string(buf)),
}
}
return nil
return &dst, nil
}

0 comments on commit afd1420

Please sign in to comment.