From 48ced9854bb4b02140654a067d0dd7f80845dd8e Mon Sep 17 00:00:00 2001 From: hftsin Date: Wed, 14 Dec 2022 15:37:35 +0800 Subject: [PATCH] chore: refactor macOS version detection (#510) * fix: rename MacOS13OrNewer * util: call version comparison only once * fix: disable duplicated macOS version check --- cmd/start.go | 6 +++--- config/config.go | 2 +- environment/vm/lima/yaml.go | 5 ++--- util/util.go | 6 +++--- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cmd/start.go b/cmd/start.go index 853cc6c79..526a089c7 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -115,7 +115,7 @@ func init() { defaultMountType := "9p" defaultVMType := "qemu" - if util.MacOS13() { + if util.MacOS13OrNewer() { defaultVMType = "vz" defaultMountType = "virtiofs" } @@ -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+")") } @@ -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 } diff --git a/config/config.go b/config/config.go index ab869fc21..7f32604c0 100644 --- a/config/config.go +++ b/config/config.go @@ -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" diff --git a/environment/vm/lima/yaml.go b/environment/vm/lima/yaml.go index 53941ad4e..6c7e22fce 100644 --- a/environment/vm/lima/yaml.go +++ b/environment/vm/lima/yaml.go @@ -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 } diff --git a/util/util.go b/util/util.go index 80d169bb9..0a3f49cd0 100644 --- a/util/util.go +++ b/util/util.go @@ -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 } @@ -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.