pkg/parsers/kernel: use strings.Cut() and minor refactor
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
774cd9a26c
commit
451b8579ef
1 changed files with 9 additions and 12 deletions
|
@ -26,27 +26,24 @@ func GetKernelVersion() (*VersionInfo, error) {
|
|||
|
||||
// getRelease uses `system_profiler SPSoftwareDataType` to get OSX kernel version
|
||||
func getRelease(osName string) (string, error) {
|
||||
var release string
|
||||
data := strings.Split(osName, "\n")
|
||||
for _, line := range data {
|
||||
for _, line := range strings.Split(osName, "\n") {
|
||||
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")
|
||||
_, ver, ok := strings.Cut(line, ":")
|
||||
if !ok {
|
||||
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, ok := strings.Cut(strings.TrimSpace(ver), " ")
|
||||
if !ok {
|
||||
return "", fmt.Errorf("kernel version needs to be 'Darwin x.x.x'")
|
||||
}
|
||||
release = prettyNames[1]
|
||||
return release, nil
|
||||
}
|
||||
|
||||
return release, nil
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func getSPSoftwareDataType() (string, error) {
|
||||
|
|
Loading…
Reference in a new issue