Skip to content

Commit 83737c5

Browse files
committed
chore: enable golangci-lint on Windows and MacOS
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 90e5996 commit 83737c5

19 files changed

+128
-112
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.go text eol=lf

.github/workflows/lint.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ permissions:
99

1010
jobs:
1111
golangci:
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
os: [ubuntu-latest, windows-latest, macos-latest]
1216
permissions:
1317
contents: read # for actions/checkout to fetch code
1418
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
1519
name: lint
16-
runs-on: ubuntu-latest
20+
runs-on: ${{ matrix.os }}
1721
steps:
1822
- name: Checkout repository
1923
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

cpu/cpu_windows.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package cpu
66
import (
77
"context"
88
"fmt"
9+
"strconv"
910
"unsafe"
1011

11-
"github.com/shirou/gopsutil/v4/internal/common"
1212
"github.com/yusufpapurcu/wmi"
1313
"golang.org/x/sys/windows"
14+
15+
"github.com/shirou/gopsutil/v4/internal/common"
1416
)
1517

1618
var procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")
@@ -110,7 +112,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
110112

111113
cpu := InfoStat{
112114
CPU: int32(i),
113-
Family: fmt.Sprintf("%d", l.Family),
115+
Family: strconv.FormatUint(uint64(l.Family), 10),
114116
VendorID: l.Manufacturer,
115117
ModelName: l.Name,
116118
Cores: int32(l.NumberOfLogicalProcessors),

disk/disk_darwin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (i *ioCounters) getDriveStat(d uint32) (*IOCountersStat, error) {
222222
defer i.ioObjectRelease(parent)
223223

224224
if !ioObjectConformsTo(parent, "IOBlockStorageDriver") {
225-
//return nil, fmt.Errorf("ERROR: the object is not of the IOBlockStorageDriver class")
225+
// return nil, fmt.Errorf("ERROR: the object is not of the IOBlockStorageDriver class")
226226
return nil, nil
227227
}
228228

disk/disk_windows.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ package disk
66
import (
77
"bytes"
88
"context"
9-
"fmt"
9+
"errors"
1010
"syscall"
1111
"unsafe"
1212

13-
"github.com/shirou/gopsutil/v4/internal/common"
1413
"golang.org/x/sys/windows"
1514
"golang.org/x/sys/windows/registry"
15+
16+
"github.com/shirou/gopsutil/v4/internal/common"
1617
)
1718

1819
var (
@@ -202,11 +203,11 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
202203
if typeret != windows.DRIVE_FIXED {
203204
continue
204205
}
205-
szDevice := fmt.Sprintf(`\\.\%s`, path)
206+
szDevice := `\\.\` + path
206207
const IOCTL_DISK_PERFORMANCE = 0x70020
207208
h, err := windows.CreateFile(syscall.StringToUTF16Ptr(szDevice), 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
208209
if err != nil {
209-
if err == windows.ERROR_FILE_NOT_FOUND {
210+
if errors.Is(err, windows.ERROR_FILE_NOT_FOUND) {
210211
continue
211212
}
212213
return drivemap, err

host/host_windows.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
"time"
1414
"unsafe"
1515

16+
"golang.org/x/sys/windows"
17+
1618
"github.com/shirou/gopsutil/v4/internal/common"
1719
"github.com/shirou/gopsutil/v4/process"
18-
"golang.org/x/sys/windows"
1920
)
2021

2122
var (
@@ -79,7 +80,7 @@ func HostIDWithContext(ctx context.Context) (string, error) {
7980
hostID := windows.UTF16ToString(regBuf[:])
8081
hostIDLen := len(hostID)
8182
if hostIDLen != uuidLen {
82-
return "", fmt.Errorf("HostID incorrect: %q\n", hostID)
83+
return "", fmt.Errorf("HostID incorrect: %q\n", hostID) //nolint:revive //FIXME
8384
}
8485

8586
return strings.ToLower(hostID), nil
@@ -135,15 +136,15 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
135136
return t, nil
136137
}
137138

138-
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
139-
platform, family, _, displayVersion, err := platformInformation(ctx)
139+
func PlatformInformationWithContext(_ context.Context) (platform string, family string, version string, err error) {
140+
platform, family, _, displayVersion, err := platformInformation()
140141
if err != nil {
141142
return "", "", "", err
142143
}
143144
return platform, family, displayVersion, nil
144145
}
145146

146-
func platformInformation(ctx context.Context) (platform, family, version, displayVersion string, err error) {
147+
func platformInformation() (platform, family, version, displayVersion string, err error) {
147148
// GetVersionEx lies on Windows 8.1 and returns as Windows 8 if we don't declare compatibility in manifest
148149
// RtlGetVersion bypasses this lying layer and returns the true Windows version
149150
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/nf-wdm-rtlgetversion
@@ -152,26 +153,26 @@ func platformInformation(ctx context.Context) (platform, family, version, displa
152153
osInfo.dwOSVersionInfoSize = uint32(unsafe.Sizeof(osInfo))
153154
ret, _, err := procRtlGetVersion.Call(uintptr(unsafe.Pointer(&osInfo)))
154155
if ret != 0 {
155-
return
156+
return //nolint:nakedret //FIXME
156157
}
157158

158159
// Platform
159160
var h windows.Handle // like HostIDWithContext(), we query the registry using the raw windows.RegOpenKeyEx/RegQueryValueEx
160161
err = windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Windows NT\CurrentVersion`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h)
161162
if err != nil {
162-
return
163+
return //nolint:nakedret //FIXME
163164
}
164165
defer windows.RegCloseKey(h)
165166
var bufLen uint32
166167
var valType uint32
167168
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`ProductName`), nil, &valType, nil, &bufLen)
168169
if err != nil {
169-
return
170+
return //nolint:nakedret //FIXME
170171
}
171172
regBuf := make([]uint16, bufLen/2+1)
172173
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`ProductName`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
173174
if err != nil {
174-
return
175+
return //nolint:nakedret //FIXME
175176
}
176177
platform = windows.UTF16ToString(regBuf[:])
177178
if strings.Contains(platform, "Windows 10") { // check build number to determine whether it's actually Windows 11
@@ -243,8 +244,8 @@ func VirtualizationWithContext(ctx context.Context) (string, string, error) {
243244
return "", "", common.ErrNotImplementedError
244245
}
245246

246-
func KernelVersionWithContext(ctx context.Context) (string, error) {
247-
_, _, version, _, err := platformInformation(ctx)
247+
func KernelVersionWithContext(_ context.Context) (string, error) {
248+
_, _, version, _, err := platformInformation()
248249
return version, err
249250
}
250251

internal/common/common_darwin.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package common
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"os"
1011
"os/exec"
@@ -306,7 +307,7 @@ const (
306307

307308
func NewSMC(ioKit *Library) (*SMC, error) {
308309
if ioKit.path != IOKit {
309-
return nil, fmt.Errorf("library is not IOKit")
310+
return nil, errors.New("library is not IOKit")
310311
}
311312

312313
ioServiceGetMatchingService := GetFunc[IOServiceGetMatchingServiceFunc](ioKit, IOServiceGetMatchingServiceSym)
@@ -324,7 +325,7 @@ func NewSMC(ioKit *Library) (*SMC, error) {
324325

325326
var conn uint32
326327
if result := ioServiceOpen(service, machTaskSelf(), 0, &conn); result != 0 {
327-
return nil, fmt.Errorf("ERROR: IOServiceOpen failed")
328+
return nil, errors.New("ERROR: IOServiceOpen failed")
328329
}
329330

330331
ioObjectRelease(service)
@@ -343,7 +344,7 @@ func (s *SMC) Close() error {
343344
ioServiceClose := GetFunc[IOServiceCloseFunc](s.lib, IOServiceCloseSym)
344345

345346
if result := ioServiceClose(s.conn); result != 0 {
346-
return fmt.Errorf("ERROR: IOServiceClose failed")
347+
return errors.New("ERROR: IOServiceClose failed")
347348
}
348349
return nil
349350
}
@@ -367,8 +368,8 @@ func (s CStr) Ptr() *byte {
367368
return &s[0]
368369
}
369370

370-
func (c CStr) Addr() uintptr {
371-
return uintptr(unsafe.Pointer(c.Ptr()))
371+
func (s CStr) Addr() uintptr {
372+
return uintptr(unsafe.Pointer(s.Ptr()))
372373
}
373374

374375
func (s CStr) GoString() string {

internal/common/common_windows.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,19 @@ type SystemExtendedHandleInformation struct {
273273
// CallWithExpandingBuffer https://github.com/hillu/go-ntdll
274274
func CallWithExpandingBuffer(fn func() NtStatus, buf *[]byte, resultLength *uint32) NtStatus {
275275
for {
276-
if st := fn(); st == STATUS_BUFFER_OVERFLOW || st == STATUS_BUFFER_TOO_SMALL || st == STATUS_INFO_LENGTH_MISMATCH {
276+
st := fn()
277+
if st == STATUS_BUFFER_OVERFLOW || st == STATUS_BUFFER_TOO_SMALL || st == STATUS_INFO_LENGTH_MISMATCH {
277278
if int(*resultLength) <= cap(*buf) {
278279
(*reflect.SliceHeader)(unsafe.Pointer(buf)).Len = int(*resultLength)
279280
} else {
280281
*buf = make([]byte, int(*resultLength))
281282
}
282283
continue
283-
} else {
284-
if !st.IsError() {
285-
*buf = (*buf)[:int(*resultLength)]
286-
}
287-
return st
288284
}
285+
if !st.IsError() {
286+
*buf = (*buf)[:int(*resultLength)]
287+
}
288+
return st
289289
}
290290
}
291291

load/load_windows.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414

1515
var (
1616
loadErr error
17-
loadAvg1M float64 = 0.0
18-
loadAvg5M float64 = 0.0
19-
loadAvg15M float64 = 0.0
17+
loadAvg1M = 0.0
18+
loadAvg5M = 0.0
19+
loadAvg15M = 0.0
2020
loadAvgMutex sync.RWMutex
2121
loadAvgGoroutineOnce sync.Once
2222
)
@@ -27,10 +27,10 @@ var (
2727
// code https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/arch/windows/wmi.c
2828
func loadAvgGoroutine(ctx context.Context) {
2929
var (
30-
samplingFrequency time.Duration = 5 * time.Second
31-
loadAvgFactor1M float64 = 1 / math.Exp(samplingFrequency.Seconds()/time.Minute.Seconds())
32-
loadAvgFactor5M float64 = 1 / math.Exp(samplingFrequency.Seconds()/(5*time.Minute).Seconds())
33-
loadAvgFactor15M float64 = 1 / math.Exp(samplingFrequency.Seconds()/(15*time.Minute).Seconds())
30+
samplingFrequency = 5 * time.Second
31+
loadAvgFactor1M = 1 / math.Exp(samplingFrequency.Seconds()/time.Minute.Seconds())
32+
loadAvgFactor5M = 1 / math.Exp(samplingFrequency.Seconds()/(5*time.Minute).Seconds())
33+
loadAvgFactor15M = 1 / math.Exp(samplingFrequency.Seconds()/(15*time.Minute).Seconds())
3434
currentLoad float64
3535
)
3636

mem/mem_darwin_test.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,40 @@ import (
99
"testing"
1010

1111
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestVirtualMemoryDarwin(t *testing.T) {
1516
v, err := VirtualMemory()
16-
assert.Nil(t, err)
17+
require.NoError(t, err)
1718

1819
outBytes, err := invoke.Command("/usr/sbin/sysctl", "hw.memsize")
19-
assert.Nil(t, err)
20+
require.NoError(t, err)
2021
outString := string(outBytes)
2122
outString = strings.TrimSpace(outString)
2223
outParts := strings.Split(outString, " ")
2324
actualTotal, err := strconv.ParseInt(outParts[1], 10, 64)
24-
assert.Nil(t, err)
25+
require.NoError(t, err)
2526
assert.Equal(t, uint64(actualTotal), v.Total)
2627

27-
assert.True(t, v.Available > 0)
28+
assert.Positive(t, v.Available)
2829
assert.Equal(t, v.Available, v.Free+v.Inactive, "%v", v)
2930

30-
assert.True(t, v.Used > 0)
31-
assert.True(t, v.Used < v.Total)
31+
assert.Positive(t, v.Used)
32+
assert.Less(t, v.Used, v.Total)
3233

33-
assert.True(t, v.UsedPercent > 0)
34-
assert.True(t, v.UsedPercent < 100)
34+
assert.Positive(t, v.UsedPercent)
35+
assert.Less(t, v.UsedPercent, 100)
3536

36-
assert.True(t, v.Free > 0)
37-
assert.True(t, v.Free < v.Available)
37+
assert.Positive(t, v.Free)
38+
assert.Less(t, v.Free, v.Available)
3839

39-
assert.True(t, v.Active > 0)
40-
assert.True(t, v.Active < v.Total)
40+
assert.Positive(t, v.Active)
41+
assert.Less(t, v.Active, v.Total)
4142

42-
assert.True(t, v.Inactive > 0)
43-
assert.True(t, v.Inactive < v.Total)
43+
assert.Positive(t, v.Inactive)
44+
assert.Less(t, v.Inactive, v.Total)
4445

45-
assert.True(t, v.Wired > 0)
46-
assert.True(t, v.Wired < v.Total)
46+
assert.Positive(t, v.Wired)
47+
assert.Less(t, v.Wired, v.Total)
4748
}

mem/mem_windows.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"syscall"
1010
"unsafe"
1111

12-
"github.com/shirou/gopsutil/v4/internal/common"
1312
"golang.org/x/sys/windows"
13+
14+
"github.com/shirou/gopsutil/v4/internal/common"
1415
)
1516

1617
var (

net/net_darwin.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ func parseNetstatLine(line string) (stat *IOCountersStat, linkID *uint, err erro
3030

3131
if columns[0] == "Name" {
3232
err = errNetstatHeader
33-
return
33+
return //nolint:nakedret //FIXME
3434
}
3535

3636
// try to extract the numeric value from <Link#123>
3737
if subMatch := netstatLinkRegexp.FindStringSubmatch(columns[2]); len(subMatch) == 2 {
3838
numericValue, err = strconv.ParseUint(subMatch[1], 10, 64)
3939
if err != nil {
40-
return
40+
return //nolint:nakedret //FIXME
4141
}
4242
linkIDUint := uint(numericValue)
4343
linkID = &linkIDUint
@@ -51,7 +51,7 @@ func parseNetstatLine(line string) (stat *IOCountersStat, linkID *uint, err erro
5151
}
5252
if numberColumns < 11 || numberColumns > 13 {
5353
err = fmt.Errorf("Line %q do have an invalid number of columns %d", line, numberColumns)
54-
return
54+
return //nolint:nakedret //FIXME
5555
}
5656

5757
parsed := make([]uint64, 0, 7)
@@ -74,7 +74,7 @@ func parseNetstatLine(line string) (stat *IOCountersStat, linkID *uint, err erro
7474
}
7575

7676
if numericValue, err = strconv.ParseUint(target, 10, 64); err != nil {
77-
return
77+
return //nolint:nakedret //FIXME
7878
}
7979
parsed = append(parsed, numericValue)
8080
}
@@ -91,7 +91,7 @@ func parseNetstatLine(line string) (stat *IOCountersStat, linkID *uint, err erro
9191
if len(parsed) == 7 {
9292
stat.Dropout = parsed[6]
9393
}
94-
return
94+
return //nolint:nakedret //FIXME
9595
}
9696

9797
type netstatInterface struct {

net/net_darwin_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,5 @@ func TestParseNetstatTruncated(t *testing.T) {
137137

138138
mapUsage := newMapInterfaceNameUsage(nsInterfaces)
139139
assert.True(t, mapUsage.isTruncated())
140-
assert.Equal(t, 3, len(mapUsage.notTruncated()), "en0, gif0 and stf0")
140+
assert.Len(t, mapUsage.notTruncated(), 3, "en0, gif0 and stf0")
141141
}

0 commit comments

Comments
 (0)