From 7b6ab803e40aadf0a7eabea3f23ee51c329e2312 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sun, 5 Feb 2023 13:49:30 +0300 Subject: [PATCH] [system] 'GetOSInfo' now works on macOS --- CHANGELOG.md | 1 + system/info.go | 1 + system/info_darwin.go | 61 ++++++++++++++++++++++++++++---------- system/info_darwin_test.go | 23 ++++++++++++++ system/info_linux_test.go | 6 ++++ 5 files changed, 77 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8a3f8df..b2d63354 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * `[system]` Added system arch name to `SystemInfo` * `[system]` `Version` and `Distribution` info removed from `SystemInfo` (_use `OSInfo` instead_) +* `[system]` `GetOSInfo` now works on macOS ### 12.57.1 diff --git a/system/info.go b/system/info.go index 9f74132e..8cf82b7f 100644 --- a/system/info.go +++ b/system/info.go @@ -135,6 +135,7 @@ type OSInfo struct { Name string `json:"name"` PrettyName string `json:"pretty_name"` Version string `json:"version"` + Build string `json:"build"` VersionID string `json:"version_id"` VersionCodename string `json:"version_codename"` ID string `json:"id"` diff --git a/system/info_darwin.go b/system/info_darwin.go index 23fecbea..ae9d14e6 100644 --- a/system/info_darwin.go +++ b/system/info_darwin.go @@ -13,6 +13,8 @@ import ( "os/exec" "strings" "syscall" + + "github.com/essentialkaos/ek/v12/strutil" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -43,19 +45,50 @@ func GetSystemInfo() (*SystemInfo, error) { return nil, errors.New("Can't read arch info") } + arch = getMacOSArch(arch) + return &SystemInfo{ - Hostname: hostname, - OS: os, - Distribution: DARWIN_OSX, - Version: getMacOSVersion(), - Kernel: kernel, - Arch: getMacOSArch(arch), - ArchBits: 64, + Hostname: hostname, + OS: os, + Kernel: kernel, + Arch: arch, + ArchName: getArchName(arch), + ArchBits: 64, }, nil } +// GetOSInfo returns info about OS +func GetOSInfo() (*OSInfo, error) { + cmd := exec.Command("sw_vers") + versionData, err := cmd.Output() + + if err != nil { + return nil, err + } + + info := &OSInfo{} + + for _, line := range strings.Split(string(versionData), "\n") { + name := strutil.ReadField(line, 0, false, ":") + value := strutil.ReadField(line, 1, false, ":") + value = strings.Trim(value, " \r\t\n") + + switch name { + case "ProductName": + info.Name = value + case "ProductVersion": + info.Version = value + case "BuildVersion": + info.Build = value + } + } + + return info, nil +} + // ////////////////////////////////////////////////////////////////////////////////// // +// getMacOSArch returns info about arch func getMacOSArch(archInfo string) string { switch { case strings.Contains(archInfo, "X86_64"): @@ -67,14 +100,12 @@ func getMacOSArch(archInfo string) string { return "unknown" } -func getMacOSVersion() string { - cmd := exec.Command("sw_vers", "-productVersion") - - versionData, err := cmd.Output() - - if err != nil { - return "" +// getArchName returns name for given arch +func getArchName(arch string) string { + switch arch { + case "x86_64": + return "amd64" } - return strings.Trim(string(versionData), "\r\n") + return arch } diff --git a/system/info_darwin_test.go b/system/info_darwin_test.go index 69f1663e..0d994bf5 100644 --- a/system/info_darwin_test.go +++ b/system/info_darwin_test.go @@ -45,3 +45,26 @@ func (s *SystemSuite) TestUser(c *C) { c.Assert(IsGroupExist("wheel"), Equals, true) c.Assert(IsGroupExist("_unknown_"), Equals, false) } + +func (s *SystemSuite) TestSystemInfo(c *C) { + sysInfo, err := GetSystemInfo() + + c.Assert(err, IsNil) + c.Assert(sysInfo, NotNil) + + c.Assert(sysInfo.Hostname, Not(Equals), "") + c.Assert(sysInfo.OS, Not(Equals), "") + c.Assert(sysInfo.Kernel, Not(Equals), "") + c.Assert(sysInfo.Arch, Not(Equals), "") + c.Assert(sysInfo.ArchName, Not(Equals), "") + c.Assert(sysInfo.ArchBits, Not(Equals), 0) + + osInfo, err := GetOSInfo() + + c.Assert(err, IsNil) + c.Assert(osInfo, NotNil) + + c.Assert(osInfo.Name, Equals, "macOS") + c.Assert(osInfo.Version, Not(Equals), "") + c.Assert(osInfo.Build, Not(Equals), "") +} diff --git a/system/info_linux_test.go b/system/info_linux_test.go index 929f6f56..19315b62 100644 --- a/system/info_linux_test.go +++ b/system/info_linux_test.go @@ -615,6 +615,12 @@ func (s *SystemSuite) TestGetInfo(c *C) { sysInfo, err := GetSystemInfo() c.Assert(err, IsNil) c.Assert(sysInfo, NotNil) + c.Assert(sysInfo.Hostname, Not(Equals), "") + c.Assert(sysInfo.OS, Not(Equals), "") + c.Assert(sysInfo.Kernel, Not(Equals), "") + c.Assert(sysInfo.Arch, Not(Equals), "") + c.Assert(sysInfo.ArchName, Not(Equals), "") + c.Assert(sysInfo.ArchBits, Not(Equals), 0) osReleaseFile = "/_UNKNOWN_" osInfo, err := GetOSInfo()