Skip to content

Commit

Permalink
chore: refactor macOS version detection (#510)
Browse files Browse the repository at this point in the history
* fix: rename MacOS13OrNewer

* util: call version comparison only once

* fix: disable duplicated macOS version check
  • Loading branch information
hftsin authored Dec 14, 2022
1 parent 77a42c6 commit 48ced98
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func init() {

defaultMountType := "9p"
defaultVMType := "qemu"
if util.MacOS13() {
if util.MacOS13OrNewer() {
defaultVMType = "vz"
defaultMountType = "virtiofs"
}
Expand All @@ -136,7 +136,7 @@ func init() {
if util.MacOS() {
startCmd.Flags().BoolVar(&startCmdArgs.Network.Address, "network-address", false, "assign reachable IP address to the VM")
}
if util.MacOS13() {
if util.MacOS13OrNewer() {
startCmd.Flags().StringVarP(&startCmdArgs.VMType, "vm-type", "t", defaultVMType, "virtual machine type ("+types+")")
}

Expand Down Expand Up @@ -297,7 +297,7 @@ func prepareConfig(cmd *cobra.Command) {
if !cmd.Flag("network-address").Changed {
startCmdArgs.Network.Address = current.Network.Address
}
if util.MacOS13() {
if util.MacOS13OrNewer() {
if !cmd.Flag("vm-type").Changed {
startCmdArgs.VMType = current.VMType
}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func CtxKey() any {
}

func (c Config) DriverLabel() string {
if util.MacOS13() && c.VMType == "vz" {
if util.MacOS13OrNewer() && c.VMType == "vz" {
return "macOS Virtualization.Framework"
}
return "QEMU"
Expand Down
5 changes: 2 additions & 3 deletions environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ func newConf(ctx context.Context, conf config.Config) (l Config, err error) {
l.VMType = QEMU

sameArchitecture := environment.HostArch() == l.Arch
isM1 := util.MacOS13() && environment.HostArch() == environment.AARCH64

// when vz is chosen and OS version supports it
if util.MacOS13() && conf.VMType == VZ && sameArchitecture {
if util.MacOS13OrNewer() && conf.VMType == VZ && sameArchitecture {
l.VMType = VZ

// Rosetta is only available on M1
if isM1 {
if l.Arch == environment.AARCH64 {
l.Rosetta.Enabled = true
l.Rosetta.BinFmt = true
}
Expand Down
6 changes: 3 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func MacOS() bool {
return runtime.GOOS == "darwin"
}

// MacOS13 returns if the current OS is macOS 13 or newer.
func MacOS13() bool {
// MacOS13OrNewer returns if the current OS is macOS 13 or newer.
func MacOS13OrNewer() bool {
if !MacOS() {
return false
}
Expand All @@ -46,7 +46,7 @@ func MacOS13() bool {
return false
}

return cver.Equal(*ver) || cver.LessThan(*ver)
return cver.Compare(*ver) <= 0
}

// AppendToPath appends directory to PATH.
Expand Down

0 comments on commit 48ced98

Please sign in to comment.