From a1a03af8a4f4752343f3ef5abb520aab811f5d8b Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 20 Feb 2025 20:11:43 +0100 Subject: [PATCH] chore(golangci-lint): GOOS and GOARCH matrix --- .github/workflows/lint.yml | 31 ++++++++++++++++++++++- cpu/cpu_dragonfly.go | 5 ++-- cpu/cpu_netbsd.go | 2 +- cpu/cpu_openbsd.go | 5 ++-- cpu/cpu_plan9.go | 1 + cpu/cpu_solaris.go | 32 ++++++++++++------------ disk/disk_openbsd.go | 3 ++- disk/disk_solaris.go | 6 +++-- host/host_openbsd.go | 3 ++- host/host_solaris.go | 16 ++++++------ mem/mem_bsd_test.go | 19 +++++++-------- mem/mem_linux_test.go | 5 ++-- mem/mem_openbsd.go | 3 ++- mem/mem_plan9.go | 1 + mem/mem_solaris.go | 13 +++++----- mem/mem_solaris_test.go | 12 ++++----- net/net.go | 4 +-- net/net_darwin.go | 2 +- net/net_freebsd.go | 2 +- net/net_linux.go | 2 +- net/net_linux_test.go | 7 +++--- net/net_openbsd.go | 6 ++--- net/net_solaris.go | 5 ++-- net/net_test.go | 6 +---- net/net_windows.go | 2 +- process/process_openbsd.go | 12 ++++----- process/process_solaris.go | 5 +--- process/process_windows_32bit.go | 42 +++++++++++++++----------------- sensors/sensors_darwin.go | 8 +++--- sensors/sensors_solaris.go | 3 ++- 30 files changed, 146 insertions(+), 117 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f31683027..51c912dd3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -12,12 +12,41 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, windows-latest, macos-latest] + include: + - {os: macos-latest, GOOS: darwin, GOARCH: amd64} + - {os: macos-latest, GOOS: darwin, GOARCH: arm64} + - {os: ubuntu-latest, GOOS: dragonfly, GOARCH: amd64} + - {os: ubuntu-latest, GOOS: freebsd, GOARCH: amd64} + - {os: ubuntu-latest, GOOS: freebsd, GOARCH: 386} + - {os: ubuntu-latest, GOOS: freebsd, GOARCH: arm} + - {os: ubuntu-latest, GOOS: linux, GOARCH: 386} + - {os: ubuntu-latest, GOOS: linux, GOARCH: amd64} + - {os: ubuntu-latest, GOOS: linux, GOARCH: arm64} + - {os: ubuntu-latest, GOOS: linux, GOARCH: arm} + - {os: ubuntu-latest, GOOS: linux, GOARCH: mips64} + - {os: ubuntu-latest, GOOS: linux, GOARCH: mips64le} + - {os: ubuntu-latest, GOOS: linux, GOARCH: mips} + - {os: ubuntu-latest, GOOS: linux, GOARCH: mipsle} + - {os: ubuntu-latest, GOOS: linux, GOARCH: ppc64le} + - {os: ubuntu-latest, GOOS: linux, GOARCH: ppc64} + - {os: ubuntu-latest, GOOS: linux, GOARCH: riscv64} + - {os: ubuntu-latest, GOOS: linux, GOARCH: s390x} + - {os: ubuntu-latest, GOOS: netbsd, GOARCH: amd64} + - {os: ubuntu-latest, GOOS: openbsd, GOARCH: 386} + - {os: ubuntu-latest, GOOS: openbsd, GOARCH: amd64} + - {os: ubuntu-latest, GOOS: plan9, GOARCH: amd64} + - {os: ubuntu-latest, GOOS: plan9, GOARCH: 386} + - {os: ubuntu-latest, GOOS: solaris, GOARCH: amd64} + - {os: windows-latest, GOOS: windows, GOARCH: amd64} + - {os: windows-latest, GOOS: windows, GOARCH: 386} permissions: contents: read # for actions/checkout to fetch code pull-requests: read # for golangci/golangci-lint-action to fetch pull requests name: lint runs-on: ${{ matrix.os }} + env: + GOARCH: ${{ matrix.GOARCH }} + GOOS: ${{ matrix.GOOS }} steps: - name: Checkout repository uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 diff --git a/cpu/cpu_dragonfly.go b/cpu/cpu_dragonfly.go index 19b1e9dd3..fb9b93736 100644 --- a/cpu/cpu_dragonfly.go +++ b/cpu/cpu_dragonfly.go @@ -11,9 +11,10 @@ import ( "strings" "unsafe" - "github.com/shirou/gopsutil/v4/internal/common" "github.com/tklauser/go-sysconf" "golang.org/x/sys/unix" + + "github.com/shirou/gopsutil/v4/internal/common" ) var ( @@ -135,7 +136,7 @@ func parseDmesgBoot(fileName string) (InfoStat, error) { c.VendorID = matches[1] t, err := strconv.ParseInt(matches[2], 10, 32) if err != nil { - return c, fmt.Errorf("unable to parse DragonflyBSD CPU stepping information from %q: %v", line, err) + return c, fmt.Errorf("unable to parse DragonflyBSD CPU stepping information from %q: %w", line, err) } c.Stepping = int32(t) } else if matches := featuresMatch.FindStringSubmatch(line); matches != nil { diff --git a/cpu/cpu_netbsd.go b/cpu/cpu_netbsd.go index 198be5e64..7d807dc43 100644 --- a/cpu/cpu_netbsd.go +++ b/cpu/cpu_netbsd.go @@ -57,7 +57,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er ncpu, err := unix.SysctlUint32("hw.ncpu") if err != nil { - return + return //nolint:nakedret //FIXME } var i uint32 diff --git a/cpu/cpu_openbsd.go b/cpu/cpu_openbsd.go index 33233d3c7..30d87ac68 100644 --- a/cpu/cpu_openbsd.go +++ b/cpu/cpu_openbsd.go @@ -9,9 +9,10 @@ import ( "runtime" "unsafe" - "github.com/shirou/gopsutil/v4/internal/common" "github.com/tklauser/go-sysconf" "golang.org/x/sys/unix" + + "github.com/shirou/gopsutil/v4/internal/common" ) const ( @@ -74,7 +75,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er ncpu, err := unix.SysctlUint32("hw.ncpu") if err != nil { - return + return //nolint:nakedret //FIXME } var i uint32 diff --git a/cpu/cpu_plan9.go b/cpu/cpu_plan9.go index bff2e0c75..d6f63ba1e 100644 --- a/cpu/cpu_plan9.go +++ b/cpu/cpu_plan9.go @@ -9,6 +9,7 @@ import ( "runtime" stats "github.com/lufia/plan9stats" + "github.com/shirou/gopsutil/v4/internal/common" ) diff --git a/cpu/cpu_solaris.go b/cpu/cpu_solaris.go index d8ba1d324..91d41a9f8 100644 --- a/cpu/cpu_solaris.go +++ b/cpu/cpu_solaris.go @@ -42,7 +42,7 @@ var kstatSplit = regexp.MustCompile(`[:\s]+`) func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/") if err != nil { - return nil, fmt.Errorf("cannot execute kstat: %s", err) + return nil, fmt.Errorf("cannot execute kstat: %w", err) } cpu := make(map[float64]float64) idle := make(map[float64]float64) @@ -57,29 +57,29 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { } cpuNumber, err := strconv.ParseFloat(fields[1], 64) if err != nil { - return nil, fmt.Errorf("cannot parse cpu number: %s", err) + return nil, fmt.Errorf("cannot parse cpu number: %w", err) } cpu[cpuNumber] = cpuNumber switch fields[3] { case "idle": idle[cpuNumber], err = strconv.ParseFloat(fields[4], 64) if err != nil { - return nil, fmt.Errorf("cannot parse idle: %s", err) + return nil, fmt.Errorf("cannot parse idle: %w", err) } case "user": user[cpuNumber], err = strconv.ParseFloat(fields[4], 64) if err != nil { - return nil, fmt.Errorf("cannot parse user: %s", err) + return nil, fmt.Errorf("cannot parse user: %w", err) } case "kernel": kern[cpuNumber], err = strconv.ParseFloat(fields[4], 64) if err != nil { - return nil, fmt.Errorf("cannot parse kernel: %s", err) + return nil, fmt.Errorf("cannot parse kernel: %w", err) } case "iowait": iowt[cpuNumber], err = strconv.ParseFloat(fields[4], 64) if err != nil { - return nil, fmt.Errorf("cannot parse iowait: %s", err) + return nil, fmt.Errorf("cannot parse iowait: %w", err) } //not sure how this translates, don't report, add to kernel, something else? /*case "swap": @@ -121,22 +121,22 @@ func Info() ([]InfoStat, error) { func InfoWithContext(ctx context.Context) ([]InfoStat, error) { psrInfoOut, err := invoke.CommandWithContext(ctx, "psrinfo", "-p", "-v") if err != nil { - return nil, fmt.Errorf("cannot execute psrinfo: %s", err) + return nil, fmt.Errorf("cannot execute psrinfo: %w", err) } procs, err := parseProcessorInfo(string(psrInfoOut)) if err != nil { - return nil, fmt.Errorf("error parsing psrinfo output: %s", err) + return nil, fmt.Errorf("error parsing psrinfo output: %w", err) } isaInfoOut, err := invoke.CommandWithContext(ctx, "isainfo", "-b", "-v") if err != nil { - return nil, fmt.Errorf("cannot execute isainfo: %s", err) + return nil, fmt.Errorf("cannot execute isainfo: %w", err) } flags, err := parseISAInfo(string(isaInfoOut)) if err != nil { - return nil, fmt.Errorf("error parsing isainfo output: %s", err) + return nil, fmt.Errorf("error parsing isainfo output: %w", err) } result := make([]InfoStat, 0, len(flags)) @@ -160,7 +160,7 @@ func parseISAInfo(cmdOutput string) ([]string, error) { } flags := make([]string, len(words)-4) - for i, val := range words[4:] { + for i, val := range words[4:] { //nolint:gosimple //FIXME flags[i] = val } sort.Strings(flags) @@ -194,7 +194,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) { if physicalCPU[psrStepOffset] != "" { stepParsed, err := strconv.ParseInt(physicalCPU[psrStepOffset], 10, 32) if err != nil { - return nil, fmt.Errorf("cannot parse value %q for step as 32-bit integer: %s", physicalCPU[9], err) + return nil, fmt.Errorf("cannot parse value %q for step as 32-bit integer: %w", physicalCPU[9], err) } step = int32(stepParsed) } @@ -202,7 +202,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) { if physicalCPU[psrClockOffset] != "" { clockParsed, err := strconv.ParseInt(physicalCPU[psrClockOffset], 10, 64) if err != nil { - return nil, fmt.Errorf("cannot parse value %q for clock as 32-bit integer: %s", physicalCPU[10], err) + return nil, fmt.Errorf("cannot parse value %q for clock as 32-bit integer: %w", physicalCPU[10], err) } clock = float64(clockParsed) } @@ -214,7 +214,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) { case physicalCPU[psrNumCoresOffset] != "": numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresOffset], 10, 32) if err != nil { - return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[1], err) + return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %w", physicalCPU[1], err) } for i := 0; i < int(numCores); i++ { @@ -235,12 +235,12 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) { case physicalCPU[psrNumCoresHTOffset] != "": numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresHTOffset], 10, 32) if err != nil { - return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[3], err) + return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %w", physicalCPU[3], err) } numHT, err = strconv.ParseInt(physicalCPU[psrNumHTOffset], 10, 32) if err != nil { - return nil, fmt.Errorf("cannot parse value %q for hyperthread count as 32-bit integer: %s", physicalCPU[4], err) + return nil, fmt.Errorf("cannot parse value %q for hyperthread count as 32-bit integer: %w", physicalCPU[4], err) } for i := 0; i < int(numCores); i++ { diff --git a/disk/disk_openbsd.go b/disk/disk_openbsd.go index 713e38e1d..c840088df 100644 --- a/disk/disk_openbsd.go +++ b/disk/disk_openbsd.go @@ -8,8 +8,9 @@ import ( "context" "encoding/binary" - "github.com/shirou/gopsutil/v4/internal/common" "golang.org/x/sys/unix" + + "github.com/shirou/gopsutil/v4/internal/common" ) func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { diff --git a/disk/disk_solaris.go b/disk/disk_solaris.go index f849da96b..7360a47a2 100644 --- a/disk/disk_solaris.go +++ b/disk/disk_solaris.go @@ -7,6 +7,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "math" "os" @@ -16,8 +17,9 @@ import ( "strconv" "strings" - "github.com/shirou/gopsutil/v4/internal/common" "golang.org/x/sys/unix" + + "github.com/shirou/gopsutil/v4/internal/common" ) const ( @@ -100,7 +102,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC } lines := strings.Split(strings.TrimSpace(string(kstatSysOut)), "\n") if len(lines) == 0 { - return nil, fmt.Errorf("no disk class found") + return nil, errors.New("no disk class found") } dnamearr := make(map[string]string) nreadarr := make(map[string]uint64) diff --git a/host/host_openbsd.go b/host/host_openbsd.go index f21c5e859..d5a5cd10d 100644 --- a/host/host_openbsd.go +++ b/host/host_openbsd.go @@ -12,9 +12,10 @@ import ( "strings" "unsafe" + "golang.org/x/sys/unix" + "github.com/shirou/gopsutil/v4/internal/common" "github.com/shirou/gopsutil/v4/process" - "golang.org/x/sys/unix" ) const ( diff --git a/host/host_solaris.go b/host/host_solaris.go index 371cc98e8..e7adba017 100644 --- a/host/host_solaris.go +++ b/host/host_solaris.go @@ -7,6 +7,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "os" "regexp" @@ -31,14 +32,13 @@ func HostIDWithContext(ctx context.Context) (string, error) { line := sc.Text() // If we're in the global zone, rely on the hostname. - if line == "global" { - hostname, err := os.Hostname() - if err == nil { - return hostname, nil - } - } else { + if line != "global" { return strings.TrimSpace(line), nil } + hostname, err := os.Hostname() + if err == nil { + return hostname, nil + } } } } @@ -84,7 +84,7 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { } func UptimeWithContext(ctx context.Context) (uint64, error) { - bootTime, err := BootTime() + bootTime, err := BootTime() //nolint:contextcheck //FIXME if err != nil { return 0, err } @@ -139,7 +139,7 @@ func parseUnameOutput(ctx context.Context) (string, string, string, error) { fields := strings.Fields(string(out)) if len(fields) < 3 { - return "", "", "", fmt.Errorf("malformed `uname` output") + return "", "", "", errors.New("malformed `uname` output") } return fields[0], fields[1], fields[2], nil diff --git a/mem/mem_bsd_test.go b/mem/mem_bsd_test.go index 8b0eb9f06..ef2ae3a23 100644 --- a/mem/mem_bsd_test.go +++ b/mem/mem_bsd_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const validFreeBSD = `Device: 1kB-blocks Used: @@ -24,33 +25,31 @@ const invalid = `Device: 512-blocks Used: ` func TestParseSwapctlOutput_FreeBSD(t *testing.T) { - assert := assert.New(t) stats, err := parseSwapctlOutput(validFreeBSD) - assert.NoError(err) + require.NoError(t, err) - assert.Equal(*stats[0], SwapDevice{ + assert.Equal(t, SwapDevice{ Name: "/dev/gpt/swapfs", UsedBytes: 1263616, FreeBytes: 1072478208, - }) + }, *stats[0]) - assert.Equal(*stats[1], SwapDevice{ + assert.Equal(t, SwapDevice{ Name: "/dev/md0", UsedBytes: 681984, FreeBytes: 1073059840, - }) + }, *stats[1]) } func TestParseSwapctlOutput_OpenBSD(t *testing.T) { - assert := assert.New(t) stats, err := parseSwapctlOutput(validOpenBSD) - assert.NoError(err) + require.NoError(t, err) - assert.Equal(*stats[0], SwapDevice{ + assert.Equal(t, SwapDevice{ Name: "/dev/wd0b", UsedBytes: 1234 * 1024, FreeBytes: 653791 * 1024, - }) + }, *stats[0]) } func TestParseSwapctlOutput_Invalid(t *testing.T) { diff --git a/mem/mem_linux_test.go b/mem/mem_linux_test.go index b71b71c79..ac02fb806 100644 --- a/mem/mem_linux_test.go +++ b/mem/mem_linux_test.go @@ -151,17 +151,16 @@ const invalidFile = `INVALID Type Size Used Priority ` func TestParseSwapsFile_ValidFile(t *testing.T) { - assert := assert.New(t) stats, err := parseSwapsFile(context.Background(), strings.NewReader(validFile)) require.NoError(t, err) - assert.Equal(SwapDevice{ + assert.Equal(t, SwapDevice{ Name: "/dev/dm-2", UsedBytes: 502566912, FreeBytes: 68128825344, }, *stats[0]) - assert.Equal(SwapDevice{ + assert.Equal(t, SwapDevice{ Name: "/swapfile", UsedBytes: 1024, FreeBytes: 1024, diff --git a/mem/mem_openbsd.go b/mem/mem_openbsd.go index 2510bb0d3..ade3a1270 100644 --- a/mem/mem_openbsd.go +++ b/mem/mem_openbsd.go @@ -10,8 +10,9 @@ import ( "errors" "fmt" - "github.com/shirou/gopsutil/v4/internal/common" "golang.org/x/sys/unix" + + "github.com/shirou/gopsutil/v4/internal/common" ) func GetPageSize() (uint64, error) { diff --git a/mem/mem_plan9.go b/mem/mem_plan9.go index c17a102ee..4f1990c20 100644 --- a/mem/mem_plan9.go +++ b/mem/mem_plan9.go @@ -8,6 +8,7 @@ import ( "os" stats "github.com/lufia/plan9stats" + "github.com/shirou/gopsutil/v4/internal/common" ) diff --git a/mem/mem_solaris.go b/mem/mem_solaris.go index 06d0d9a00..ca5af4838 100644 --- a/mem/mem_solaris.go +++ b/mem/mem_solaris.go @@ -11,8 +11,9 @@ import ( "strconv" "strings" - "github.com/shirou/gopsutil/v4/internal/common" "github.com/tklauser/go-sysconf" + + "github.com/shirou/gopsutil/v4/internal/common" ) // VirtualMemory for Solaris is a minimal implementation which only returns @@ -24,17 +25,17 @@ func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { result := &VirtualMemoryStat{} - zoneName, err := zoneName() + zoneName, err := zoneName() //nolint:contextcheck //FIXME if err != nil { return nil, err } if zoneName == "global" { - cap, err := globalZoneMemoryCapacity() + capacity, err := globalZoneMemoryCapacity() //nolint:contextcheck //FIXME if err != nil { return nil, err } - result.Total = cap + result.Total = capacity freemem, err := globalZoneFreeMemory(ctx) if err != nil { return nil, err @@ -43,11 +44,11 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { result.Free = freemem result.Used = result.Total - result.Free } else { - cap, err := nonGlobalZoneMemoryCapacity() + capacity, err := nonGlobalZoneMemoryCapacity() //nolint:contextcheck //FIXME if err != nil { return nil, err } - result.Total = cap + result.Total = capacity } return result, nil diff --git a/mem/mem_solaris_test.go b/mem/mem_solaris_test.go index 5e0aa700f..31b35e03d 100644 --- a/mem/mem_solaris_test.go +++ b/mem/mem_solaris_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const validFile = `swapfile dev swaplo blocks free @@ -18,21 +19,20 @@ const invalidFile = `swapfile dev swaplo INVALID free /dev/dsk/c0t0d0s1 136,1 16 1638608 1600528` func TestParseSwapsCommandOutput_Valid(t *testing.T) { - assert := assert.New(t) stats, err := parseSwapsCommandOutput(validFile) - assert.NoError(err) + require.NoError(t, err) - assert.Equal(*stats[0], SwapDevice{ + assert.Equal(t, SwapDevice{ Name: "/dev/zvol/dsk/rpool/swap", UsedBytes: 0, FreeBytes: 1058800 * 512, - }) + }, *stats[0]) - assert.Equal(*stats[1], SwapDevice{ + assert.Equal(t, SwapDevice{ Name: "/dev/dsk/c0t0d0s1", UsedBytes: 38080 * 512, FreeBytes: 1600528 * 512, - }) + }, *stats[1]) } func TestParseSwapsCommandOutput_Invalid(t *testing.T) { diff --git a/net/net.go b/net/net.go index 74af54a45..e114fab44 100644 --- a/net/net.go +++ b/net/net.go @@ -255,7 +255,7 @@ func InterfacesWithContext(ctx context.Context) (InterfaceStatList, error) { return ret, nil } -func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) { +func getIOCountersAll(n []IOCountersStat) []IOCountersStat { r := IOCountersStat{ Name: "all", } @@ -270,7 +270,7 @@ func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) { r.Dropout += nic.Dropout } - return []IOCountersStat{r}, nil + return []IOCountersStat{r} } // NetIOCounters returns network I/O statistics for every network diff --git a/net/net_darwin.go b/net/net_darwin.go index f640931f2..08266fd90 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -249,7 +249,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, } if !pernic { - return getIOCountersAll(ret) + return getIOCountersAll(ret), nil } return ret, nil } diff --git a/net/net_freebsd.go b/net/net_freebsd.go index 655e13373..33d0f7c63 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -85,7 +85,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, } if !pernic { - return getIOCountersAll(ret) + return getIOCountersAll(ret), nil } return ret, nil diff --git a/net/net_linux.go b/net/net_linux.go index 23113fea7..6072965cb 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -128,7 +128,7 @@ func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename stri } if !pernic { - return getIOCountersAll(ret) + return getIOCountersAll(ret), nil } return ret, nil diff --git a/net/net_linux_test.go b/net/net_linux_test.go index abd33963f..bb52c9065 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -138,7 +138,6 @@ type AddrTest struct { } func TestDecodeAddress(t *testing.T) { - assert := assert.New(t) addr := map[string]AddrTest{ "11111:0035": { @@ -182,11 +181,11 @@ func TestDecodeAddress(t *testing.T) { } addr, err := decodeAddress(uint32(family), src) if dst.Error { - assert.Error(err, src) + assert.Error(t, err, src) } else { require.NoError(t, err, src) - assert.Equal(dst.IP, addr.IP, src) - assert.Equal(dst.Port, int(addr.Port), src) + assert.Equal(t, dst.IP, addr.IP, src) + assert.Equal(t, dst.Port, int(addr.Port), src) } } } diff --git a/net/net_openbsd.go b/net/net_openbsd.go index a90d5904a..ed9c506d1 100644 --- a/net/net_openbsd.go +++ b/net/net_openbsd.go @@ -150,15 +150,15 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, ret = append(ret, ioc) } - if pernic == false { - return getIOCountersAll(ret) + if !pernic { + return getIOCountersAll(ret), nil } return ret, nil } func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { - return IOCounters(pernic) + return IOCounters(pernic) //nolint:contextcheck //FIXME } func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { diff --git a/net/net_solaris.go b/net/net_solaris.go index 83eb1d0f7..e44979d72 100644 --- a/net/net_solaris.go +++ b/net/net_solaris.go @@ -5,6 +5,7 @@ package net import ( "context" + "errors" "fmt" "regexp" "runtime" @@ -29,7 +30,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, lines := strings.Split(strings.TrimSpace(string(kstatSysOut)), "\n") if len(lines) == 0 { - return nil, fmt.Errorf("no interface found") + return nil, errors.New("no interface found") } rbytes64arr := make(map[string]uint64) ipackets64arr := make(map[string]uint64) @@ -104,7 +105,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, } if !pernic { - return getIOCountersAll(ret) + return getIOCountersAll(ret), nil } return ret, nil diff --git a/net/net_test.go b/net/net_test.go index 60e843894..630b64be4 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -133,11 +133,7 @@ func TestGetNetIOCountersAll(t *testing.T) { Errin: 10, }, } - ret, err := getIOCountersAll(n) - skipIfNotImplementedErr(t, err) - if err != nil { - t.Error(err) - } + ret := getIOCountersAll(n) if len(ret) != 1 { t.Errorf("invalid return count") } diff --git a/net/net_windows.go b/net/net_windows.go index aa3cb6731..d1029cfdd 100644 --- a/net/net_windows.go +++ b/net/net_windows.go @@ -193,7 +193,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, } if !pernic { - return getIOCountersAll(counters) + return getIOCountersAll(counters), nil } return counters, nil } diff --git a/process/process_openbsd.go b/process/process_openbsd.go index 5e8a9e0b4..381b14370 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -8,7 +8,6 @@ import ( "context" "encoding/binary" "errors" - "fmt" "io" "path/filepath" "sort" @@ -16,11 +15,12 @@ import ( "strings" "unsafe" - cpu "github.com/shirou/gopsutil/v4/cpu" - "github.com/shirou/gopsutil/v4/internal/common" - mem "github.com/shirou/gopsutil/v4/mem" - net "github.com/shirou/gopsutil/v4/net" "golang.org/x/sys/unix" + + "github.com/shirou/gopsutil/v4/cpu" + "github.com/shirou/gopsutil/v4/internal/common" + "github.com/shirou/gopsutil/v4/mem" + "github.com/shirou/gopsutil/v4/net" ) func pidsWithContext(ctx context.Context) ([]int32, error) { @@ -130,7 +130,7 @@ func readPtr(r io.Reader) (uintptr, error) { } return uintptr(p), nil default: - return 0, fmt.Errorf("unsupported pointer size") + return 0, errors.New("unsupported pointer size") } } diff --git a/process/process_solaris.go b/process/process_solaris.go index 5c8d4d3b1..279d11ce8 100644 --- a/process/process_solaris.go +++ b/process/process_solaris.go @@ -247,10 +247,7 @@ func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error return "", err } ret := strings.FieldsFunc(string(cmdline), func(r rune) bool { - if r == '\u0000' { - return true - } - return false + return r == '\u0000' }) return strings.Join(ret, " "), nil diff --git a/process/process_windows_32bit.go b/process/process_windows_32bit.go index 2b231c79d..d69916bf0 100644 --- a/process/process_windows_32bit.go +++ b/process/process_windows_32bit.go @@ -8,8 +8,9 @@ import ( "syscall" "unsafe" - "github.com/shirou/gopsutil/v4/internal/common" "golang.org/x/sys/windows" + + "github.com/shirou/gopsutil/v4/internal/common" ) type PROCESS_MEMORY_COUNTERS struct { @@ -39,30 +40,27 @@ func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, er ) if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { return uint64(info.PebBaseAddress), nil - } else { - return 0, windows.NTStatus(ret) } - } else { - // we are on a 32-bit process reading an external 64-bit process - if common.ProcNtWow64QueryInformationProcess64.Find() == nil { // avoid panic - var info processBasicInformation64 + return 0, windows.NTStatus(ret) + } + // we are on a 32-bit process reading an external 64-bit process + if common.ProcNtWow64QueryInformationProcess64.Find() != nil { + return 0, errors.New("can't find API to query 64 bit process from 32 bit") + } + // avoid panic + var info processBasicInformation64 - ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return info.PebBaseAddress, nil - } else { - return 0, windows.NTStatus(ret) - } - } else { - return 0, errors.New("can't find API to query 64 bit process from 32 bit") - } + ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( + uintptr(procHandle), + uintptr(common.ProcessBasicInformation), + uintptr(unsafe.Pointer(&info)), + uintptr(unsafe.Sizeof(info)), + uintptr(0), + ) + if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { + return info.PebBaseAddress, nil } + return 0, windows.NTStatus(ret) } func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte { diff --git a/sensors/sensors_darwin.go b/sensors/sensors_darwin.go index b8535e793..23a3a7a1e 100644 --- a/sensors/sensors_darwin.go +++ b/sensors/sensors_darwin.go @@ -5,7 +5,7 @@ package sensors import ( "context" - "fmt" + "errors" "unsafe" "github.com/shirou/gopsutil/v4/internal/common" @@ -129,7 +129,7 @@ func readSMC(smc *common.SMC, key string) (*smcReturn, error) { resultSmc.kSMC = result.result if err != nil || result.result != common.KSMCSuccess { - return resultSmc, fmt.Errorf("ERROR: IOConnectCallStructMethod failed") + return resultSmc, errors.New("ERROR: IOConnectCallStructMethod failed") } resultSmc.dataSize = uint32(result.keyInfo.dataSize) @@ -158,7 +158,7 @@ func callSMC(smc *common.SMC, input *smcParamStruct) (*smcParamStruct, error) { uintptr(unsafe.Pointer(input)), inputCnt, uintptr(unsafe.Pointer(output)), &outputCnt) if result != 0 { - return output, fmt.Errorf("ERROR: IOConnectCallStructMethod failed") + return output, errors.New("ERROR: IOConnectCallStructMethod failed") } return output, nil @@ -169,7 +169,7 @@ func toUint32(key string) uint32 { return 0 } - var ans uint32 = 0 + var ans uint32 var shift uint32 = 24 for i := 0; i < smcKeySize; i++ { diff --git a/sensors/sensors_solaris.go b/sensors/sensors_solaris.go index c5d1bce8d..0ca1d9a1e 100644 --- a/sensors/sensors_solaris.go +++ b/sensors/sensors_solaris.go @@ -6,6 +6,7 @@ package sensors import ( "context" "encoding/csv" + "errors" "io" "strconv" "strings" @@ -24,7 +25,7 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) { r.FieldsPerRecord = -1 for { record, err := r.Read() - if err == io.EOF { + if errors.Is(err, io.EOF) { break } if err != nil {