diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42afc6a..d569431 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -142,6 +142,12 @@ jobs: with: terraform_version: ${{ matrix.terraform-version }} terraform_wrapper: false + - name: Install Atlas CLI + run: | + curl -sSf https://atlasgo.sh | sh + env: + ATLAS_DEBUG: "true" + ATLAS_FLAVOR: enterprise - name: Terraform (sqlite) working-directory: integration-tests/sqlite run: | @@ -224,6 +230,12 @@ jobs: with: terraform_version: ${{ matrix.terraform-version }} terraform_wrapper: false + - name: Install Atlas CLI + run: | + curl -sSf https://atlasgo.sh | sh + env: + ATLAS_DEBUG: "true" + ATLAS_FLAVOR: enterprise - name: Terraform (skip-policy) working-directory: integration-tests/skip-policy run: | @@ -241,7 +253,7 @@ jobs: ! cat stdout.txt | grep --silent "DROP TABLE \`test\`.\`t2\`" terraform apply --auto-approve -no-color -var="schema=schema-t2" -var="skip_drop_table=true" # Confirm that the table was not dropped - ../../scripts/atlas-cli.sh 0.0.0-pre.0 schema inspect \ + atlas schema inspect \ -u "mysql://root:pass@localhost:3306/" > actual.hcl cmp -s "expected.hcl" "actual.hcl" integration-postgres: @@ -292,6 +304,12 @@ jobs: with: terraform_version: ${{ matrix.terraform-version }} terraform_wrapper: false + - name: Install Atlas CLI + run: | + curl -sSf https://atlasgo.sh | sh + env: + ATLAS_DEBUG: "true" + ATLAS_FLAVOR: enterprise - name: Terraform (concurrent_index-policy) working-directory: integration-tests/concurrent_index-policy run: | @@ -312,6 +330,11 @@ jobs: MSSQL_SA_PASSWORD: P@ssw0rd0995 ports: - 1433:1433 + options: >- + --health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P \"${MSSQL_SA_PASSWORD}\" -Q \"SELECT 1\"" + --health-interval 10s + --health-timeout 5s + --health-retries 5 sqlserver-dev: image: mcr.microsoft.com/mssql/server:2022-latest env: @@ -320,6 +343,11 @@ jobs: MSSQL_SA_PASSWORD: P@ssw0rd0995 ports: - 1434:1433 + options: >- + --health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P \"${MSSQL_SA_PASSWORD}\" -Q \"SELECT 1\"" + --health-interval 10s + --health-timeout 5s + --health-retries 5 strategy: fail-fast: false matrix: @@ -339,6 +367,12 @@ jobs: with: terraform_version: ${{ matrix.terraform-version }} terraform_wrapper: false + - name: Install Atlas CLI + run: | + curl -sSf https://atlasgo.sh | sh + env: + ATLAS_DEBUG: "true" + ATLAS_FLAVOR: enterprise - name: Terraform (login-feature) working-directory: integration-tests/login-feature run: | diff --git a/.goreleaser.yml b/.goreleaser.yml index b4c8b33..b9b5cfc 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -27,9 +27,6 @@ builds: - goos: windows goarch: arm64 binary: '{{ .ProjectName }}_v{{ .Version }}' - hooks: - post: - - ./scripts/atlas.sh {{ .Os }}-{{ .Arch }} {{ dir .Path }}/atlas{{ .Ext }} archives: - format: zip files: diff --git a/docs/index.md b/docs/index.md index f760d99..04f3b66 100644 --- a/docs/index.md +++ b/docs/index.md @@ -35,6 +35,7 @@ resource "atlas_schema" "market" { ### Optional +- `binary_path` (String) The path to the atlas-cli binary. If not set, the provider will look for the binary in the PATH. - `cloud` (Block, Optional) (see [below for nested schema](#nestedblock--cloud)) - `dev_url` (String, Sensitive) The URL of the dev database. This configuration is shared for all resources if there is no config on the resource. diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 0e56866..fdb5043 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -6,10 +6,7 @@ import ( "fmt" "net/url" "os" - "os/exec" - "path" "path/filepath" - "runtime" "strings" "github.com/mitchellh/go-homedir" @@ -34,8 +31,6 @@ type ( AtlasProvider struct { // client is the client used to interact with the Atlas CLI. client *atlas.Client - // dir is the directory where the provider is installed. - dir string // version is set to the provider version on release, "dev" when the // provider is built and ran locally, and "test" when running acceptance // testing. @@ -44,6 +39,8 @@ type ( } // AtlasProviderModel describes the provider data model. AtlasProviderModel struct { + // BinaryPath is the path to the atlas-cli binary. + BinaryPath types.String `tfsdk:"binary_path"` // DevURL is the URL of the dev-db. DevURL types.String `tfsdk:"dev_url"` // Cloud is the Atlas Cloud configuration. @@ -97,15 +94,8 @@ const ( // New returns a new provider. func New(address, version, commit string) func() provider.Provider { - wd, err := os.Getwd() - if err != nil { - panic(err) - } - providersDir := path.Join(wd, ".terraform", "providers") - platform := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH) return func() provider.Provider { return &AtlasProvider{ - dir: path.Join(providersDir, address, version, platform), version: version, } } @@ -126,6 +116,10 @@ func (p *AtlasProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp "cloud": cloudBlock, }, Attributes: map[string]schema.Attribute{ + "binary_path": schema.StringAttribute{ + Description: "The path to the atlas-cli binary. If not set, the provider will look for the binary in the PATH.", + Optional: true, + }, "dev_url": schema.StringAttribute{ Description: "The URL of the dev database. This configuration is shared for all resources if there is no config on the resource.", Optional: true, @@ -137,12 +131,16 @@ func (p *AtlasProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp // Configure implements provider.Provider. func (p *AtlasProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { - atlasPath, err := execPath(p.dir, "atlas") - if err != nil { - resp.Diagnostics.AddError("Unable to find atlas-cli", err.Error()) + var model *AtlasProviderModel + resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) + if resp.Diagnostics.HasError() { return } - c, err := atlas.NewClient("", atlasPath) + binPath := "atlas" + if s := model.BinaryPath.ValueString(); s != "" { + binPath = s + } + c, err := atlas.NewClient("", binPath) if err != nil { resp.Diagnostics.AddError("Failed to create client", err.Error()) return @@ -157,16 +155,9 @@ func (p *AtlasProvider) Configure(ctx context.Context, req provider.ConfigureReq version += "-canary" } tflog.Debug(ctx, "found atlas-cli", map[string]any{ - "path": atlasPath, "version": version, }) p.client = c - - var model *AtlasProviderModel - resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) - if resp.Diagnostics.HasError() { - return - } p.data = providerData{client: c, cloud: model.Cloud, version: p.version} if model != nil { p.data.devURL = model.DevURL.ValueString() @@ -282,19 +273,6 @@ func checkForUpdate(ctx context.Context, version string) (string, error) { return b.String(), nil } -func execPath(dir, name string) (file string, err error) { - if runtime.GOOS == "windows" { - name += ".exe" - } - file = filepath.Join(dir, name) - if _, err = os.Stat(file); err == nil { - return file, nil - } - // If the binary is not in the current directory, - // try to find it in the PATH. - return exec.LookPath(name) -} - // absPath returns the absolute path of a file URL. func absPath(path string) (string, error) { u, err := url.Parse(path) diff --git a/scripts/atlas-cli.sh b/scripts/atlas-cli.sh deleted file mode 100755 index 16790b7..0000000 --- a/scripts/atlas-cli.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -HOSTNAME=registry.terraform.io -NAMESPACE=ariga -TYPE=atlas -VERSION=${1:-0.0.0-pre.0} -TARGET=$(go env GOOS)_$(go env GOARCH) - -PLUGIN_ADDR="${HOSTNAME}/${NAMESPACE}/${TYPE}/${VERSION}/${TARGET}" - -./.terraform/providers/$PLUGIN_ADDR/atlas ${@:2} diff --git a/scripts/atlas.sh b/scripts/atlas.sh deleted file mode 100755 index e711462..0000000 --- a/scripts/atlas.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash -curl -sSf https://atlasgo.sh | \ - sh -s -- -y --no-install --platform $1 --output $2