From a7f551359ad187a37663b39afdfe33122addea30 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 --- pkg/parsers/kernel/kernel.go | 26 --------- pkg/parsers/kernel/kernel_darwin.go | 56 +++++++++++++++++++ pkg/parsers/kernel/kernel_unix.go | 34 +++++++++++ pkg/parsers/kernel/kernel_windows.go | 2 + ...tem_freebsd.go => operatingsystem_unix.go} | 15 +++-- 5 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 pkg/parsers/kernel/kernel_darwin.go create mode 100644 pkg/parsers/kernel/kernel_unix.go rename pkg/parsers/operatingsystem/{operatingsystem_freebsd.go => operatingsystem_unix.go} (55%) diff --git a/pkg/parsers/kernel/kernel.go b/pkg/parsers/kernel/kernel.go index a21ba137e3..7738fc7411 100644 --- a/pkg/parsers/kernel/kernel.go +++ b/pkg/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/pkg/parsers/kernel/kernel_darwin.go b/pkg/parsers/kernel/kernel_darwin.go new file mode 100644 index 0000000000..71f205b285 --- /dev/null +++ b/pkg/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/pkg/parsers/kernel/kernel_unix.go b/pkg/parsers/kernel/kernel_unix.go new file mode 100644 index 0000000000..71c16b666a --- /dev/null +++ b/pkg/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/pkg/parsers/kernel/kernel_windows.go b/pkg/parsers/kernel/kernel_windows.go index 85ca250c9f..80fab8ff64 100644 --- a/pkg/parsers/kernel/kernel_windows.go +++ b/pkg/parsers/kernel/kernel_windows.go @@ -1,3 +1,5 @@ +// +build windows + package kernel import ( diff --git a/pkg/parsers/operatingsystem/operatingsystem_freebsd.go b/pkg/parsers/operatingsystem/operatingsystem_unix.go similarity index 55% rename from pkg/parsers/operatingsystem/operatingsystem_freebsd.go rename to pkg/parsers/operatingsystem/operatingsystem_unix.go index 0589cf2ae3..bc91c3c533 100644 --- a/pkg/parsers/operatingsystem/operatingsystem_freebsd.go +++ b/pkg/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") }