diff --git a/.goreleaser.yml b/.goreleaser.yml index e6682b0b9..6df35977b 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,4 +1,5 @@ builds: - ldflags: + - -X github.com/elastic/elastic-package/internal/version.Tag={{.Tag}} - -X github.com/elastic/elastic-package/internal/version.CommitHash={{.ShortCommit}} - -X github.com/elastic/elastic-package/internal/version.BuildTime={{.Timestamp}} \ No newline at end of file diff --git a/cmd/version.go b/cmd/version.go index 3658bf63d..1bce57b9e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -5,6 +5,9 @@ package cmd import ( + "fmt" + "strings" + "github.com/spf13/cobra" "github.com/elastic/elastic-package/internal/cobraext" @@ -25,6 +28,13 @@ func setupVersionCommand() *cobraext.Command { } func versionCommandAction(cmd *cobra.Command, args []string) error { - cmd.Printf("elastic-package version-hash %s (build time: %s)\n", version.CommitHash, version.BuildTimeFormatted()) + var sb strings.Builder + sb.WriteString("elastic-package ") + if version.Tag != "" { + sb.WriteString(version.Tag) + sb.WriteString(" ") + } + sb.WriteString(fmt.Sprintf("version-hash %s (build time: %s)", version.CommitHash, version.BuildTimeFormatted())) + fmt.Println(sb.String()) return nil } diff --git a/internal/version/version.go b/internal/version/version.go index 8b53b6312..30d8a5a90 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -15,6 +15,9 @@ var ( // CommitHash is the Git hash of the branch, used for version purposes (set externally with ldflags). CommitHash = "undefined" + + // Tag describes the semver version of the application (set externally with ldflags). + Tag string ) // BuildTimeFormatted method returns the build time preserving the RFC3339 format.