Skip to content

Commit 764f90d

Browse files
praveenkumaranjannath
authored andcommitted
Move macos version compare to pkg/os/darwin
This can be now used by other package also like in next commit where we are going to use it for preflight check to provide a warning.
1 parent 517fd85 commit 764f90d

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

Diff for: pkg/drivers/vfkit/driver_darwin.go

+2-30
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ import (
2323
"os/exec"
2424
"strconv"
2525
"strings"
26-
"sync"
2726
"syscall"
2827
"time"
2928

30-
"github.com/Masterminds/semver/v3"
3129
"github.com/crc-org/crc/v2/pkg/crc/constants"
32-
"github.com/crc-org/crc/v2/pkg/crc/logging"
3330
crcos "github.com/crc-org/crc/v2/pkg/os"
31+
"github.com/crc-org/crc/v2/pkg/os/darwin"
3432
"github.com/crc-org/machine/libmachine/drivers"
3533
"github.com/crc-org/machine/libmachine/state"
3634
"github.com/crc-org/vfkit/pkg/config"
@@ -488,36 +486,10 @@ func (d *Driver) sendSignal(s syscall.Signal) error {
488486
return proc.SendSignal(s)
489487
}
490488

491-
var (
492-
once sync.Once
493-
macCurrentVersion string
494-
errGettingVersion error
495-
)
496-
497489
func (d *Driver) supportsVirtiofs() bool {
498-
supportsVirtioFS, err := macosAtLeast("12.0.0")
490+
supportsVirtioFS, err := darwin.AtLeast("12.0.0")
499491
if err != nil {
500492
log.Debugf("Not able to compare version: %v", err)
501493
}
502494
return supportsVirtioFS
503495
}
504-
505-
func macosAtLeast(targetVersion string) (bool, error) {
506-
once.Do(func() {
507-
macCurrentVersion, errGettingVersion = syscall.Sysctl("kern.osproductversion")
508-
logging.Debugf("kern.osproductversion is: %s", macCurrentVersion)
509-
})
510-
if errGettingVersion != nil {
511-
return false, errGettingVersion
512-
}
513-
514-
cVersion, err := semver.NewVersion(macCurrentVersion)
515-
if err != nil {
516-
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", macCurrentVersion))
517-
}
518-
targetVersionStr, err := semver.NewVersion(targetVersion)
519-
if err != nil {
520-
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", targetVersion))
521-
}
522-
return cVersion.Equal(targetVersionStr) || cVersion.GreaterThan(targetVersionStr), nil
523-
}

Diff for: pkg/os/darwin/release_info.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build darwin
2+
3+
package darwin
4+
5+
import (
6+
"fmt"
7+
"sync"
8+
"syscall"
9+
10+
"github.com/Masterminds/semver/v3"
11+
"github.com/crc-org/crc/v2/pkg/crc/logging"
12+
"github.com/pkg/errors"
13+
)
14+
15+
var (
16+
once sync.Once
17+
macCurrentVersion string
18+
errGettingVersion error
19+
)
20+
21+
func AtLeast(targetVersion string) (bool, error) {
22+
once.Do(func() {
23+
macCurrentVersion, errGettingVersion = syscall.Sysctl("kern.osproductversion")
24+
logging.Debugf("kern.osproductversion is: %s", macCurrentVersion)
25+
})
26+
if errGettingVersion != nil {
27+
return false, errGettingVersion
28+
}
29+
30+
cVersion, err := semver.NewVersion(macCurrentVersion)
31+
if err != nil {
32+
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", macCurrentVersion))
33+
}
34+
targetVersionStr, err := semver.NewVersion(targetVersion)
35+
if err != nil {
36+
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", targetVersion))
37+
}
38+
return cVersion.Equal(targetVersionStr) || cVersion.GreaterThan(targetVersionStr), nil
39+
}

0 commit comments

Comments
 (0)