pkg/parsers/kernel: use strings.Cut() and minor refactor

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-01 11:30:56 +01:00
parent 774cd9a26c
commit 451b8579ef
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -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) {