From dadbfbe65a8c684cff658d9b4e25ef44275bc5c7 Mon Sep 17 00:00:00 2001 From: allencloud Date: Fri, 6 May 2016 18:03:01 +0800 Subject: [PATCH] make pkg/parsers support darwin and solaris Signed-off-by: allencloud --- parsers/kernel/kernel.go | 26 --------- parsers/kernel/kernel_darwin.go | 56 +++++++++++++++++++ parsers/kernel/kernel_unix.go | 34 +++++++++++ parsers/kernel/kernel_windows.go | 2 + ...tem_freebsd.go => operatingsystem_unix.go} | 15 +++-- 5 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 parsers/kernel/kernel_darwin.go create mode 100644 parsers/kernel/kernel_unix.go rename parsers/operatingsystem/{operatingsystem_freebsd.go => operatingsystem_unix.go} (55%) diff --git a/parsers/kernel/kernel.go b/parsers/kernel/kernel.go index a21ba13..7738fc7 100644 --- a/parsers/kernel/kernel.go +++ b/parsers/kernel/kernel.go @@ -5,7 +5,6 @@ package kernel import ( - "bytes" "errors" "fmt" ) @@ -46,31 +45,6 @@ func CompareKernelVersion(a, b VersionInfo) int { return 0 } -// GetKernelVersion gets the current kernel version. -func GetKernelVersion() (*VersionInfo, error) { - var ( - err error - ) - - uts, err := uname() - if err != nil { - return nil, err - } - - release := make([]byte, len(uts.Release)) - - i := 0 - for _, c := range uts.Release { - release[i] = byte(c) - i++ - } - - // Remove the \x00 from the release for Atoi to parse correctly - release = release[:bytes.IndexByte(release, 0)] - - return ParseRelease(string(release)) -} - // ParseRelease parses a string and creates a VersionInfo based on it. func ParseRelease(release string) (*VersionInfo, error) { var ( diff --git a/parsers/kernel/kernel_darwin.go b/parsers/kernel/kernel_darwin.go new file mode 100644 index 0000000..71f205b --- /dev/null +++ b/parsers/kernel/kernel_darwin.go @@ -0,0 +1,56 @@ +// +build darwin + +// Package kernel provides helper function to get, parse and compare kernel +// versions for different platforms. +package kernel + +import ( + "fmt" + "os/exec" + "strings" + + "github.com/mattn/go-shellwords" +) + +// GetKernelVersion gets the current kernel version. +func GetKernelVersion() (*VersionInfo, error) { + release, err := getRelease() + if err != nil { + return nil, err + } + + return ParseRelease(release) +} + +// getRelease uses `system_profiler SPSoftwareDataType` to get OSX kernel version +func getRelease() (string, error) { + cmd := exec.Command("system_profiler", "SPSoftwareDataType") + osName, err := cmd.Output() + if err != nil { + return "", err + } + + var release string + data := strings.Split(string(osName), "\n") + for _, line := range data { + if strings.Contains(line, "Kernel Version") { + // It has the format like ' Kernel Version: Darwin 14.5.0' + content := strings.SplitN(line, ":", 2) + if len(content) != 2 { + return "", fmt.Errorf("Kernel Version is invalid") + } + + prettyNames, err := shellwords.Parse(content[1]) + if err != nil { + return "", fmt.Errorf("Kernel Version is invalid: %s", err.Error()) + } + + if len(prettyNames) != 2 { + return "", fmt.Errorf("Kernel Version needs to be 'Darwin x.x.x' ") + } + release = prettyNames[1] + } + } + + return release, nil +} diff --git a/parsers/kernel/kernel_unix.go b/parsers/kernel/kernel_unix.go new file mode 100644 index 0000000..71c16b6 --- /dev/null +++ b/parsers/kernel/kernel_unix.go @@ -0,0 +1,34 @@ +// +build linux freebsd solaris + +// Package kernel provides helper function to get, parse and compare kernel +// versions for different platforms. +package kernel + +import ( + "bytes" +) + +// GetKernelVersion gets the current kernel version. +func GetKernelVersion() (*VersionInfo, error) { + var ( + err error + ) + + uts, err := uname() + if err != nil { + return nil, err + } + + release := make([]byte, len(uts.Release)) + + i := 0 + for _, c := range uts.Release { + release[i] = byte(c) + i++ + } + + // Remove the \x00 from the release for Atoi to parse correctly + release = release[:bytes.IndexByte(release, 0)] + + return ParseRelease(string(release)) +} diff --git a/parsers/kernel/kernel_windows.go b/parsers/kernel/kernel_windows.go index 85ca250..80fab8f 100644 --- a/parsers/kernel/kernel_windows.go +++ b/parsers/kernel/kernel_windows.go @@ -1,3 +1,5 @@ +// +build windows + package kernel import ( diff --git a/parsers/operatingsystem/operatingsystem_freebsd.go b/parsers/operatingsystem/operatingsystem_unix.go similarity index 55% rename from parsers/operatingsystem/operatingsystem_freebsd.go rename to parsers/operatingsystem/operatingsystem_unix.go index 0589cf2..bc91c3c 100644 --- a/parsers/operatingsystem/operatingsystem_freebsd.go +++ b/parsers/operatingsystem/operatingsystem_unix.go @@ -1,18 +1,25 @@ +// +build freebsd darwin + package operatingsystem import ( "errors" + "os/exec" ) // GetOperatingSystem gets the name of the current operating system. func GetOperatingSystem() (string, error) { - // TODO: Implement OS detection - return "", errors.New("Cannot detect OS version") + cmd := exec.Command("uname", "-s") + osName, err := cmd.Output() + if err != nil { + return "", err + } + return string(osName), nil } // IsContainerized returns true if we are running inside a container. -// No-op on FreeBSD, always returns false. +// No-op on FreeBSD and Darwin, always returns false. func IsContainerized() (bool, error) { - // TODO: Implement jail detection + // TODO: Implement jail detection for freeBSD return false, errors.New("Cannot detect if we are in container") }