Skip to content

Commit

Permalink
Make version more flexible to allow kapp to be used as a library (#897)
Browse files Browse the repository at this point in the history
Signed-off-by: João Pereira <[email protected]>
  • Loading branch information
joaopapereira authored Mar 11, 2024
1 parent ff35d20 commit 939c724
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion pkg/kapp/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 939c724

Please sign in to comment.