From c5525431846f848be86d264085f5effcd932a416 Mon Sep 17 00:00:00 2001 From: Sebastian Webber Date: Sat, 2 Apr 2022 15:48:38 -0300 Subject: [PATCH] fix: tag version in the binaries Signed-off-by: Sebastian Webber --- .goreleaser.yml | 10 +++++++++- cmd/api/handlers/v1/config.go | 3 ++- cmd/api/main.go | 2 +- cmd/pgconfigctl/cmd/tune.go | 5 +++-- cmd/pgconfigctl/cmd/version.go | 9 ++------- pkg/version/version.go | 9 ++++++++- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 828222f..d8d8ece 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -18,6 +18,11 @@ builds: goarch: - amd64 - arm64 + ldflags: + - -s + - -w + - -X github.com/pgconfig/api/pkg/version.Tag={{.Version}} + - -X github.com/pgconfig/api/pkg/version.Commit={{.Commit}} - id: api binary: api @@ -35,7 +40,10 @@ builds: - amd64 - arm64 ldflags: - - -s -w -X version.Tag={{.Version}} -X version.Commit={{.Commit}} + - -s + - -w + - -X github.com/pgconfig/api/pkg/version.Tag={{.Version}} + - -X github.com/pgconfig/api/pkg/version.Commit={{.Commit}} nfpms: - id: default package_name: pgconfigctl diff --git a/cmd/api/handlers/v1/config.go b/cmd/api/handlers/v1/config.go index 8638158..fdd2e1f 100644 --- a/cmd/api/handlers/v1/config.go +++ b/cmd/api/handlers/v1/config.go @@ -15,6 +15,7 @@ import ( "github.com/pgconfig/api/pkg/docs" "github.com/pgconfig/api/pkg/format" "github.com/pgconfig/api/pkg/rules" + "github.com/pgconfig/api/pkg/version" ) // GetConfig is a function to that computes the input and suggests a tuning configuration @@ -181,7 +182,7 @@ func formatConf(c *fiber.Ctx, f string, output []category.SliceOutput, pgVersion case "stackgres", "sg", "sgpostgresconfig": b.WriteString("---\n") } - b.WriteString(fmt.Sprintf("%s Generated by PGConfig 3.0 alpha\n", fillComment(1, comment))) + b.WriteString(fmt.Sprintf("%s Generated by PGConfig %s\n", fillComment(1, comment), version.Pretty())) b.WriteString(fmt.Sprintf("%s %s%s\n", fillComment(1, comment), c.BaseURL(), c.OriginalURL())) b.WriteString("\n") diff --git a/cmd/api/main.go b/cmd/api/main.go index 7448146..f7c3827 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -28,7 +28,7 @@ func init() { } func main() { - log.Printf("%s (%s)\n", version.Tag, version.Commit) + log.Printf("PGConfig API - %s\n", version.Pretty()) app := routes.New() if err := app.Listen(fmt.Sprintf(":%d", port)); err != nil { diff --git a/cmd/pgconfigctl/cmd/tune.go b/cmd/pgconfigctl/cmd/tune.go index 5d3ab33..9c58cfa 100644 --- a/cmd/pgconfigctl/cmd/tune.go +++ b/cmd/pgconfigctl/cmd/tune.go @@ -33,6 +33,7 @@ import ( "github.com/pgconfig/api/pkg/defaults" "github.com/pgconfig/api/pkg/format" "github.com/pgconfig/api/pkg/rules" + "github.com/pgconfig/api/pkg/version" "github.com/spf13/cobra" "github.com/mackerelio/go-osstat/memory" @@ -125,12 +126,12 @@ func init() { func export(f string, report []category.SliceOutput) { if f == "config" { - fmt.Printf("## Generated by pgconfigctl-%s\n\n", version) + fmt.Printf("## Generated by pgconfigctl-%s\n\n", version.Pretty()) fmt.Println(format.ConfigFile(report)) return } - fmt.Printf("-- Generated by pgconfigctl-%s\n\n", version) + fmt.Printf("-- Generated by pgconfigctl-%s\n\n", version.Pretty()) fmt.Println(format.AlterSystem(report)) } diff --git a/cmd/pgconfigctl/cmd/version.go b/cmd/pgconfigctl/cmd/version.go index 46ac21a..8e0057d 100644 --- a/cmd/pgconfigctl/cmd/version.go +++ b/cmd/pgconfigctl/cmd/version.go @@ -26,13 +26,8 @@ import ( "fmt" "github.com/spf13/cobra" -) -var ( - version = "development" - commit = "latest" - date = "TBD" - builtBy = "hand" + "github.com/pgconfig/api/pkg/version" ) // versionCmd represents the version command @@ -40,7 +35,7 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "Displays version information", Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("%s (%s) on %s - built by %s\n", version, commit, date, builtBy) + fmt.Printf("pgconfigctl - %s\n", version.Pretty()) }, } diff --git a/pkg/version/version.go b/pkg/version/version.go index 4bc4d06..e2f92ae 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,9 +1,16 @@ package version -const ( +import "fmt" + +var ( // Tag is the current $(git tag) that this code is built Tag = "development" // Commit is the current commit that this code is built Commit = "latest" ) + +// Pretty formats the version with commit and tag +func Pretty() string { + return fmt.Sprintf("%s (%s)", Tag, Commit) +}