From 8bde3d7522b253cf5b7bfa7a9fcbf0e391ad2248 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 18 Aug 2023 10:24:45 +1000 Subject: [PATCH] [v3 mac] Improved operating system detection --- v3/internal/operatingsystem/os.go | 16 ++++++++-- v3/internal/operatingsystem/os_darwin.go | 39 ++++++++++++++++------- v3/internal/operatingsystem/os_windows.go | 1 + v3/pkg/application/application_darwin.go | 2 +- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/v3/internal/operatingsystem/os.go b/v3/internal/operatingsystem/os.go index 39f1de8e..2d565628 100644 --- a/v3/internal/operatingsystem/os.go +++ b/v3/internal/operatingsystem/os.go @@ -2,9 +2,19 @@ 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 +} + +func (o *OS) AsLogSlice() []any { + return []any{ + "ID", o.ID, + "Name", o.Name, + "Version", o.Version, + "Branding", o.Branding, + } } // Info retrieves information about the current platform diff --git a/v3/internal/operatingsystem/os_darwin.go b/v3/internal/operatingsystem/os_darwin.go index 213ccfb0..137cf71a 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[trimmedVersion] + if ok { + return name + } + return "MacOS " + version +} + 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 38ea43a1..0926fcdc 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() } diff --git a/v3/pkg/application/application_darwin.go b/v3/pkg/application/application_darwin.go index 0276a8ba..2091f008 100644 --- a/v3/pkg/application/application_darwin.go +++ b/v3/pkg/application/application_darwin.go @@ -277,6 +277,6 @@ func (a *App) logPlatformInfo() { return } - a.info("Platform Info:", "name", info.Name, "version", info.Version, "id", info.ID) + a.info("Platform Info:", info.AsLogSlice()...) }