diff --git a/.changes/unreleased/.gitkeep b/.changes/unreleased/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/.changes/unreleased/NOTES-20231010-144258.yaml b/.changes/unreleased/NOTES-20231010-144258.yaml new file mode 100644 index 0000000..68c9823 --- /dev/null +++ b/.changes/unreleased/NOTES-20231010-144258.yaml @@ -0,0 +1,6 @@ +kind: NOTES +body: Initial release of `tfplugingen-openapi` CLI for Terraform Provider Code Generation + tech preview +time: 2023-10-10T14:42:58.532948-04:00 +custom: + Issue: "68" diff --git a/.changie.yaml b/.changie.yaml new file mode 100644 index 0000000..970c770 --- /dev/null +++ b/.changie.yaml @@ -0,0 +1,22 @@ +changesDir: .changes +unreleasedDir: unreleased +changelogPath: CHANGELOG.md +versionExt: md +versionFormat: '## {{.Version}} ({{.Time.Format "January 02, 2006"}})' +kindFormat: '{{.Kind}}:' +changeFormat: '* {{.Body}} ([#{{.Custom.Issue}}](https://github.com/hashicorp/terraform-plugin-codegen-openapi/issues/{{.Custom.Issue}}))' +custom: + - key: Issue + label: Issue/PR Number + type: int + minInt: 1 +kinds: + - label: BREAKING CHANGES + - label: NOTES + - label: FEATURES + - label: ENHANCEMENTS + - label: BUG FIXES +newlines: + afterKind: 1 + beforeKind: 1 + endOfVersion: 2 \ No newline at end of file diff --git a/.github/workflows/ci-goreleaser.yml b/.github/workflows/ci-goreleaser.yml new file mode 100644 index 0000000..02aa5df --- /dev/null +++ b/.github/workflows/ci-goreleaser.yml @@ -0,0 +1,23 @@ +# Continuous integration handling for GoReleaser +name: ci-goreleaser + +on: + pull_request: + paths: + - .github/workflows/ci-goreleaser.yml + - .goreleaser.yml + +permissions: + contents: read + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 + - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 + with: + go-version-file: 'go.mod' + - uses: goreleaser/goreleaser-action@7ec5c2b0c6cdda6e8bbb49444bc797dd33d74dd8 # v5.0.0 + with: + args: check \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..d048f56 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,104 @@ +name: release + +on: + workflow_dispatch: + inputs: + versionNumber: + description: 'Release version number (v#.#.#)' + type: string + required: true + +permissions: + contents: read # Changelog commit operations use service account PAT + +env: + CI_COMMIT_AUTHOR: hc-github-team-tf-provider-devex + CI_COMMIT_EMAIL: github-team-tf-provider-devex@hashicorp.com + +jobs: + changelog-version: + runs-on: ubuntu-latest + outputs: + version: ${{ steps.changelog-version.outputs.version }} + steps: + - id: changelog-version + run: echo "version=$(echo "${{ inputs.versionNumber }}" | cut -c 2-)" >> "$GITHUB_OUTPUT" + + changelog: + needs: changelog-version + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 + with: + fetch-depth: 0 + # Avoid persisting GITHUB_TOKEN credentials as they take priority over our service account PAT for `git push` operations + # More details: https://github.com/actions/checkout/blob/b4626ce19ce1106186ddf9bb20e706842f11a7c3/adrs/0153-checkout-v2.md#persist-credentials + persist-credentials: false + - name: Batch changes + uses: miniscruff/changie-action@6dcc2533cac0495148ed4046c438487e4dceaa23 # v2 + with: + version: latest + args: batch ${{ needs.changelog-version.outputs.version }} + - name: Merge changes + uses: miniscruff/changie-action@6dcc2533cac0495148ed4046c438487e4dceaa23 # v2 + with: + version: latest + args: merge + - name: Git push changelog + run: | + git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" + git config --global user.email "${{ env.CI_COMMIT_EMAIL }}" + git add . + git commit -a -m "Update changelog" + git push "https://${{ env.CI_COMMIT_AUTHOR }}:${{ secrets.TF_DEVEX_COMMIT_GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" + + release-tag: + needs: changelog + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 + with: + fetch-depth: 0 + # Default input is the SHA that initially triggered the workflow. As we created a new commit in the previous job, + # to ensure we get the latest commit we use the ref for checkout: 'refs/heads/' + ref: ${{ github.ref }} + # Avoid persisting GITHUB_TOKEN credentials as they take priority over our service account PAT for `git push` operations + # More details: https://github.com/actions/checkout/blob/b4626ce19ce1106186ddf9bb20e706842f11a7c3/adrs/0153-checkout-v2.md#persist-credentials + persist-credentials: false + + - name: Git push release tag + run: | + git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" + git config --global user.email "${{ env.CI_COMMIT_EMAIL }}" + + git tag "${{ inputs.versionNumber }}" + git push "https://${{ env.CI_COMMIT_AUTHOR }}:${{ secrets.TF_DEVEX_COMMIT_GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" "${{ inputs.versionNumber }}" + + goreleaser: + needs: [ changelog-version, changelog, release-tag ] + runs-on: ubuntu-latest + permissions: + contents: write # Needed for goreleaser to create GitHub release + issues: write # Needed for goreleaser to close associated milestone + steps: + - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 + with: + ref: ${{ inputs.versionNumber }} + fetch-depth: 0 + + - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 + with: + go-version-file: 'go.mod' + + - name: Generate Release Notes + run: | + cd .changes + sed -e "1{/# /d;}" -e "2{/^$/d;}" ${{ needs.changelog-version.outputs.version }}.md > /tmp/release-notes.txt + + - uses: goreleaser/goreleaser-action@7ec5c2b0c6cdda6e8bbb49444bc797dd33d74dd8 # v5.0.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: release --release-notes /tmp/release-notes.txt --clean \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..8912304 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,27 @@ +project_name: tfplugingen-openapi +builds: +- main: ./cmd/tfplugingen-openapi + env: + - CGO_ENABLED=0 + mod_timestamp: '{{ .CommitTimestamp }}' + flags: + - -trimpath + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + goos: + - windows + - linux + - darwin + goarch: + - amd64 + - arm64 + binary: '{{ .ProjectName }}' +archives: +- format: zip + name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' +checksum: + name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS' + algorithm: sha256 +milestones: + - close: true +release: \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index ff5949d..d41d460 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ For more in-depth details about the design and internals of the OpenAPI Provider ### Installation -The CLI tool can be installed with the Go toolchain, via go install: +You install a copy of the binary manually from the [releases](https://github.com/hashicorp/terraform-plugin-codegen-openapi/releases) tab, or install via the Go toolchain: ```shell-session go install github.com/hashicorp/terraform-plugin-codegen-openapi/cmd/tfplugingen-openapi@latest diff --git a/cmd/tfplugingen-openapi/main.go b/cmd/tfplugingen-openapi/main.go index 1534455..21b2d5d 100644 --- a/cmd/tfplugingen-openapi/main.go +++ b/cmd/tfplugingen-openapi/main.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "os" - "runtime/debug" "github.com/mattn/go-colorable" "github.com/mitchellh/cli" @@ -19,23 +18,11 @@ import ( // https://goreleaser.com/cookbooks/using-main.version/ func main() { name := "tfplugingen-openapi" - version := name + func() string { - if info, ok := debug.ReadBuildInfo(); ok { - for _, setting := range info.Settings { - if setting.Key == "vcs.revision" { - return fmt.Sprintf(" commit: %s", setting.Value) - } - } - - return fmt.Sprintf(" module: %s", info.Main.Version) - } - - return " local" - }() + versionOutput := fmt.Sprintf("%s %s", name, getVersion()) os.Exit(runCLI( name, - version, + versionOutput, os.Args[1:], os.Stdin, colorable.NewColorableStdout(), @@ -56,7 +43,7 @@ func initCommands(ui cli.Ui) map[string]cli.CommandFactory { } } -func runCLI(name, version string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { +func runCLI(name, versionOutput string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { ui := &cli.ColoredUi{ ErrorColor: cli.UiColorRed, WarnColor: cli.UiColorYellow, @@ -75,7 +62,7 @@ func runCLI(name, version string, args []string, stdin io.Reader, stdout, stderr Commands: commands, HelpFunc: cli.BasicHelpFunc(name), HelpWriter: stderr, - Version: version, + Version: versionOutput, } exitCode, err := openAPIGen.Run() if err != nil { diff --git a/cmd/tfplugingen-openapi/version.go b/cmd/tfplugingen-openapi/version.go new file mode 100644 index 0000000..821c71c --- /dev/null +++ b/cmd/tfplugingen-openapi/version.go @@ -0,0 +1,39 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package main + +import ( + "fmt" + "runtime/debug" +) + +var ( + // These vars will be set by goreleaser. + version string + commit string +) + +func getVersion() string { + // Prefer global version as it's set by goreleaser via ldflags + // https://goreleaser.com/cookbooks/using-main.version/ + if version != "" { + if commit != "" { + version = fmt.Sprintf("%s from commit: %s", version, commit) + } + return version + } + + // If not built with goreleaser, check the binary for VCS revision/module version info + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return fmt.Sprintf("commit: %s", setting.Value) + } + } + + return fmt.Sprintf("module: %s", info.Main.Version) + } + + return "local" +}