@@ -19,12 +19,21 @@ var (
1919 // During a release, this will be set to the release tag, e.g. "kustomize/v4.5.7"
2020 version = developmentVersion
2121 // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
22- buildDate = " unknown"
22+ buildDate = unknown
2323)
2424
25- // This default value, (devel), matches
26- // the value debug.BuildInfo uses for an unset main module version.
27- const developmentVersion = "(devel)"
25+ const (
26+ // This default value, (devel), matches
27+ // the value debug.BuildInfo uses for an unset main module version.
28+ developmentVersion = "(devel)"
29+
30+ // ModulePath is kustomize module path, defined in kustomize/go.mod
31+ ModulePath = "sigs.k8s.io/kustomize/kustomize/v5"
32+
33+ // This is default value, unknown, substituted when
34+ // the value can't be determined from debug.BuildInfo.
35+ unknown = "unknown"
36+ )
2837
2938// Provenance holds information about the build of an executable.
3039type Provenance struct {
@@ -47,7 +56,7 @@ func GetProvenance() Provenance {
4756 p := Provenance {
4857 BuildDate : buildDate ,
4958 Version : version ,
50- GitCommit : " unknown" ,
59+ GitCommit : unknown ,
5160 GoOs : runtime .GOOS ,
5261 GoArch : runtime .GOARCH ,
5362 GoVersion : runtime .Version (),
@@ -62,24 +71,37 @@ func GetProvenance() Provenance {
6271 // We could consider adding other info such as the commit date in the future.
6372 if setting .Key == "vcs.revision" {
6473 p .GitCommit = setting .Value
74+ break
6575 }
6676 }
77+ p .Version = FindVersion (info , p .Version )
78+
79+ return p
80+ }
6781
82+ // FindVersion searches for a version in the depth of dependencies including replacements,
83+ // otherwise, it tries to get version from debug.BuildInfo Main.
84+ func FindVersion (info * debug.BuildInfo , version string ) string {
6885 for _ , dep := range info .Deps {
69- if dep != nil && dep .Path == "sigs.k8s.io/kustomize/kustomize/v5" {
70- if dep .Version != "devel" {
86+ if dep != nil && dep .Path == ModulePath {
87+ if dep .Version == developmentVersion {
7188 continue
7289 }
7390 v , err := GetMostRecentTag (* dep )
7491 if err != nil {
7592 fmt .Printf ("failed to get most recent tag for %s: %v\n " , dep .Path , err )
7693 continue
7794 }
78- p .Version = v
95+
96+ return v
7997 }
8098 }
8199
82- return p
100+ if version == developmentVersion && info .Main .Version != "" {
101+ return info .Main .Version
102+ }
103+
104+ return version
83105}
84106
85107func GetMostRecentTag (m debug.Module ) (string , error ) {
0 commit comments