Skip to content

Commit

Permalink
atlasexec: update schema/push output (#95)
Browse files Browse the repository at this point in the history
* atlasexec: update schema/push output

* chore: added `slug`

* chore: run command with json format
  • Loading branch information
giautm authored Sep 19, 2024
1 parent 5d10c68 commit d312a53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
14 changes: 10 additions & 4 deletions atlasexec/atlas_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type (
Version string // Version of the schema to push. Defaults to the current timestamp.
Description string // Description of the schema changes.
}
// SchemaPush represents the result of a 'schema push' command.
SchemaPush struct {
Link string
Slug string
URL string
}
// SchemaApplyParams are the parameters for the `schema apply` command.
SchemaApplyParams struct {
ConfigURL string
Expand Down Expand Up @@ -205,8 +211,8 @@ type (
)

// SchemaPush runs the 'schema push' command.
func (c *Client) SchemaPush(ctx context.Context, params *SchemaPushParams) (string, error) {
args := []string{"schema", "push"}
func (c *Client) SchemaPush(ctx context.Context, params *SchemaPushParams) (*SchemaPush, error) {
args := []string{"schema", "push", "--format", "{{ json . }}"}
// Global flags
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
Expand All @@ -221,7 +227,7 @@ func (c *Client) SchemaPush(ctx context.Context, params *SchemaPushParams) (stri
if params.Context != nil {
buf, err := json.Marshal(params.Context)
if err != nil {
return "", err
return nil, err
}
args = append(args, "--context", string(buf))
}
Expand All @@ -241,7 +247,7 @@ func (c *Client) SchemaPush(ctx context.Context, params *SchemaPushParams) (stri
if params.Repo != "" {
args = append(args, params.Repo)
}
return stringVal(c.runCommand(ctx, args))
return firstResult(jsonDecode[SchemaPush](c.runCommand(ctx, args)))
}

// SchemaApply runs the 'schema apply' command.
Expand Down
16 changes: 9 additions & 7 deletions atlasexec/atlas_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,22 +536,22 @@ func TestSchema_Push(t *testing.T) {
{
name: "no params",
params: &atlasexec.SchemaPushParams{},
args: "schema push",
args: "schema push --format {{ json . }}",
},
{
name: "with repo",
params: &atlasexec.SchemaPushParams{
Repo: "atlas-action",
},
args: "schema push atlas-action",
args: "schema push --format {{ json . }} atlas-action",
},
{
name: "with repo and tag",
params: &atlasexec.SchemaPushParams{
Repo: "atlas-action",
Tag: "v1.0.0",
},
args: "schema push --tag v1.0.0 atlas-action",
args: "schema push --format {{ json . }} --tag v1.0.0 atlas-action",
},
{
name: "with repo and tag and description",
Expand All @@ -560,7 +560,7 @@ func TestSchema_Push(t *testing.T) {
Tag: "v1.0.0",
Description: "release-v1",
},
args: "schema push --tag v1.0.0 --desc release-v1 atlas-action",
args: "schema push --format {{ json . }} --tag v1.0.0 --desc release-v1 atlas-action",
},
{
name: "with repo and tag, version and description",
Expand All @@ -570,16 +570,18 @@ func TestSchema_Push(t *testing.T) {
Version: "20240829100417",
Description: "release-v1",
},
args: "schema push --tag v1.0.0 --version 20240829100417 --desc release-v1 atlas-action",
args: "schema push --format {{ json . }} --tag v1.0.0 --version 20240829100417 --desc release-v1 atlas-action",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("TEST_ARGS", tt.args)
t.Setenv("TEST_STDOUT", `https://gh.atlasgo.cloud/schemas/141733920810`)
t.Setenv("TEST_STDOUT", `{"Link":"https://gh.atlasgo.cloud/schemas/141733920810","Slug":"awesome-app","URL":"atlas://awesome-app?tag=latest"}`)
result, err := c.SchemaPush(context.Background(), tt.params)
require.NoError(t, err)
require.Equal(t, "https://gh.atlasgo.cloud/schemas/141733920810", result)
require.Equal(t, "https://gh.atlasgo.cloud/schemas/141733920810", result.Link)
require.Equal(t, "atlas://awesome-app?tag=latest", result.URL)
require.Equal(t, "awesome-app", result.Slug)
})
}
}
Expand Down

0 comments on commit d312a53

Please sign in to comment.