Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version flag #65

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions constants/build.go
Original file line number Diff line number Diff line change
@@ -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"
)
5 changes: 5 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion templates/.goreleaser.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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={{"{{"}} .Env.VERSION {{"}}"}} -X main.date={{"{{"}} .Date {{"}}"}} -X main.commit={{"{{"}}.Commit {{"}}"}} -X main.builtBy=goreleaser"

# Archive customization
archives:
Expand Down
20 changes: 20 additions & 0 deletions templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]",
Expand All @@ -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
Expand All @@ -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}} v{{"{{"}} .Version {{"}}"}}\n")

viper.BindPFlags(rootCmd.PersistentFlags())

Expand All @@ -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]
Expand Down
9 changes: 7 additions & 2 deletions templates/out/Makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 | sub("^v"; "")')

build:
go build -tags "$(BUILD_TAGS)" -o ${OUTPUT_DIR}/steampipe_export_{{.Plugin}}
$(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}}
9 changes: 9 additions & 0 deletions templates/scripts/release.sh.tmpl
Original file line number Diff line number Diff line change
@@ -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