Skip to content

Commit

Permalink
[system] 'GetOSInfo' now works on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Feb 5, 2023
1 parent 985d3b1 commit 7b6ab80
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions system/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
61 changes: 46 additions & 15 deletions system/info_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"os/exec"
"strings"
"syscall"

"github.com/essentialkaos/ek/v12/strutil"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -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"):
Expand All @@ -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
}
23 changes: 23 additions & 0 deletions system/info_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), "")
}
6 changes: 6 additions & 0 deletions system/info_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7b6ab80

Please sign in to comment.