@@ -16,6 +16,7 @@ import (
16
16
"text/template"
17
17
"time"
18
18
19
+ "github.com/coreos/go-semver/semver"
19
20
"github.com/docker/go-units"
20
21
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
21
22
"github.com/lima-vm/lima/pkg/limayaml"
@@ -56,6 +57,7 @@ type Instance struct {
56
57
Config * limayaml.LimaYAML `json:"config,omitempty"`
57
58
SSHAddress string `json:"sshAddress,omitempty"`
58
59
Protected bool `json:"protected"`
60
+ LimaVersion string `json:"limaVersion"`
59
61
}
60
62
61
63
func (inst * Instance ) LoadYAML () (* limayaml.LimaYAML , error ) {
@@ -167,6 +169,20 @@ func Inspect(instName string) (*Instance, error) {
167
169
}
168
170
}
169
171
}
172
+
173
+ limaVersionFile := filepath .Join (instDir , filenames .LimaVersion )
174
+ if version , err := os .ReadFile (limaVersionFile ); err == nil {
175
+ limaVersion := strings .TrimSpace (string (version ))
176
+ // Don't store invalid version strings because later code will not validate again
177
+ if _ , err := parseLimaVersion (limaVersion ); err == nil {
178
+ inst .LimaVersion = limaVersion
179
+ } else {
180
+ err = fmt .Errorf ("cannot parse lima version %q from %q: %w" , limaVersion , limaVersionFile , err )
181
+ inst .Errors = append (inst .Errors , err )
182
+ }
183
+ } else if ! errors .Is (err , os .ErrNotExist ) {
184
+ inst .Errors = append (inst .Errors , err )
185
+ }
170
186
return inst , nil
171
187
}
172
188
@@ -423,3 +439,32 @@ func (inst *Instance) Unprotect() error {
423
439
inst .Protected = false
424
440
return nil
425
441
}
442
+
443
+ // parseLimaVersion parses a Lima version string by removing the leading "v" character and
444
+ // stripping everything from the first "-" forward (which are `git describe` artifacts and
445
+ // not semver pre-release markers). So "v0.19.1-16-gf3dc6ed.m" will be parsed as "0.19.1".
446
+ func parseLimaVersion (version string ) (* semver.Version , error ) {
447
+ version = strings .TrimPrefix (version , "v" )
448
+ version , _ , _ = strings .Cut (version , "-" )
449
+ return semver .NewVersion (version )
450
+ }
451
+
452
+ // LimaVersionGreaterThan returns true if the Lima version used to create an instance is greater
453
+ // than a specific older version. Always returns false if the Lima version is the empty string.
454
+ // This function compares `github describe` versions, not semantic versions. So "0.19.1-16-gf3dc6ed.m"
455
+ // will be considered greater than "0.19.1".
456
+ func LimaVersionGreaterThan (limaVersion , oldVersion string ) bool {
457
+ if limaVersion == "" {
458
+ return false
459
+ }
460
+ version , _ := parseLimaVersion (limaVersion )
461
+ switch version .Compare (* semver .New (oldVersion )) {
462
+ case - 1 :
463
+ return false
464
+ case + 1 :
465
+ return true
466
+ case 0 :
467
+ return strings .Contains (limaVersion , "-" )
468
+ }
469
+ panic ("unreachable" )
470
+ }
0 commit comments