Skip to content

Commit

Permalink
Move macos version compare to pkg/os/darwin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
praveenkumar authored and anjannath committed Feb 26, 2024
1 parent 517fd85 commit 764f90d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
32 changes: 2 additions & 30 deletions pkg/drivers/vfkit/driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ import (
"os/exec"
"strconv"
"strings"
"sync"
"syscall"
"time"

"github.com/Masterminds/semver/v3"
"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/logging"
crcos "github.com/crc-org/crc/v2/pkg/os"
"github.com/crc-org/crc/v2/pkg/os/darwin"
"github.com/crc-org/machine/libmachine/drivers"
"github.com/crc-org/machine/libmachine/state"
"github.com/crc-org/vfkit/pkg/config"
Expand Down Expand Up @@ -488,36 +486,10 @@ func (d *Driver) sendSignal(s syscall.Signal) error {
return proc.SendSignal(s)
}

var (
once sync.Once
macCurrentVersion string
errGettingVersion error
)

func (d *Driver) supportsVirtiofs() bool {
supportsVirtioFS, err := macosAtLeast("12.0.0")
supportsVirtioFS, err := darwin.AtLeast("12.0.0")
if err != nil {
log.Debugf("Not able to compare version: %v", err)
}
return supportsVirtioFS
}

func macosAtLeast(targetVersion string) (bool, error) {
once.Do(func() {
macCurrentVersion, errGettingVersion = syscall.Sysctl("kern.osproductversion")
logging.Debugf("kern.osproductversion is: %s", macCurrentVersion)
})
if errGettingVersion != nil {
return false, errGettingVersion
}

cVersion, err := semver.NewVersion(macCurrentVersion)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", macCurrentVersion))
}
targetVersionStr, err := semver.NewVersion(targetVersion)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", targetVersion))
}
return cVersion.Equal(targetVersionStr) || cVersion.GreaterThan(targetVersionStr), nil
}
39 changes: 39 additions & 0 deletions pkg/os/darwin/release_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build darwin

package darwin

import (
"fmt"
"sync"
"syscall"

"github.com/Masterminds/semver/v3"
"github.com/crc-org/crc/v2/pkg/crc/logging"
"github.com/pkg/errors"
)

var (
once sync.Once
macCurrentVersion string
errGettingVersion error
)

func AtLeast(targetVersion string) (bool, error) {
once.Do(func() {
macCurrentVersion, errGettingVersion = syscall.Sysctl("kern.osproductversion")
logging.Debugf("kern.osproductversion is: %s", macCurrentVersion)
})
if errGettingVersion != nil {
return false, errGettingVersion
}

cVersion, err := semver.NewVersion(macCurrentVersion)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", macCurrentVersion))
}
targetVersionStr, err := semver.NewVersion(targetVersion)
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("cannot parse %s", targetVersion))
}
return cVersion.Equal(targetVersionStr) || cVersion.GreaterThan(targetVersionStr), nil
}

0 comments on commit 764f90d

Please sign in to comment.