|
@@ -8,49 +8,51 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"os/exec"
|
|
"os/exec"
|
|
"strings"
|
|
"strings"
|
|
-
|
|
|
|
- shellwords "github.com/mattn/go-shellwords"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
// GetKernelVersion gets the current kernel version.
|
|
// GetKernelVersion gets the current kernel version.
|
|
func GetKernelVersion() (*VersionInfo, error) {
|
|
func GetKernelVersion() (*VersionInfo, error) {
|
|
- release, err := getRelease()
|
|
|
|
|
|
+ osName, err := getSPSoftwareDataType()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ release, err := getRelease(osName)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
-
|
|
|
|
return ParseRelease(release)
|
|
return ParseRelease(release)
|
|
}
|
|
}
|
|
|
|
|
|
// getRelease uses `system_profiler SPSoftwareDataType` to get OSX kernel version
|
|
// 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
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+func getRelease(osName string) (string, error) {
|
|
var release string
|
|
var release string
|
|
- data := strings.Split(string(osName), "\n")
|
|
|
|
|
|
+ data := strings.Split(osName, "\n")
|
|
for _, line := range data {
|
|
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]
|
|
|
|
|
|
+ if !strings.Contains(line, "Kernel Version") {
|
|
|
|
+ continue
|
|
}
|
|
}
|
|
|
|
+ // 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 := strings.SplitN(strings.TrimSpace(content[1]), " ", 2)
|
|
|
|
+
|
|
|
|
+ if len(prettyNames) != 2 {
|
|
|
|
+ return "", fmt.Errorf("Kernel Version needs to be 'Darwin x.x.x' ")
|
|
|
|
+ }
|
|
|
|
+ release = prettyNames[1]
|
|
}
|
|
}
|
|
|
|
|
|
return release, nil
|
|
return release, nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func getSPSoftwareDataType() (string, error) {
|
|
|
|
+ cmd := exec.Command("system_profiler", "SPSoftwareDataType")
|
|
|
|
+ osName, err := cmd.Output()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return "", err
|
|
|
|
+ }
|
|
|
|
+ return string(osName), nil
|
|
|
|
+}
|