From 939c7249c2a0e2c27680718b871cc645837f26c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pereira?= Date: Mon, 11 Mar 2024 09:38:54 -0500 Subject: [PATCH] Make version more flexible to allow kapp to be used as a library (#897) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Pereira --- pkg/kapp/version/version.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/kapp/version/version.go b/pkg/kapp/version/version.go index 3ca685ad0..db5df8491 100644 --- a/pkg/kapp/version/version.go +++ b/pkg/kapp/version/version.go @@ -3,4 +3,37 @@ package version -var Version = "develop" +import "runtime/debug" + +var ( + // Version can be set via: + // -ldflags="-X 'github.com/vmware-tanzu/carvel-kapp/pkg/kapp/version.Version=$TAG'" + defaultVersion = "develop" + Version = "" + moduleName = "github.com/vmware-tanzu/carvel-kapp" +) + +func init() { + Version = version() +} + +func version() string { + if Version != "" { + // Version was set via ldflags, just return it. + return Version + } + + info, ok := debug.ReadBuildInfo() + if !ok { + return defaultVersion + } + + // Anything else. + for _, dep := range info.Deps { + if dep.Path == moduleName { + return dep.Version + } + } + + return defaultVersion +}