diff --git a/atlasaction/action_test.go b/atlasaction/action_test.go index f83ed665..6a63a5c1 100644 --- a/atlasaction/action_test.go +++ b/atlasaction/action_test.go @@ -27,6 +27,7 @@ import ( "ariga.io/atlas-action/atlasaction" "ariga.io/atlas-action/atlasaction/cloud" + "ariga.io/atlas-action/internal/cmdapi" "ariga.io/atlas-go-sdk/atlasexec" "ariga.io/atlas/sql/migrate" _ "github.com/mattn/go-sqlite3" @@ -2181,12 +2182,22 @@ func renderTemplate[T any](ts *testscript.TestScript, neg bool, args []string) { ts.Check(err) } +func TestMain(m *testing.M) { + os.Exit(testscript.RunMain(m, map[string]func() int{ + "atlas-action": func() int { + return cmdapi.Main(context.Background(), "testscript", "dev") + }, + })) +} + func TestGitHubActions(t *testing.T) { var ( actions = "actions" output = filepath.Join(actions, "output.txt") summary = filepath.Join(actions, "summary.txt") ) + wd, err := os.Getwd() + require.NoError(t, err) testscript.Run(t, testscript.Params{ Dir: filepath.Join("testdata", "github"), Setup: func(e *testscript.Env) (err error) { @@ -2194,21 +2205,14 @@ func TestGitHubActions(t *testing.T) { if err := os.Mkdir(dir, 0700); err != nil { return err } + e.Setenv("MOCK_ATLAS", filepath.Join(wd, "mock-atlas.sh")) e.Setenv("GITHUB_ACTIONS", "true") e.Setenv("GITHUB_ENV", filepath.Join(dir, "env.txt")) e.Setenv("GITHUB_OUTPUT", filepath.Join(dir, "output.txt")) e.Setenv("GITHUB_STEP_SUMMARY", filepath.Join(dir, "summary.txt")) - c, err := atlasexec.NewClient(e.WorkDir, "atlas") - if err != nil { - return err - } - // Create a new actions for each test. - e.Values[atlasKey{}] = &atlasClient{c} return nil }, Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ - "atlas-action": atlasAction, - "mock-atlas": mockAtlasOutput, "summary": func(ts *testscript.TestScript, neg bool, args []string) { if len(args) == 0 { _, err := os.Stat(ts.MkAbs(summary)) @@ -2247,63 +2251,6 @@ func TestGitHubActions(t *testing.T) { }) } -type ( - atlasKey struct{} - atlasClient struct { - atlasaction.AtlasExec - } -) - -func atlasAction(ts *testscript.TestScript, neg bool, args []string) { - if len(args) != 1 { - ts.Fatalf("usage: atlas-action ") - } - client, ok := ts.Value(atlasKey{}).(*atlasClient) - if !ok || client == nil { - ts.Fatalf("client not found") - } - // The action need to be create for each call to read correct inputs - act, err := atlasaction.New( - atlasaction.WithGetenv(ts.Getenv), - atlasaction.WithOut(ts.Stdout()), - atlasaction.WithAtlas(client.AtlasExec), - atlasaction.WithVersion("testscript"), - ) - ts.Check(err) - action := args[0] - ts.Setenv("ATLAS_ACTION_COMMAND", action) - ts.Defer(func() { - ts.Setenv("ATLAS_ACTION_COMMAND", "") - }) - // Run the action! - switch err := act.Run(context.Background(), action); { - case !neg: - ts.Check(err) - case err == nil: - ts.Fatalf("expected fail") - case neg: - // Print the error to asserting on the testscript - fmt.Fprint(ts.Stderr(), err.Error()) - } -} - -func mockAtlasOutput(ts *testscript.TestScript, neg bool, args []string) { - if len(args) != 1 { - ts.Fatalf("usage: mock-atlas ") - } - client, ok := ts.Value(atlasKey{}).(*atlasClient) - if !ok || client == nil { - ts.Fatalf("client not found") - } - m, err := atlasexec.NewClient("", "./mock-atlas.sh") - ts.Check(err) - ts.Check(m.SetEnv(map[string]string{ - "TEST_BATCH": args[0], - })) - // Replace the atlas client with a mock client. - client.AtlasExec = m -} - func cmpFiles(ts *testscript.TestScript, neg bool, name1, name2 string) { text1 := ts.ReadFile(name1) data, err := os.ReadFile(ts.MkAbs(name2)) diff --git a/atlasaction/bitbucket.go b/atlasaction/bitbucket.go index 625ac4f8..7026a91e 100644 --- a/atlasaction/bitbucket.go +++ b/atlasaction/bitbucket.go @@ -18,7 +18,7 @@ import ( "strings" "testing" - "ariga.io/atlas-action/atlasaction/internal/bitbucket" + "ariga.io/atlas-action/internal/bitbucket" "ariga.io/atlas-go-sdk/atlasexec" "github.com/fatih/color" "golang.org/x/oauth2" diff --git a/atlasaction/bitbucket_test.go b/atlasaction/bitbucket_test.go index 8af9e5b1..eab82134 100644 --- a/atlasaction/bitbucket_test.go +++ b/atlasaction/bitbucket_test.go @@ -5,8 +5,8 @@ import ( "path/filepath" "testing" - "ariga.io/atlas-go-sdk/atlasexec" "github.com/rogpeppe/go-internal/testscript" + "github.com/stretchr/testify/require" ) func TestBitbucketPipe(t *testing.T) { @@ -14,6 +14,8 @@ func TestBitbucketPipe(t *testing.T) { actions = "actions" outputs = filepath.Join("outputs.sh") ) + wd, err := os.Getwd() + require.NoError(t, err) testscript.Run(t, testscript.Params{ Dir: filepath.Join("testdata", "bitbucket"), Setup: func(e *testscript.Env) (err error) { @@ -21,20 +23,13 @@ func TestBitbucketPipe(t *testing.T) { if err := os.Mkdir(dir, 0700); err != nil { return err } + e.Setenv("MOCK_ATLAS", filepath.Join(wd, "mock-atlas.sh")) e.Setenv("BITBUCKET_PIPELINE_UUID", "fbfb4205-c666-42ed-983a-d27f47f2aad2") e.Setenv("BITBUCKET_PIPE_STORAGE_DIR", dir) e.Setenv("BITBUCKET_CLONE_DIR", e.WorkDir) - c, err := atlasexec.NewClient(e.WorkDir, "atlas") - if err != nil { - return err - } - // Create a new actions for each test. - e.Values[atlasKey{}] = &atlasClient{c} return nil }, Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ - "atlas-action": atlasAction, - "mock-atlas": mockAtlasOutput, "output": func(ts *testscript.TestScript, neg bool, args []string) { if len(args) == 0 { _, err := os.Stat(ts.MkAbs(outputs)) diff --git a/atlasaction/circleci_action_test.go b/atlasaction/circleci_action_test.go index add241ec..0775993b 100644 --- a/atlasaction/circleci_action_test.go +++ b/atlasaction/circleci_action_test.go @@ -13,7 +13,6 @@ import ( "testing" "ariga.io/atlas-action/atlasaction" - "ariga.io/atlas-go-sdk/atlasexec" "github.com/rogpeppe/go-internal/testscript" "github.com/stretchr/testify/require" ) @@ -63,6 +62,8 @@ func TestCircleCI(t *testing.T) { actions = "actions" output = filepath.Join(actions, "output.txt") ) + wd, err := os.Getwd() + require.NoError(t, err) testscript.Run(t, testscript.Params{ Dir: filepath.Join("testdata", "circleci"), Setup: func(e *testscript.Env) (err error) { @@ -70,21 +71,14 @@ func TestCircleCI(t *testing.T) { if err := os.Mkdir(dir, 0700); err != nil { return err } + e.Setenv("MOCK_ATLAS", filepath.Join(wd, "mock-atlas.sh")) e.Setenv("CIRCLECI", "true") e.Setenv("CIRCLE_PROJECT_REPONAME", "atlas-orb") e.Setenv("CIRCLE_SHA1", "1234567890") e.Setenv("BASH_ENV", filepath.Join(dir, "output.txt")) - c, err := atlasexec.NewClient(e.WorkDir, "atlas") - if err != nil { - return err - } - // Create a new actions for each test. - e.Values[atlasKey{}] = &atlasClient{c} return nil }, Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ - "atlas-action": atlasAction, - "mock-atlas": mockAtlasOutput, "output": func(ts *testscript.TestScript, neg bool, args []string) { if len(args) == 0 { _, err := os.Stat(ts.MkAbs(output)) diff --git a/atlasaction/gitlab_ci_test.go b/atlasaction/gitlab_ci_test.go index b3a7befa..3d4704d1 100644 --- a/atlasaction/gitlab_ci_test.go +++ b/atlasaction/gitlab_ci_test.go @@ -10,9 +10,9 @@ import ( "testing" "ariga.io/atlas-action/atlasaction" - "ariga.io/atlas-go-sdk/atlasexec" "github.com/gorilla/mux" "github.com/rogpeppe/go-internal/testscript" + "github.com/stretchr/testify/require" ) func newMockHandler(dir string) http.Handler { @@ -88,6 +88,8 @@ func newMockHandler(dir string) http.Handler { } func TestGitlabCI(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) testscript.Run(t, testscript.Params{ Dir: "testdata/gitlab", Setup: func(e *testscript.Env) error { @@ -97,21 +99,14 @@ func TestGitlabCI(t *testing.T) { return err } e.Defer(srv.Close) + e.Setenv("MOCK_ATLAS", filepath.Join(wd, "mock-atlas.sh")) e.Setenv("CI_API_V4_URL", srv.URL) e.Setenv("CI_PROJECT_ID", "1") e.Setenv("GITLAB_CI", "true") e.Setenv("GITLAB_TOKEN", "token") - c, err := atlasexec.NewClient(e.WorkDir, "atlas") - if err != nil { - return err - } - // Create a new actions for each test. - e.Values[atlasKey{}] = &atlasClient{c} return nil }, Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ - "atlas-action": atlasAction, - "mock-atlas": mockAtlasOutput, "output": func(ts *testscript.TestScript, neg bool, args []string) { if len(args) == 0 { _, err := os.Stat(ts.MkAbs(".env")) diff --git a/atlasaction/testdata/bitbucket/schema-push.txtar b/atlasaction/testdata/bitbucket/schema-push.txtar index 29115ce2..7112c018 100644 --- a/atlasaction/testdata/bitbucket/schema-push.txtar +++ b/atlasaction/testdata/bitbucket/schema-push.txtar @@ -1,15 +1,15 @@ # Mock atlas command outputs -mock-atlas $WORK/schema-push +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./schema-push # Setup the action input variables env ATLAS_INPUT_ENV=test env ATLAS_INPUT_LATEST=true # Run the action without a tag -atlas-action schema/push +atlas-action --action=schema/push # Run the action with a tag env ATLAS_INPUT_TAG=98765 -atlas-action schema/push +atlas-action --action=schema/push exec cat .atlas-action/outputs.sh output outputs.sh diff --git a/atlasaction/testdata/circleci/migrate-lint.txtar b/atlasaction/testdata/circleci/migrate-lint.txtar index c823e203..a6041936 100644 --- a/atlasaction/testdata/circleci/migrate-lint.txtar +++ b/atlasaction/testdata/circleci/migrate-lint.txtar @@ -1,5 +1,5 @@ # Mock the atlas command outputs -mock-atlas $WORK/migrate-lint +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./migrate-lint # Setup the action input variables env INPUT_CONFIG=file://testdata/config/atlas.hcl env INPUT_ENV=test @@ -12,7 +12,7 @@ env INPUT_RUN=example # The action's output should append the existing outputs cp output-pre.txt actions/output.txt -atlas-action migrate/lint +atlas-action --action=migrate/lint output output.txt -- migrate-lint/1/args -- diff --git a/atlasaction/testdata/github/migrate-lint.txtar b/atlasaction/testdata/github/migrate-lint.txtar index 4940ce0c..af95f2f1 100644 --- a/atlasaction/testdata/github/migrate-lint.txtar +++ b/atlasaction/testdata/github/migrate-lint.txtar @@ -1,5 +1,5 @@ # Mock the atlas command outputs -mock-atlas $WORK/migrate-lint +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./migrate-lint # Setup the action input variables env INPUT_CONFIG=file://testdata/config/atlas.hcl env INPUT_ENV=test @@ -10,7 +10,7 @@ env INPUT_DIR=file://testdata/migrations env INPUT_DEV_URL=sqlite://file?mode=memory env INPUT_RUN=example -atlas-action migrate/lint +atlas-action --action=migrate/lint output output.txt -- migrate-lint/1/args -- diff --git a/atlasaction/testdata/github/migrate-test.txtar b/atlasaction/testdata/github/migrate-test.txtar index 67cc6a0f..091f5200 100644 --- a/atlasaction/testdata/github/migrate-test.txtar +++ b/atlasaction/testdata/github/migrate-test.txtar @@ -1,5 +1,5 @@ # Mock the atlas command outputs -mock-atlas $WORK/migrate-test +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./migrate-test # Setup the action input variables env INPUT_CONFIG=file://testdata/config/atlas.hcl env INPUT_ENV=test @@ -8,14 +8,13 @@ env INPUT_DIR=file://testdata/migrations env INPUT_DEV_URL=sqlite://file?mode=memory env INPUT_RUN=example -atlas-action migrate/test +atlas-action --action=migrate/test stdout '`atlas migrate test` completed successfully, no issues found' stdout 'Success' ! output -! atlas-action migrate/test -stderr '`atlas migrate test` completed with errors:' -stderr 'Failure' +! atlas-action --action=migrate/test +stdout '::error::`atlas migrate test` completed with errors:%0AFailure' ! output -- migrate-test/1/args -- diff --git a/atlasaction/testdata/github/schema-apply-envs.txtar b/atlasaction/testdata/github/schema-apply-envs.txtar index 67d9e5fb..0dd3f2c4 100644 --- a/atlasaction/testdata/github/schema-apply-envs.txtar +++ b/atlasaction/testdata/github/schema-apply-envs.txtar @@ -1,10 +1,10 @@ # Mock the atlas command outputs -mock-atlas $WORK/schema-apply +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./schema-apply # Setup the action input variables env INPUT_ENV=test env INPUT_PLAN=file://20240910173744.plan.hcl # Run the action -! atlas-action schema/apply +! atlas-action --action=schema/apply stdout '"atlas schema apply" completed successfully on the target "sqlite://local-bu.db"' stdout '"atlas schema apply" completed successfully on the target "sqlite://local-pi.db"' stdout '"atlas schema apply" completed successfully on the target "sqlite://local-su.db"' diff --git a/atlasaction/testdata/github/schema-apply-lint.txtar b/atlasaction/testdata/github/schema-apply-lint.txtar index 4f35e02e..f46724cc 100644 --- a/atlasaction/testdata/github/schema-apply-lint.txtar +++ b/atlasaction/testdata/github/schema-apply-lint.txtar @@ -1,9 +1,9 @@ # Mock the atlas command outputs -mock-atlas $WORK/schema-apply +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./schema-apply # Setup the action input variables env INPUT_ENV=test # Run the action -! atlas-action schema/apply +! atlas-action --action=schema/apply summary summary.html output output.txt diff --git a/atlasaction/testdata/github/schema-push.txtar b/atlasaction/testdata/github/schema-push.txtar index e3f07700..6dedb4f7 100644 --- a/atlasaction/testdata/github/schema-push.txtar +++ b/atlasaction/testdata/github/schema-push.txtar @@ -1,15 +1,15 @@ # Mock atlas command outputs -mock-atlas $WORK/schema-push +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./schema-push # Setup the action input variables env INPUT_ENV=test env INPUT_LATEST=true # Run the action without a tag -atlas-action schema/push +atlas-action --action=schema/push # Run the action with a tag env INPUT_TAG=98765 -atlas-action schema/push +atlas-action --action=schema/push -- schema-push/1/args -- schema push --format {{ json . }} --env test --context {"scmType":"GITHUB"} --tag latest diff --git a/atlasaction/testdata/github/schema-test.txtar b/atlasaction/testdata/github/schema-test.txtar index 8405ea20..69336cbf 100644 --- a/atlasaction/testdata/github/schema-test.txtar +++ b/atlasaction/testdata/github/schema-test.txtar @@ -1,5 +1,5 @@ # Mock the atlas command outputs -mock-atlas $WORK/schema-test +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./schema-test # Setup the action input variables env INPUT_CONFIG=file://testdata/config/atlas.hcl env INPUT_ENV=test @@ -7,14 +7,13 @@ env INPUT_VARS='{"var1":"value1","var2":"value2"}' env INPUT_DEV_URL=sqlite://file?mode=memory env INPUT_RUN=example -atlas-action schema/test +atlas-action --action=schema/test stdout '`atlas schema test` completed successfully, no issues found' stdout 'Success' ! output -! atlas-action schema/test -stderr '`atlas schema test` completed with errors:' -stderr 'Failure' +! atlas-action --action=schema/test +stdout '::error::`atlas schema test` completed with errors:%0AFailure' ! output -- schema-test/1/args -- diff --git a/atlasaction/testdata/gitlab/schema-plan-approve.txtar b/atlasaction/testdata/gitlab/schema-plan-approve.txtar index cd192034..edaeff43 100644 --- a/atlasaction/testdata/gitlab/schema-plan-approve.txtar +++ b/atlasaction/testdata/gitlab/schema-plan-approve.txtar @@ -1,21 +1,26 @@ -mock-atlas $WORK/schema-plan-approve +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./schema-plan-approve # No plans found. -atlas-action schema/plan/approve +atlas-action --action=schema/plan/approve stdout 'No plan URL provided, searching for the pending plan' stdout 'No schema plan found' # One pending plan. -atlas-action schema/plan/approve +atlas-action --action=schema/plan/approve +cmp .env expected.env # Multiple pending plans. -! atlas-action schema/plan/approve +! atlas-action --action=schema/plan/approve stdout 'No plan URL provided, searching for the pending plan' stdout 'Found schema plan: atlas://plans/1234' stdout 'Found schema plan: atlas://plans/5678' -stderr 'found multiple schema plans, please approve or delete the existing plans' - +stdout 'found multiple schema plans, please approve or delete the existing plans' +cmp .env expected.env +-- expected.env -- +link=https://test.atlasgo.cloud/schemas/123/plans/456 +plan=atlas://plans/1234 +status= -- schema-plan-approve/1/args -- schema plan list --format {{ json . }} --context {"scmType":"GITLAB"} --pending --auto-approve diff --git a/atlasaction/testdata/gitlab/schema-plan.txtar b/atlasaction/testdata/gitlab/schema-plan.txtar index 7aff1c77..d3acb919 100644 --- a/atlasaction/testdata/gitlab/schema-plan.txtar +++ b/atlasaction/testdata/gitlab/schema-plan.txtar @@ -1,7 +1,7 @@ -! atlas-action schema/plan -stderr 'the action should be run in a pull request context' +! atlas-action --action=schema/plan +stdout 'the action should be run in a pull request context' -mock-atlas $WORK/schema-plan +env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./schema-plan env CI_MERGE_REQUEST_IID=1 env CI_PROJECT_NAME=my-project env CI_PROJECT_URL=https://gitlab.com/projects/my-project @@ -11,11 +11,16 @@ env GITLAB_USER_NAME=user env GITLAB_USER_ID=123 env CI_MERGE_REQUEST_REF_PATH=$CI_PROJECT_URL/merge_requests/1 -atlas-action schema/plan +atlas-action --action=schema/plan stdout 'Schema plan does not exist, creating a new one with name "pr-1-3RRRcLHF"' cmp comments-expected/1 comments/1 +cmp .env expected.env +-- expected.env -- +link=http://test.atlasgo.cloud/schemas/141733920769/plans/210453397511 +plan=atlas://app/plans/20241010143904 +status=PENDING -- schema-plan/1/args -- schema plan list --format {{ json . }} --context {"repo":"my-project","branch":"test-branch","commit":"123","url":"https://gitlab.com/projects/my-project/merge_requests/1","username":"user","userID":"123","scmType":"GITLAB"} --pending --auto-approve diff --git a/cmd/atlas-action/main.go b/cmd/atlas-action/main.go index 47b0d7eb..f94ff91b 100644 --- a/cmd/atlas-action/main.go +++ b/cmd/atlas-action/main.go @@ -6,13 +6,9 @@ package main import ( "context" - "errors" - "fmt" "os" - "ariga.io/atlas-action/atlasaction" - "ariga.io/atlas-action/atlasaction/cloud" - "github.com/alecthomas/kong" + "ariga.io/atlas-action/internal/cmdapi" ) var ( @@ -25,56 +21,5 @@ var ( ) func main() { - atlasPath := "atlas" - if p := os.Getenv("ATLAS_PATH"); p != "" { - // The environment can provide the path to the atlas-cli binary. - // - // This is useful when running the action in a Docker container, - // and the user can mount the atlas-cli binary in the container and set the - // ATLAS_PATH environment variable to the path of the binary. - atlasPath = p - } - act, err := atlasaction.New( - atlasaction.WithAtlasPath(atlasPath), - atlasaction.WithCloudClient(cloud.New), - atlasaction.WithVersion(version), - ) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to run action in the current environment: %s\n", err) - os.Exit(1) - } - cli := kong.Parse( - &RunActionCmd{}, - kong.BindTo(context.Background(), (*context.Context)(nil)), - ) - if err := cli.Run(act); err != nil { - if uerr := errors.Unwrap(err); uerr != nil { - err = uerr - } - act.Fatalf(err.Error()) - } -} - -// VersionFlag is a flag type that can be used to display a version number, stored in the "version" variable. -type VersionFlag bool - -// BeforeReset writes the version variable and terminates with a 0 exit status. -func (v VersionFlag) BeforeReset(app *kong.Kong) error { - _, err := fmt.Fprintf(app.Stdout, "atlas-action version %s-%s\n", version, commit) - app.Exit(0) - return err -} - -// RunActionCmd is a command to run one of the Atlas GitHub Actions. -type RunActionCmd struct { - Action string `help:"Command to run" required:"" env:"ATLAS_ACTION"` - Version VersionFlag `help:"Prints the version and exits"` -} - -func (r *RunActionCmd) Run(ctx context.Context, a *atlasaction.Actions) error { - _ = os.Setenv("ATLAS_ACTION_COMMAND", r.Action) - defer func() { - _ = os.Unsetenv("ATLAS_ACTION_COMMAND") - }() - return a.Run(ctx, r.Action) + os.Exit(cmdapi.Main(context.Background(), version, commit)) } diff --git a/atlasaction/internal/bitbucket/bitbucket.go b/internal/bitbucket/bitbucket.go similarity index 100% rename from atlasaction/internal/bitbucket/bitbucket.go rename to internal/bitbucket/bitbucket.go diff --git a/internal/cmdapi/cmdapi.go b/internal/cmdapi/cmdapi.go new file mode 100644 index 00000000..2a345432 --- /dev/null +++ b/internal/cmdapi/cmdapi.go @@ -0,0 +1,65 @@ +// Copyright 2021-present The Atlas Authors. All rights reserved. +// This source code is licensed under the Apache 2.0 license found +// in the LICENSE file in the root directory of this source tree. + +package cmdapi + +import ( + "context" + "errors" + "fmt" + "os" + + "ariga.io/atlas-action/atlasaction" + "ariga.io/atlas-action/atlasaction/cloud" + "github.com/alecthomas/kong" +) + +func Main(ctx context.Context, version, commit string) int { + atlasPath := "atlas" + if p := os.Getenv("ATLAS_PATH"); p != "" { + // The environment can provide the path to the atlas-cli binary. + // + // This is useful when running the action in a Docker container, + // and the user can mount the atlas-cli binary in the container and set the + // ATLAS_PATH environment variable to the path of the binary. + atlasPath = p + } + act, err := atlasaction.New( + atlasaction.WithAtlasPath(atlasPath), + atlasaction.WithCloudClient(cloud.New), + atlasaction.WithVersion(version), + ) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to run action in the current environment: %s\n", err) + os.Exit(1) + } + cli := kong.Parse( + &RunActionCmd{}, + kong.BindTo(ctx, (*context.Context)(nil)), + kong.Vars{ + "version": fmt.Sprintf("atlas-action version %s-%s\n", version, commit), + }, + ) + if err := cli.Run(act); err != nil { + if uerr := errors.Unwrap(err); uerr != nil { + err = uerr + } + act.Fatalf(err.Error()) + } + return 0 +} + +// RunActionCmd is a command to run one of the Atlas GitHub Actions. +type RunActionCmd struct { + Action string `help:"Command to run" required:"" env:"ATLAS_ACTION"` + Version kong.VersionFlag `name:"version" help:"Print version information and quit"` +} + +func (r *RunActionCmd) Run(ctx context.Context, a *atlasaction.Actions) error { + _ = os.Setenv("ATLAS_ACTION_COMMAND", r.Action) + defer func() { + _ = os.Unsetenv("ATLAS_ACTION_COMMAND") + }() + return a.Run(ctx, r.Action) +} diff --git a/cmd/atlas-action/main_test.go b/internal/cmdapi/cmdapi_test.go similarity index 87% rename from cmd/atlas-action/main_test.go rename to internal/cmdapi/cmdapi_test.go index d28cadda..4442d826 100644 --- a/cmd/atlas-action/main_test.go +++ b/internal/cmdapi/cmdapi_test.go @@ -2,7 +2,7 @@ // This source code is licensed under the Apache 2.0 license found // in the LICENSE file in the root directory of this source tree. -package main +package cmdapi_test import ( "context" @@ -10,6 +10,7 @@ import ( "testing" "ariga.io/atlas-action/atlasaction" + "ariga.io/atlas-action/internal/cmdapi" "ariga.io/atlas-go-sdk/atlasexec" "github.com/stretchr/testify/require" ) @@ -19,7 +20,7 @@ func TestRunAction_Run(t *testing.T) { require.NoError(t, err) act := atlasaction.NewGHAction(os.Getenv, os.Stdout) t.Run("fake", func(t *testing.T) { - r := &RunActionCmd{Action: "fake"} + r := &cmdapi.RunActionCmd{Action: "fake"} c, err := atlasaction.New(atlasaction.WithAction(act), atlasaction.WithAtlas(client)) require.NoError(t, err) err = r.Run(context.Background(), c)