Skip to content

Commit

Permalink
Make version more flexible to allow kbld to be used as a library
Browse files Browse the repository at this point in the history
Signed-off-by: Joao Pereira <[email protected]>
  • Loading branch information
joaopapereira committed Mar 8, 2024
1 parent 771e4b2 commit df3b7ad
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/kbld/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit df3b7ad

Please sign in to comment.