Skip to content

Commit 277da9e

Browse files
committed
fix version subcommand is not working after release build
1 parent 2b00d88 commit 277da9e

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

api/provenance/provenance.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,22 @@ func GetProvenance() Provenance {
6767

6868
for _, dep := range info.Deps {
6969
if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
70-
p.Version = GetMostRecentTag(*dep)
70+
if dep.Version != "devel" {
71+
continue
72+
}
73+
v, err := GetMostRecentTag(*dep)
74+
if err != nil {
75+
fmt.Printf("failed to get most recent tag for %s: %v\n", dep.Path, err)
76+
continue
77+
}
78+
p.Version = v
7179
}
7280
}
7381

7482
return p
7583
}
7684

77-
func GetMostRecentTag(m debug.Module) string {
85+
func GetMostRecentTag(m debug.Module) (string, error) {
7886
for m.Replace != nil {
7987
m = *m.Replace
8088
}
@@ -83,13 +91,13 @@ func GetMostRecentTag(m debug.Module) string {
8391
sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))
8492

8593
if err != nil {
86-
return "unknown"
94+
return "", fmt.Errorf("failed to parse version %s: %w", m.Version, err)
8795
}
8896

8997
if len(split) > 1 && sv.Patch > 0 {
9098
sv.Patch -= 1
9199
}
92-
return fmt.Sprintf("v%s", sv.FinalizeVersion())
100+
return fmt.Sprintf("v%s", sv.FinalizeVersion()), nil
93101
}
94102

95103
// Short returns the shortened provenance stamp.

api/provenance/provenance_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func TestGetMostRecentTag(t *testing.T) {
5959
tests := []struct {
6060
name string
6161
module debug.Module
62+
isError bool
6263
expectedTag string
6364
}{
6465
{
@@ -72,9 +73,9 @@ func TestGetMostRecentTag(t *testing.T) {
7273
expectedTag: "v0.0.0",
7374
},
7475
{
75-
name: "Invalid semver string",
76-
module: mockModule("invalid-version"),
77-
expectedTag: "unknown",
76+
name: "Invalid semver string",
77+
module: mockModule("invalid-version"),
78+
isError: true,
7879
},
7980
{
8081
name: "Valid semver with patch increment and pre-release info",
@@ -90,8 +91,14 @@ func TestGetMostRecentTag(t *testing.T) {
9091

9192
for _, tt := range tests {
9293
t.Run(tt.name, func(t *testing.T) {
93-
tag := provenance.GetMostRecentTag(tt.module)
94-
assert.Equal(t, tt.expectedTag, tag)
94+
tag, err := provenance.GetMostRecentTag(tt.module)
95+
if err != nil {
96+
if !tt.isError {
97+
assert.NoError(t, err)
98+
}
99+
} else {
100+
assert.Equal(t, tt.expectedTag, tag)
101+
}
95102
})
96103
}
97104
}

0 commit comments

Comments
 (0)