Skip to content

Commit

Permalink
pkg/parsers/kernel: simplify
Browse files Browse the repository at this point in the history
Since golang.org/x/sys/unix provides portable way to use uname(2)
syscall, let's use it for all unix variants, removing the duplicated
code from uname_*.go.

While at it:
 - remove uname_unsupported*.go (not used);
 - use unix.ByteSliceToString.

NOTE this also removes the public definition of struct Utsname. I am
puzzled as to why it was exported as it is not (and was not) used by
any public functions in this package, nor I was able to find any usage
of kernel.Utsname (using sourcegraph).

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Apr 15, 2024
1 parent 1fd0dc1 commit 6db0181
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 92 deletions.
24 changes: 5 additions & 19 deletions pkg/parsers/kernel/kernel_unix.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
//go:build linux || freebsd || solaris || openbsd
// +build linux freebsd solaris openbsd

// Package kernel provides helper function to get, parse and compare kernel
// versions for different platforms.
package kernel

import (
"bytes"

"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

// GetKernelVersion gets the current kernel version.
func GetKernelVersion() (*VersionInfo, error) {
uts, err := uname()
if err != nil {
return nil, err
}
uts := &unix.Utsname{}

release := make([]byte, len(uts.Release))

i := 0
for _, c := range uts.Release {
release[i] = byte(c)
i++
if err := unix.Uname(uts); err != nil {
return nil, err
}

// Remove the \x00 from the release for Atoi to parse correctly
release = release[:bytes.IndexByte(release, 0)]

return ParseRelease(string(release))
return ParseRelease(unix.ByteSliceToString(uts.Release[:]))
}

// CheckKernelVersion checks if current kernel is newer than (or equal to)
Expand Down
17 changes: 0 additions & 17 deletions pkg/parsers/kernel/uname_freebsd.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/parsers/kernel/uname_linux.go

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/parsers/kernel/uname_solaris.go

This file was deleted.

14 changes: 0 additions & 14 deletions pkg/parsers/kernel/uname_unsupported.go

This file was deleted.

11 changes: 0 additions & 11 deletions pkg/parsers/kernel/uname_unsupported_type.go

This file was deleted.

0 comments on commit 6db0181

Please sign in to comment.