Skip to content

Commit 0c6da72

Browse files
authored
Merge pull request #2461 from AkihiroSuda/split-versionutil
pkg/store: split pkg/version/versionutil
2 parents bb9e31b + a59ac75 commit 0c6da72

File tree

4 files changed

+58
-44
lines changed

4 files changed

+58
-44
lines changed

pkg/store/instance.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import (
1616
"text/template"
1717
"time"
1818

19-
"github.com/coreos/go-semver/semver"
2019
"github.com/docker/go-units"
2120
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
2221
"github.com/lima-vm/lima/pkg/limayaml"
2322
"github.com/lima-vm/lima/pkg/store/dirnames"
2423
"github.com/lima-vm/lima/pkg/store/filenames"
2524
"github.com/lima-vm/lima/pkg/textutil"
25+
"github.com/lima-vm/lima/pkg/version/versionutil"
2626
"github.com/sirupsen/logrus"
2727
)
2828

@@ -174,7 +174,7 @@ func Inspect(instName string) (*Instance, error) {
174174
limaVersionFile := filepath.Join(instDir, filenames.LimaVersion)
175175
if version, err := os.ReadFile(limaVersionFile); err == nil {
176176
inst.LimaVersion = strings.TrimSpace(string(version))
177-
if _, err = parseLimaVersion(inst.LimaVersion); err != nil {
177+
if _, err = versionutil.Parse(inst.LimaVersion); err != nil {
178178
logrus.Warnf("treating lima version %q from %q as very latest release", inst.LimaVersion, limaVersionFile)
179179
}
180180
} else if !errors.Is(err, os.ErrNotExist) {
@@ -436,36 +436,3 @@ func (inst *Instance) Unprotect() error {
436436
inst.Protected = false
437437
return nil
438438
}
439-
440-
// parseLimaVersion parses a Lima version string by removing the leading "v" character and
441-
// stripping everything from the first "-" forward (which are `git describe` artifacts and
442-
// not semver pre-release markers). So "v0.19.1-16-gf3dc6ed.m" will be parsed as "0.19.1".
443-
func parseLimaVersion(version string) (*semver.Version, error) {
444-
version = strings.TrimPrefix(version, "v")
445-
version, _, _ = strings.Cut(version, "-")
446-
return semver.NewVersion(version)
447-
}
448-
449-
// LimaVersionGreaterThan returns true if the Lima version used to create an instance is greater
450-
// than a specific older version. Always returns false if the Lima version is the empty string.
451-
// Unparsable lima versions (like SHA1 commit ids) are treated as the latest version and return true.
452-
// limaVersion is a `github describe` string, not a semantic version. So "0.19.1-16-gf3dc6ed.m"
453-
// will be considered greater than "0.19.1".
454-
func LimaVersionGreaterThan(limaVersion, oldVersion string) bool {
455-
if limaVersion == "" {
456-
return false
457-
}
458-
version, err := parseLimaVersion(limaVersion)
459-
if err != nil {
460-
return true
461-
}
462-
switch version.Compare(*semver.New(oldVersion)) {
463-
case -1:
464-
return false
465-
case +1:
466-
return true
467-
case 0:
468-
return strings.Contains(limaVersion, "-")
469-
}
470-
panic("unreachable")
471-
}

pkg/store/instance_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,3 @@ func TestPrintInstanceTableTwo(t *testing.T) {
155155
assert.NilError(t, err)
156156
assert.Equal(t, tableTwo, buf.String())
157157
}
158-
159-
func TestLimaVersionGreaterThan(t *testing.T) {
160-
assert.Equal(t, LimaVersionGreaterThan("", "0.1.0"), false)
161-
assert.Equal(t, LimaVersionGreaterThan("0.0.1", "0.1.0"), false)
162-
assert.Equal(t, LimaVersionGreaterThan("0.1.0", "0.1.0"), false)
163-
assert.Equal(t, LimaVersionGreaterThan("0.1.0-2", "0.1.0"), true)
164-
assert.Equal(t, LimaVersionGreaterThan("0.2.0", "0.1.0"), true)
165-
assert.Equal(t, LimaVersionGreaterThan("abacab", "0.1.0"), true)
166-
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package versionutil
2+
3+
import (
4+
"strings"
5+
6+
"github.com/coreos/go-semver/semver"
7+
)
8+
9+
// Parse parses a Lima version string by removing the leading "v" character and
10+
// stripping everything from the first "-" forward (which are `git describe` artifacts and
11+
// not semver pre-release markers). So "v0.19.1-16-gf3dc6ed.m" will be parsed as "0.19.1".
12+
func Parse(version string) (*semver.Version, error) {
13+
version = strings.TrimPrefix(version, "v")
14+
version, _, _ = strings.Cut(version, "-")
15+
return semver.NewVersion(version)
16+
}
17+
18+
// GreaterThan returns true if the Lima version used to create an instance is greater
19+
// than a specific older version. Always returns false if the Lima version is the empty string.
20+
// Unparsable lima versions (like SHA1 commit ids) are treated as the latest version and return true.
21+
// limaVersion is a `github describe` string, not a semantic version. So "0.19.1-16-gf3dc6ed.m"
22+
// will be considered greater than "0.19.1".
23+
func GreaterThan(limaVersion, oldVersion string) bool {
24+
if limaVersion == "" {
25+
return false
26+
}
27+
version, err := Parse(limaVersion)
28+
if err != nil {
29+
return true
30+
}
31+
switch version.Compare(*semver.New(oldVersion)) {
32+
case -1:
33+
return false
34+
case +1:
35+
return true
36+
case 0:
37+
return strings.Contains(limaVersion, "-")
38+
}
39+
panic("unreachable")
40+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package versionutil
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestGreaterThan(t *testing.T) {
10+
assert.Equal(t, GreaterThan("", "0.1.0"), false)
11+
assert.Equal(t, GreaterThan("0.0.1", "0.1.0"), false)
12+
assert.Equal(t, GreaterThan("0.1.0", "0.1.0"), false)
13+
assert.Equal(t, GreaterThan("0.1.0-2", "0.1.0"), true)
14+
assert.Equal(t, GreaterThan("0.2.0", "0.1.0"), true)
15+
assert.Equal(t, GreaterThan("abacab", "0.1.0"), true)
16+
}

0 commit comments

Comments
 (0)