diff --git a/pkg/kbld/version/version.go b/pkg/kbld/version/version.go index 3ca685ad..15fdea76 100644 --- a/pkg/kbld/version/version.go +++ b/pkg/kbld/version/version.go @@ -3,4 +3,39 @@ package version -var Version = "develop" +import ( + "runtime/debug" +) + +var ( + // Version can be set via: + // -ldflags="-X 'carvel.dev/kbld/pkg/kbld/version.Version=$TAG'" + defaultVersion = "develop" + Version = defaultVersion + moduleName = "carvel.dev/kbld" +) + +func init() { + Version = version() +} + +func version() string { + if Version != defaultVersion { + // 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 +}