4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "github.com/sirupsen/logrus"
7
8
"io"
8
9
"os"
9
10
"os/user"
@@ -16,6 +17,7 @@ import (
16
17
"text/template"
17
18
"time"
18
19
20
+ "github.com/coreos/go-semver/semver"
19
21
"github.com/docker/go-units"
20
22
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
21
23
"github.com/lima-vm/lima/pkg/limayaml"
@@ -35,6 +37,8 @@ const (
35
37
StatusRunning Status = "Running"
36
38
)
37
39
40
+ const DefaultLimaVersion = "v0.19.1"
41
+
38
42
type Instance struct {
39
43
Name string `json:"name"`
40
44
Status Status `json:"status"`
@@ -56,6 +60,7 @@ type Instance struct {
56
60
Config * limayaml.LimaYAML `json:"config,omitempty"`
57
61
SSHAddress string `json:"sshAddress,omitempty"`
58
62
Protected bool `json:"protected"`
63
+ LimaVersion string `json:"limaVersion"`
59
64
}
60
65
61
66
func (inst * Instance ) LoadYAML () (* limayaml.LimaYAML , error ) {
@@ -167,6 +172,14 @@ func Inspect(instName string) (*Instance, error) {
167
172
}
168
173
}
169
174
}
175
+ if version , err := os .ReadFile (filepath .Join (instDir , filenames .LimaVersion )); err == nil {
176
+ inst .LimaVersion = strings .TrimSpace (string (version ))
177
+ } else {
178
+ if ! errors .Is (err , os .ErrNotExist ) {
179
+ inst .Errors = append (inst .Errors , err )
180
+ }
181
+ inst .LimaVersion = DefaultLimaVersion
182
+ }
170
183
return inst , nil
171
184
}
172
185
@@ -423,3 +436,22 @@ func (inst *Instance) Unprotect() error {
423
436
inst .Protected = false
424
437
return nil
425
438
}
439
+
440
+ // MinVersion checks if the instance has been created by the specified minimum version of Lima.
441
+ // Prerelease versions are treated as the previous version, not the next, so "0.19.1-16-gf3dc6ed.m"
442
+ // is considered the same as "0.19.1" and not "0.20.0".
443
+ func MinVersion (instanceVersion , minVersion string ) bool {
444
+ v := strings .TrimPrefix (instanceVersion , "v" )
445
+ v , _ , _ = strings .Cut (v , "-" )
446
+ instVers , err := semver .NewVersion (v )
447
+ if err != nil {
448
+ logrus .Warnf ("cannot parse instance version %q" , instanceVersion )
449
+ return false
450
+ }
451
+ minVers , err := semver .NewVersion (minVersion )
452
+ if err != nil {
453
+ logrus .Warnf ("cannot parse minimum version %q" , minVersion )
454
+ return false
455
+ }
456
+ return ! instVers .LessThan (* minVers )
457
+ }
0 commit comments