@@ -23,6 +23,8 @@ import (
23
23
"regexp"
24
24
"strconv"
25
25
"strings"
26
+
27
+ apimachineryversion "k8s.io/apimachinery/pkg/version"
26
28
)
27
29
28
30
// Version is an opaque representation of a version number
@@ -31,6 +33,7 @@ type Version struct {
31
33
semver bool
32
34
preRelease string
33
35
buildMetadata string
36
+ info apimachineryversion.Info
34
37
}
35
38
36
39
var (
@@ -252,19 +255,30 @@ func (v *Version) WithMinor(minor uint) *Version {
252
255
return & result
253
256
}
254
257
255
- // SubtractMinor returns the version diff minor versions back , with the same major and no patch.
256
- // If diff >= current minor, the minor would be 0.
257
- func (v * Version ) SubtractMinor ( diff uint ) * Version {
258
+ // SubtractMinor returns the version with offset from the original minor , with the same major and no patch.
259
+ // If -offset >= current minor, the minor would be 0.
260
+ func (v * Version ) OffsetMinor ( offset int ) * Version {
258
261
var minor uint
259
- if diff < v .Minor () {
260
- minor = v .Minor () - diff
262
+ if offset >= 0 {
263
+ minor = v .Minor () + uint (offset )
264
+ } else {
265
+ diff := uint (- offset )
266
+ if diff < v .Minor () {
267
+ minor = v .Minor () - diff
268
+ }
261
269
}
262
270
return MajorMinor (v .Major (), minor )
263
271
}
264
272
273
+ // SubtractMinor returns the version diff minor versions back, with the same major and no patch.
274
+ // If diff >= current minor, the minor would be 0.
275
+ func (v * Version ) SubtractMinor (diff uint ) * Version {
276
+ return v .OffsetMinor (- int (diff ))
277
+ }
278
+
265
279
// AddMinor returns the version diff minor versions forward, with the same major and no patch.
266
280
func (v * Version ) AddMinor (diff uint ) * Version {
267
- return MajorMinor ( v . Major (), v . Minor () + diff )
281
+ return v . OffsetMinor ( int ( diff ) )
268
282
}
269
283
270
284
// WithPatch returns copy of the version object with requested patch number
@@ -441,3 +455,30 @@ func (v *Version) Compare(other string) (int, error) {
441
455
}
442
456
return v .compareInternal (ov ), nil
443
457
}
458
+
459
+ // WithInfo returns copy of the version object with requested info
460
+ func (v * Version ) WithInfo (info apimachineryversion.Info ) * Version {
461
+ result := * v
462
+ result .info = info
463
+ return & result
464
+ }
465
+
466
+ func (v * Version ) Info () * apimachineryversion.Info {
467
+ if v == nil {
468
+ return nil
469
+ }
470
+ // in case info is empty, or the major and minor in info is different from the actual major and minor
471
+ v .info .Major = itoa (v .Major ())
472
+ v .info .Minor = itoa (v .Minor ())
473
+ if v .info .GitVersion == "" {
474
+ v .info .GitVersion = v .String ()
475
+ }
476
+ return & v .info
477
+ }
478
+
479
+ func itoa (i uint ) string {
480
+ if i == 0 {
481
+ return ""
482
+ }
483
+ return strconv .Itoa (int (i ))
484
+ }
0 commit comments