Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/store: split pkg/version/versionutil #2461

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 2 additions & 35 deletions pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
"text/template"
"time"

"github.com/coreos/go-semver/semver"
"github.com/docker/go-units"
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/lima-vm/lima/pkg/textutil"
"github.com/lima-vm/lima/pkg/version/versionutil"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -174,7 +174,7 @@ func Inspect(instName string) (*Instance, error) {
limaVersionFile := filepath.Join(instDir, filenames.LimaVersion)
if version, err := os.ReadFile(limaVersionFile); err == nil {
inst.LimaVersion = strings.TrimSpace(string(version))
if _, err = parseLimaVersion(inst.LimaVersion); err != nil {
if _, err = versionutil.Parse(inst.LimaVersion); err != nil {
logrus.Warnf("treating lima version %q from %q as very latest release", inst.LimaVersion, limaVersionFile)
}
} else if !errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -436,36 +436,3 @@ func (inst *Instance) Unprotect() error {
inst.Protected = false
return nil
}

// parseLimaVersion parses a Lima version string by removing the leading "v" character and
// stripping everything from the first "-" forward (which are `git describe` artifacts and
// not semver pre-release markers). So "v0.19.1-16-gf3dc6ed.m" will be parsed as "0.19.1".
func parseLimaVersion(version string) (*semver.Version, error) {
version = strings.TrimPrefix(version, "v")
version, _, _ = strings.Cut(version, "-")
return semver.NewVersion(version)
}

// LimaVersionGreaterThan returns true if the Lima version used to create an instance is greater
// than a specific older version. Always returns false if the Lima version is the empty string.
// Unparsable lima versions (like SHA1 commit ids) are treated as the latest version and return true.
// limaVersion is a `github describe` string, not a semantic version. So "0.19.1-16-gf3dc6ed.m"
// will be considered greater than "0.19.1".
func LimaVersionGreaterThan(limaVersion, oldVersion string) bool {
if limaVersion == "" {
return false
}
version, err := parseLimaVersion(limaVersion)
if err != nil {
return true
}
switch version.Compare(*semver.New(oldVersion)) {
case -1:
return false
case +1:
return true
case 0:
return strings.Contains(limaVersion, "-")
}
panic("unreachable")
}
9 changes: 0 additions & 9 deletions pkg/store/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,3 @@ func TestPrintInstanceTableTwo(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, tableTwo, buf.String())
}

func TestLimaVersionGreaterThan(t *testing.T) {
assert.Equal(t, LimaVersionGreaterThan("", "0.1.0"), false)
assert.Equal(t, LimaVersionGreaterThan("0.0.1", "0.1.0"), false)
assert.Equal(t, LimaVersionGreaterThan("0.1.0", "0.1.0"), false)
assert.Equal(t, LimaVersionGreaterThan("0.1.0-2", "0.1.0"), true)
assert.Equal(t, LimaVersionGreaterThan("0.2.0", "0.1.0"), true)
assert.Equal(t, LimaVersionGreaterThan("abacab", "0.1.0"), true)
}
40 changes: 40 additions & 0 deletions pkg/version/versionutil/versionutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package versionutil

import (
"strings"

"github.com/coreos/go-semver/semver"
)

// Parse parses a Lima version string by removing the leading "v" character and
// stripping everything from the first "-" forward (which are `git describe` artifacts and
// not semver pre-release markers). So "v0.19.1-16-gf3dc6ed.m" will be parsed as "0.19.1".
func Parse(version string) (*semver.Version, error) {
version = strings.TrimPrefix(version, "v")
version, _, _ = strings.Cut(version, "-")
return semver.NewVersion(version)
}

// GreaterThan returns true if the Lima version used to create an instance is greater
// than a specific older version. Always returns false if the Lima version is the empty string.
// Unparsable lima versions (like SHA1 commit ids) are treated as the latest version and return true.
// limaVersion is a `github describe` string, not a semantic version. So "0.19.1-16-gf3dc6ed.m"
// will be considered greater than "0.19.1".
func GreaterThan(limaVersion, oldVersion string) bool {
if limaVersion == "" {
return false
}
version, err := Parse(limaVersion)
if err != nil {
return true
}
switch version.Compare(*semver.New(oldVersion)) {
case -1:
return false
case +1:
return true
case 0:
return strings.Contains(limaVersion, "-")
}
panic("unreachable")
}
16 changes: 16 additions & 0 deletions pkg/version/versionutil/versionutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package versionutil

import (
"testing"

"gotest.tools/v3/assert"
)

func TestGreaterThan(t *testing.T) {
assert.Equal(t, GreaterThan("", "0.1.0"), false)
assert.Equal(t, GreaterThan("0.0.1", "0.1.0"), false)
assert.Equal(t, GreaterThan("0.1.0", "0.1.0"), false)
assert.Equal(t, GreaterThan("0.1.0-2", "0.1.0"), true)
assert.Equal(t, GreaterThan("0.2.0", "0.1.0"), true)
assert.Equal(t, GreaterThan("abacab", "0.1.0"), true)
}
Loading