From c169d26d9582992c203b212255603afcad50919e Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 18 Aug 2023 10:24:45 +1000 Subject: [PATCH] [v3] Port operating system detection --- v3/internal/operatingsystem/os.go | 7 ++-- v3/internal/operatingsystem/os_darwin.go | 39 ++++++++++++++++------- v3/internal/operatingsystem/os_windows.go | 1 + 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/v3/internal/operatingsystem/os.go b/v3/internal/operatingsystem/os.go index 39f1de8e0..028a97b2e 100644 --- a/v3/internal/operatingsystem/os.go +++ b/v3/internal/operatingsystem/os.go @@ -2,9 +2,10 @@ package operatingsystem // OS contains information about the operating system type OS struct { - ID string - Name string - Version string + ID string + Name string + Version string + Branding string } // Info retrieves information about the current platform diff --git a/v3/internal/operatingsystem/os_darwin.go b/v3/internal/operatingsystem/os_darwin.go index 213ccfb03..c61affa90 100644 --- a/v3/internal/operatingsystem/os_darwin.go +++ b/v3/internal/operatingsystem/os_darwin.go @@ -7,6 +7,32 @@ import ( "strings" ) +var macOSNames = map[string]string{ + "10.10": "Yosemite", + "10.11": "El Capitan", + "10.12": "Sierra", + "10.13": "High Sierra", + "10.14": "Mojave", + "10.15": "Catalina", + "11": "Big Sur", + "12": "Monterey", + "13": "Ventura", + "14": "Sonoma", + // Add newer versions as they are released... +} + +func getOSName(version string) string { + trimmedVersion := version + if !strings.HasPrefix(version, "10.") { + trimmedVersion = strings.SplitN(version, ".", 2)[0] + } + name, ok := macOSNames[version] + if ok { + return name + } + return "MacOS " + trimmedVersion +} + func getSysctlValue(key string) (string, error) { // Run "sysctl" command command := exec.Command("sysctl", key) @@ -39,18 +65,7 @@ func platformInfo() (*OS, error) { return nil, err } result.ID = ID - - // cmd := CreateCommand(directory, command, args...) - // var stdo, stde bytes.Buffer - // cmd.Stdout = &stdo - // cmd.Stderr = &stde - // err := cmd.Run() - // return stdo.String(), stde.String(), err - // } - // sysctl := shell.NewCommand("sysctl") - // kern.ostype: Darwin - // kern.osrelease: 20.1.0 - // kern.osrevision: 199506 + result.Branding = getOSName(result.Version) return &result, nil } diff --git a/v3/internal/operatingsystem/os_windows.go b/v3/internal/operatingsystem/os_windows.go index 38ea43a12..0926fcdc1 100644 --- a/v3/internal/operatingsystem/os_windows.go +++ b/v3/internal/operatingsystem/os_windows.go @@ -27,6 +27,7 @@ func platformInfo() (*OS, error) { result.Name = productName result.Version = fmt.Sprintf("%s (Build: %s)", releaseId, currentBuild) result.ID = displayVersion + result.Branding = w32.GetBranding() return &result, key.Close() }