From 802fc7387bbe994b71420b7f86266a1327a94eef Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Wed, 17 Apr 2024 20:48:57 +0530 Subject: [PATCH 1/6] template changes to support versioning --- constants/build.go | 17 +++++++++++++++++ templates/.goreleaser.yml.tmpl | 3 ++- templates/main.go.tmpl | 20 ++++++++++++++++++++ templates/out/Makefile.tmpl | 9 +++++++-- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 constants/build.go diff --git a/constants/build.go b/constants/build.go new file mode 100644 index 0000000..82ecc08 --- /dev/null +++ b/constants/build.go @@ -0,0 +1,17 @@ +package constants + +const ( + ConfigKeyVersion = "main.version" + ConfigKeyCommit = "main.commit" + ConfigKeyDate = "main.date" + ConfigKeyBuiltBy = "main.builtBy" + + LocalBuild = DefaultBuiltBy +) + +const ( + DefaultVersion = "0.0.0" + DefaultCommit = "none" + DefaultDate = "unknown" + DefaultBuiltBy = "local" +) diff --git a/templates/.goreleaser.yml.tmpl b/templates/.goreleaser.yml.tmpl index 90b0782..95dba6c 100644 --- a/templates/.goreleaser.yml.tmpl +++ b/templates/.goreleaser.yml.tmpl @@ -21,7 +21,8 @@ builds: - amd64 - arm64 ldflags: - - "-s -w" # strip debug info for smaller binaries + # strip debug info for smaller binaries + - "-s -w -X main.version={{"{{"}} .Version {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser" # Archive customization archives: diff --git a/templates/main.go.tmpl b/templates/main.go.tmpl index 99cae58..af7dd65 100644 --- a/templates/main.go.tmpl +++ b/templates/main.go.tmpl @@ -26,6 +26,15 @@ import ( "github.com/spf13/viper" filter2 "github.com/turbot/steampipe-plugin-sdk/v5/filter" "github.com/turbot/steampipe-plugin-sdk/v5/sperr" + "github.com/turbot/steampipe-export/constants" +) + +var ( + // These variables will be set by GoReleaser. + version = constants.DefaultVersion + commit = constants.DefaultCommit + date = constants.DefaultDate + builtBy = constants.DefaultBuiltBy ) var pluginServer *grpc.PluginServer @@ -39,6 +48,8 @@ var isFirstJSONRow = true var isJSONStarted = false func main() { + // add the auto-populated version properties into viper + setVersionProperties() setupLogger(pluginAlias) rootCmd := &cobra.Command{ Use: "steampipe_export_{{.Plugin}} TABLE_NAME [flags]", @@ -50,6 +61,7 @@ examples at the Steampipe Hub: https://hub.steampipe.io/plugins/turbot/{{.Plugin `, Run: executeCommand, Args: cobra.ExactArgs(1), + Version: viper.GetString("main.version"), } // Define flags @@ -58,6 +70,7 @@ examples at the Steampipe Hub: https://hub.steampipe.io/plugins/turbot/{{.Plugin rootCmd.PersistentFlags().String("output", "csv", "Output format: csv, json or jsonl") rootCmd.PersistentFlags().StringSlice("select", nil, "Column data to display") rootCmd.PersistentFlags().Int("limit", 0, "Limit data") + rootCmd.SetVersionTemplate("steampipe_export_{{.Plugin}} {{"{{"}} .Version {{"}}"}}\n") viper.BindPFlags(rootCmd.PersistentFlags()) @@ -72,6 +85,13 @@ examples at the Steampipe Hub: https://hub.steampipe.io/plugins/turbot/{{.Plugin } +func setVersionProperties() { + viper.SetDefault(constants.ConfigKeyVersion, version) + viper.SetDefault(constants.ConfigKeyCommit, commit) + viper.SetDefault(constants.ConfigKeyDate, date) + viper.SetDefault(constants.ConfigKeyBuiltBy, builtBy) +} + func executeCommand(cmd *cobra.Command, args []string) { // TODO template table := args[0] diff --git a/templates/out/Makefile.tmpl b/templates/out/Makefile.tmpl index aae219f..4838656 100644 --- a/templates/out/Makefile.tmpl +++ b/templates/out/Makefile.tmpl @@ -5,8 +5,13 @@ OS := $(shell uname) # Check if the OS is Mac OS/Darwin ifeq ($(OS),Darwin) - BUILD_TAGS = netgo +BUILD_TAGS = netgo endif +# Function to fetch the version of the plugin +fetch_module_version = $(shell go list -m -json github.com/turbot/steampipe-plugin-{{.Plugin}} | jq --raw-output '.Version') + build: - go build -tags "$(BUILD_TAGS)" -o ${OUTPUT_DIR}/steampipe_export_{{.Plugin}} \ No newline at end of file + $(eval VERSION=$(call fetch_module_version)) + @echo Version is $(VERSION) + go build -tags "$(BUILD_TAGS)" -ldflags "-X main.version=$(VERSION)" -o ${OUTPUT_DIR}/steampipe_export_{{.Plugin}} \ No newline at end of file From 65054f04d35c14026c246834e8a04add0caffe5d Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Wed, 17 Apr 2024 20:53:26 +0530 Subject: [PATCH 2/6] prefixed with v --- templates/main.go.tmpl | 2 +- templates/out/Makefile.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/main.go.tmpl b/templates/main.go.tmpl index af7dd65..3a92de9 100644 --- a/templates/main.go.tmpl +++ b/templates/main.go.tmpl @@ -70,7 +70,7 @@ examples at the Steampipe Hub: https://hub.steampipe.io/plugins/turbot/{{.Plugin rootCmd.PersistentFlags().String("output", "csv", "Output format: csv, json or jsonl") rootCmd.PersistentFlags().StringSlice("select", nil, "Column data to display") rootCmd.PersistentFlags().Int("limit", 0, "Limit data") - rootCmd.SetVersionTemplate("steampipe_export_{{.Plugin}} {{"{{"}} .Version {{"}}"}}\n") + rootCmd.SetVersionTemplate("steampipe_export_{{.Plugin}} v{{"{{"}} .Version {{"}}"}}\n") viper.BindPFlags(rootCmd.PersistentFlags()) diff --git a/templates/out/Makefile.tmpl b/templates/out/Makefile.tmpl index 4838656..f8aa8b9 100644 --- a/templates/out/Makefile.tmpl +++ b/templates/out/Makefile.tmpl @@ -9,7 +9,7 @@ BUILD_TAGS = netgo endif # Function to fetch the version of the plugin -fetch_module_version = $(shell go list -m -json github.com/turbot/steampipe-plugin-{{.Plugin}} | jq --raw-output '.Version') +fetch_module_version = $(shell go list -m -json github.com/turbot/steampipe-plugin-{{.Plugin}} | jq --raw-output '.Version | sub("^v"; "")') build: $(eval VERSION=$(call fetch_module_version)) From f7f803a1a602b495daf1951629e29cc4aaee6594 Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Thu, 18 Apr 2024 16:14:34 +0530 Subject: [PATCH 3/6] use script to run goreleaser to get correct versioning --- scripts/version.sh | 5 +++++ templates/.goreleaser.yml.tmpl | 2 +- templates/scripts/version.sh.tmpl | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100755 scripts/version.sh create mode 100644 templates/scripts/version.sh.tmpl diff --git a/scripts/version.sh b/scripts/version.sh new file mode 100755 index 0000000..3dd8095 --- /dev/null +++ b/scripts/version.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +export VERSION=$(go list -m -json github.com/turbot/steampipe-plugin-chaosdynamic | jq --raw-output '.Version | sub("^v"; "")') +echo "VERSION set to $VERSION" +goreleaser release --snapshot --rm-dist --skip=publish --skip=validate \ No newline at end of file diff --git a/templates/.goreleaser.yml.tmpl b/templates/.goreleaser.yml.tmpl index 95dba6c..12a5a78 100644 --- a/templates/.goreleaser.yml.tmpl +++ b/templates/.goreleaser.yml.tmpl @@ -22,7 +22,7 @@ builds: - arm64 ldflags: # strip debug info for smaller binaries - - "-s -w -X main.version={{"{{"}} .Version {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser" + - "-s -w -X main.version={{"{{"}} .Env.VERSION {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser" # Archive customization archives: diff --git a/templates/scripts/version.sh.tmpl b/templates/scripts/version.sh.tmpl new file mode 100644 index 0000000..439de93 --- /dev/null +++ b/templates/scripts/version.sh.tmpl @@ -0,0 +1,9 @@ +#!/bin/bash + +# This script fetches the specific version of the steampipe plugin module, sets it as an environment variable, +# and then uses it in a GoReleaser snapshot release to ensure version consistency. This is needed since Goreleaser is +# run from the export repository and the version tag is pushed to the plugin repository. + +export VERSION=$(go list -m -json github.com/turbot/steampipe-plugin-{{.Plugin}} | jq --raw-output '.Version | sub("^v"; "")') +echo "VERSION set to $VERSION" +goreleaser release --snapshot --rm-dist --skip=publish \ No newline at end of file From c470db1e0e74be96aa4c2461e27df007989bbdb9 Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Thu, 18 Apr 2024 16:20:08 +0530 Subject: [PATCH 4/6] rename script --- scripts/{version.sh => release.sh} | 0 templates/scripts/{version.sh.tmpl => release.sh.tmpl} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename scripts/{version.sh => release.sh} (100%) rename templates/scripts/{version.sh.tmpl => release.sh.tmpl} (100%) diff --git a/scripts/version.sh b/scripts/release.sh similarity index 100% rename from scripts/version.sh rename to scripts/release.sh diff --git a/templates/scripts/version.sh.tmpl b/templates/scripts/release.sh.tmpl similarity index 100% rename from templates/scripts/version.sh.tmpl rename to templates/scripts/release.sh.tmpl From 4583d5318ddeec970e4ab45c22eb85c134ca93cb Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Thu, 18 Apr 2024 17:04:10 +0530 Subject: [PATCH 5/6] try env --- templates/.goreleaser.yml.tmpl | 2 +- templates/scripts/release.sh.tmpl | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/templates/.goreleaser.yml.tmpl b/templates/.goreleaser.yml.tmpl index 12a5a78..68ef673 100644 --- a/templates/.goreleaser.yml.tmpl +++ b/templates/.goreleaser.yml.tmpl @@ -22,7 +22,7 @@ builds: - arm64 ldflags: # strip debug info for smaller binaries - - "-s -w -X main.version={{"{{"}} .Env.VERSION {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser" + - "-s -w -X main.version={{"{{"}} .Env.GORELEASER_CURRENT_TAG {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser" # Archive customization archives: diff --git a/templates/scripts/release.sh.tmpl b/templates/scripts/release.sh.tmpl index 439de93..c617677 100644 --- a/templates/scripts/release.sh.tmpl +++ b/templates/scripts/release.sh.tmpl @@ -5,5 +5,4 @@ # run from the export repository and the version tag is pushed to the plugin repository. export VERSION=$(go list -m -json github.com/turbot/steampipe-plugin-{{.Plugin}} | jq --raw-output '.Version | sub("^v"; "")') -echo "VERSION set to $VERSION" -goreleaser release --snapshot --rm-dist --skip=publish \ No newline at end of file +echo "VERSION set to $VERSION" \ No newline at end of file From d7afa3badff360382ad27a281d3fa3f01e62c6a8 Mon Sep 17 00:00:00 2001 From: Puskar Basu Date: Thu, 18 Apr 2024 17:07:11 +0530 Subject: [PATCH 6/6] Revert "try env" This reverts commit 4583d5318ddeec970e4ab45c22eb85c134ca93cb. --- templates/.goreleaser.yml.tmpl | 2 +- templates/scripts/release.sh.tmpl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/.goreleaser.yml.tmpl b/templates/.goreleaser.yml.tmpl index 68ef673..12a5a78 100644 --- a/templates/.goreleaser.yml.tmpl +++ b/templates/.goreleaser.yml.tmpl @@ -22,7 +22,7 @@ builds: - arm64 ldflags: # strip debug info for smaller binaries - - "-s -w -X main.version={{"{{"}} .Env.GORELEASER_CURRENT_TAG {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser" + - "-s -w -X main.version={{"{{"}} .Env.VERSION {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser" # Archive customization archives: diff --git a/templates/scripts/release.sh.tmpl b/templates/scripts/release.sh.tmpl index c617677..439de93 100644 --- a/templates/scripts/release.sh.tmpl +++ b/templates/scripts/release.sh.tmpl @@ -5,4 +5,5 @@ # run from the export repository and the version tag is pushed to the plugin repository. export VERSION=$(go list -m -json github.com/turbot/steampipe-plugin-{{.Plugin}} | jq --raw-output '.Version | sub("^v"; "")') -echo "VERSION set to $VERSION" \ No newline at end of file +echo "VERSION set to $VERSION" +goreleaser release --snapshot --rm-dist --skip=publish \ No newline at end of file