chore(version): Get the version following how katenary is installed

This commit is contained in:
2024-11-26 16:47:37 +01:00
parent e13653fba1
commit 441be30720
3 changed files with 24 additions and 3 deletions

View File

@@ -7,7 +7,8 @@ PREFIX=~/.local
GOVERSION=1.23
GO=container
OUT=katenary
BLD_CMD=go build -ldflags="-X 'katenary/generator.Version=$(VERSION)'" -o $(OUT) ./cmd/katenary
RELEASE=""
BLD_CMD=go build -ldflags="-X 'katenary/generator.Version=$(RELEASE)$(VERSION)'" -o $(OUT) ./cmd/katenary
GOOS=linux
GOARCH=amd64
SIGNER=metal3d@gmail.com

View File

@@ -35,7 +35,7 @@ func buildRootCmd() *cobra.Command {
}
rootCmd.Example = ` katenary convert -c docker-compose.yml -o ./charts`
rootCmd.Version = generator.Version
rootCmd.Version = generator.GetVersion()
rootCmd.CompletionOptions.DisableDescriptions = false
rootCmd.CompletionOptions.DisableNoDescFlag = false
@@ -233,7 +233,7 @@ func generateVersionCommand() *cobra.Command {
Use: "version",
Short: "Print the version number of Katenary",
Run: func(cmd *cobra.Command, args []string) {
println(generator.Version)
println(generator.GetVersion())
},
}
}

View File

@@ -1,4 +1,24 @@
package generator
import (
"runtime/debug"
"strings"
)
// Version is the version of katenary. It is set at compile time.
var Version = "master" // changed at compile time
// GetVersion return the version of katneary. It's important to understand that
// the version is set at compile time for the github release. But, it the user get
// katneary using `go install`, the version should be different.
func GetVersion() string {
if strings.HasPrefix(Version, "release-") {
return Version
}
// get the version from the build info
v, ok := debug.ReadBuildInfo()
if ok {
return v.Main.Version + "-" + v.GoVersion
}
return Version
}