From 2c2c4d55d0e3b602f852c38c15ce5addd192abe7 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Thu, 26 Oct 2023 20:17:17 +0900 Subject: [PATCH] pkg/osutil: move MacOSProductVersion() from pkg/qemu Signed-off-by: Akihiro Suda --- pkg/osutil/osversion.go | 29 +++++++++++++++++++++++++++++ pkg/qemu/qemu.go | 22 ++-------------------- 2 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 pkg/osutil/osversion.go diff --git a/pkg/osutil/osversion.go b/pkg/osutil/osversion.go new file mode 100644 index 000000000000..bd3eebace189 --- /dev/null +++ b/pkg/osutil/osversion.go @@ -0,0 +1,29 @@ +package osutil + +import ( + "fmt" + "os/exec" + "strings" + + "github.com/coreos/go-semver/semver" +) + +// MacOSProductVersion returns the macOS product version like "12.3.1". +func MacOSProductVersion() (*semver.Version, error) { + cmd := exec.Command("sw_vers", "-productVersion") + // output is like "12.3.1\n" + b, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("failed to execute %v: %w", cmd.Args, err) + } + verTrimmed := strings.TrimSpace(string(b)) + // macOS 12.4 returns just "12.4\n" + for strings.Count(verTrimmed, ".") < 2 { + verTrimmed += ".0" + } + verSem, err := semver.NewVersion(verTrimmed) + if err != nil { + return nil, fmt.Errorf("failed to parse macOS version %q: %w", verTrimmed, err) + } + return verSem, nil +} diff --git a/pkg/qemu/qemu.go b/pkg/qemu/qemu.go index a346bb47ac51..accbbeeb2579 100644 --- a/pkg/qemu/qemu.go +++ b/pkg/qemu/qemu.go @@ -17,6 +17,7 @@ import ( "time" "github.com/lima-vm/lima/pkg/networks/usernet" + "github.com/lima-vm/lima/pkg/osutil" "github.com/coreos/go-semver/semver" "github.com/digitalocean/go-qemu/qmp" @@ -398,25 +399,6 @@ func showDarwinARM64HVFQEMU620Warning(exe, accel string, features *features) { logrus.Warn(w) } -func getMacOSProductVersion() (*semver.Version, error) { - cmd := exec.Command("sw_vers", "-productVersion") - // output is like "12.3.1\n" - b, err := cmd.Output() - if err != nil { - return nil, fmt.Errorf("failed to execute %v: %w", cmd.Args, err) - } - verTrimmed := strings.TrimSpace(string(b)) - // macOS 12.4 returns just "12.4\n" - for strings.Count(verTrimmed, ".") < 2 { - verTrimmed += ".0" - } - verSem, err := semver.NewVersion(verTrimmed) - if err != nil { - return nil, fmt.Errorf("failed to parse macOS version %q: %w", verTrimmed, err) - } - return verSem, nil -} - // adjustMemBytesDarwinARM64HVF adjusts the memory to be <= 3 GiB, only when the following conditions are met: // // - Host OS < macOS 12.4 @@ -443,7 +425,7 @@ func adjustMemBytesDarwinARM64HVF(memBytes int64, accel string, features *featur if !features.VersionGEQ7 { return memBytes } - macOSProductVersion, err := getMacOSProductVersion() + macOSProductVersion, err := osutil.MacOSProductVersion() if err != nil { logrus.Warn(err) return memBytes