github.com/lacework-dev/go-moby@v20.10.12+incompatible/pkg/parsers/kernel/kernel_darwin.go (about) 1 // +build darwin 2 3 // Package kernel provides helper function to get, parse and compare kernel 4 // versions for different platforms. 5 package kernel // import "github.com/docker/docker/pkg/parsers/kernel" 6 7 import ( 8 "fmt" 9 "os/exec" 10 "strings" 11 ) 12 13 // GetKernelVersion gets the current kernel version. 14 func GetKernelVersion() (*VersionInfo, error) { 15 osName, err := getSPSoftwareDataType() 16 if err != nil { 17 return nil, err 18 } 19 release, err := getRelease(osName) 20 if err != nil { 21 return nil, err 22 } 23 return ParseRelease(release) 24 } 25 26 // getRelease uses `system_profiler SPSoftwareDataType` to get OSX kernel version 27 func getRelease(osName string) (string, error) { 28 var release string 29 data := strings.Split(osName, "\n") 30 for _, line := range data { 31 if !strings.Contains(line, "Kernel Version") { 32 continue 33 } 34 // It has the format like ' Kernel Version: Darwin 14.5.0' 35 content := strings.SplitN(line, ":", 2) 36 if len(content) != 2 { 37 return "", fmt.Errorf("Kernel Version is invalid") 38 } 39 40 prettyNames := strings.SplitN(strings.TrimSpace(content[1]), " ", 2) 41 42 if len(prettyNames) != 2 { 43 return "", fmt.Errorf("Kernel Version needs to be 'Darwin x.x.x' ") 44 } 45 release = prettyNames[1] 46 } 47 48 return release, nil 49 } 50 51 func getSPSoftwareDataType() (string, error) { 52 cmd := exec.Command("system_profiler", "SPSoftwareDataType") 53 osName, err := cmd.Output() 54 if err != nil { 55 return "", err 56 } 57 return string(osName), nil 58 }