Skip to content

Commit 3badc2b

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

File tree

12 files changed

+55
-43
lines changed

12 files changed

+55
-43
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"fmt"
99
"unsafe"
1010

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

1617
var procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")

disk/disk_windows.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ package disk
66
import (
77
"bytes"
88
"context"
9+
"errors"
910
"fmt"
1011
"syscall"
1112
"unsafe"
1213

13-
"github.com/shirou/gopsutil/v4/internal/common"
1414
"golang.org/x/sys/windows"
1515
"golang.org/x/sys/windows/registry"
16+
17+
"github.com/shirou/gopsutil/v4/internal/common"
1618
)
1719

1820
var (
@@ -206,7 +208,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
206208
const IOCTL_DISK_PERFORMANCE = 0x70020
207209
h, err := windows.CreateFile(syscall.StringToUTF16Ptr(szDevice), 0, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
208210
if err != nil {
209-
if err == windows.ERROR_FILE_NOT_FOUND {
211+
if errors.Is(err, windows.ERROR_FILE_NOT_FOUND) {
210212
continue
211213
}
212214
return drivemap, err

host/host_windows.go

+3-2
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

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_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_windows.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ package net
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"net"
1011
"os"
1112
"syscall"
1213
"unsafe"
1314

14-
"github.com/shirou/gopsutil/v4/internal/common"
1515
"golang.org/x/sys/windows"
16+
17+
"github.com/shirou/gopsutil/v4/internal/common"
1618
)
1719

1820
var (
@@ -197,7 +199,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
197199
}
198200

199201
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
200-
return IOCounters(pernic)
202+
return IOCounters(pernic) //nolint:contextcheck //FIXME
201203
}
202204

203205
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
@@ -390,7 +392,7 @@ func getTCPConnections(family uint32) ([]ConnectionStat, error) {
390392
if err == nil {
391393
break
392394
}
393-
if err != windows.ERROR_INSUFFICIENT_BUFFER {
395+
if !errors.Is(err, windows.ERROR_INSUFFICIENT_BUFFER) {
394396
return nil, err
395397
}
396398
buf = make([]byte, size)
@@ -473,7 +475,7 @@ func getUDPConnections(family uint32) ([]ConnectionStat, error) {
473475
if err == nil {
474476
break
475477
}
476-
if err != windows.ERROR_INSUFFICIENT_BUFFER {
478+
if !errors.Is(err, windows.ERROR_INSUFFICIENT_BUFFER) {
477479
return nil, err
478480
}
479481
buf = make([]byte, size)

process/process_windows.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import (
1818
"unicode/utf16"
1919
"unsafe"
2020

21+
"golang.org/x/sys/windows"
22+
2123
"github.com/shirou/gopsutil/v4/cpu"
2224
"github.com/shirou/gopsutil/v4/internal/common"
2325
"github.com/shirou/gopsutil/v4/net"
24-
"golang.org/x/sys/windows"
2526
)
2627

2728
type Signal = syscall.Signal
@@ -245,7 +246,7 @@ func pidsWithContext(ctx context.Context) ([]int32, error) {
245246
// inspired by https://gist.github.com/henkman/3083408
246247
// and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329
247248
var ret []int32
248-
var read uint32 = 0
249+
var read uint32
249250
var psSize uint32 = 1024
250251
const dwordSize uint32 = 4
251252

@@ -288,10 +289,10 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
288289
return false, err
289290
}
290291
h, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
291-
if err == windows.ERROR_ACCESS_DENIED {
292+
if errors.Is(err, windows.ERROR_ACCESS_DENIED) {
292293
return true, nil
293294
}
294-
if err == windows.ERROR_INVALID_PARAMETER {
295+
if errors.Is(err, windows.ERROR_INVALID_PARAMETER) {
295296
return false, nil
296297
}
297298
if err != nil {
@@ -330,7 +331,7 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
330331

331332
exe, err := p.ExeWithContext(ctx)
332333
if err != nil {
333-
return "", fmt.Errorf("could not get Name: %s", err)
334+
return "", fmt.Errorf("could not get Name: %w", err)
334335
}
335336

336337
return filepath.Base(exe), nil
@@ -370,7 +371,7 @@ func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
370371
func (p *Process) CmdlineWithContext(_ context.Context) (string, error) {
371372
cmdline, err := getProcessCommandLine(p.Pid)
372373
if err != nil {
373-
return "", fmt.Errorf("could not get CommandLine: %s", err)
374+
return "", fmt.Errorf("could not get CommandLine: %w", err)
374375
}
375376
return cmdline, nil
376377
}
@@ -386,15 +387,15 @@ func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error)
386387
func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
387388
ru, err := getRusage(p.Pid)
388389
if err != nil {
389-
return 0, fmt.Errorf("could not get CreationDate: %s", err)
390+
return 0, fmt.Errorf("could not get CreationDate: %w", err)
390391
}
391392

392393
return ru.CreationTime.Nanoseconds() / 1000000, nil
393394
}
394395

395396
func (p *Process) CwdWithContext(_ context.Context) (string, error) {
396397
h, err := windows.OpenProcess(processQueryInformation|windows.PROCESS_VM_READ, false, uint32(p.Pid))
397-
if err == windows.ERROR_ACCESS_DENIED || err == windows.ERROR_INVALID_PARAMETER {
398+
if errors.Is(err, windows.ERROR_ACCESS_DENIED) || errors.Is(err, windows.ERROR_INVALID_PARAMETER) {
398399
return "", nil
399400
}
400401
if err != nil {
@@ -822,9 +823,9 @@ func (p *Process) KillWithContext(ctx context.Context) error {
822823
}
823824

824825
func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) {
825-
envVars, err := getProcessEnvironmentVariables(p.Pid, ctx)
826+
envVars, err := getProcessEnvironmentVariables(ctx, p.Pid)
826827
if err != nil {
827-
return nil, fmt.Errorf("could not get environment variables: %s", err)
828+
return nil, fmt.Errorf("could not get environment variables: %w", err)
828829
}
829830
return envVars, nil
830831
}
@@ -872,7 +873,7 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
872873

873874
pids, err := PidsWithContext(ctx)
874875
if err != nil {
875-
return out, fmt.Errorf("could not get Processes %s", err)
876+
return out, fmt.Errorf("could not get Processes %w", err)
876877
}
877878

878879
for _, pid := range pids {
@@ -1038,9 +1039,9 @@ func is32BitProcess(h windows.Handle) bool {
10381039
return procIs32Bits
10391040
}
10401041

1041-
func getProcessEnvironmentVariables(pid int32, ctx context.Context) ([]string, error) {
1042+
func getProcessEnvironmentVariables(ctx context.Context, pid int32) ([]string, error) {
10421043
h, err := windows.OpenProcess(processQueryInformation|windows.PROCESS_VM_READ, false, uint32(pid))
1043-
if err == windows.ERROR_ACCESS_DENIED || err == windows.ERROR_INVALID_PARAMETER {
1044+
if errors.Is(err, windows.ERROR_ACCESS_DENIED) || errors.Is(err, windows.ERROR_INVALID_PARAMETER) {
10441045
return nil, nil
10451046
}
10461047
if err != nil {
@@ -1124,7 +1125,7 @@ func (p *processReader) Read(buf []byte) (int, error) {
11241125

11251126
func getProcessCommandLine(pid int32) (string, error) {
11261127
h, err := windows.OpenProcess(processQueryInformation|windows.PROCESS_VM_READ, false, uint32(pid))
1127-
if err == windows.ERROR_ACCESS_DENIED || err == windows.ERROR_INVALID_PARAMETER {
1128+
if errors.Is(err, windows.ERROR_ACCESS_DENIED) || errors.Is(err, windows.ERROR_INVALID_PARAMETER) {
11281129
return "", nil
11291130
}
11301131
if err != nil {

process/process_windows_64bit.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, er
3838
)
3939
if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
4040
return uint64(wow64), nil
41-
} else {
42-
return 0, windows.NTStatus(ret)
4341
}
42+
return 0, windows.NTStatus(ret)
4443
} else {
4544
// we are on a 64-bit process reading an external 64-bit process
4645
var info processBasicInformation64
@@ -54,9 +53,8 @@ func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, er
5453
)
5554
if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
5655
return info.PebBaseAddress, nil
57-
} else {
58-
return 0, windows.NTStatus(ret)
5956
}
57+
return 0, windows.NTStatus(ret)
6058
}
6159
}
6260

winservices/winservices.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package winservices
55

66
import (
77
"context"
8+
"errors"
89
"unsafe"
910

1011
"golang.org/x/sys/windows"
@@ -86,7 +87,7 @@ func (s *Service) QueryStatusWithContext(ctx context.Context) (ServiceStatus, er
8687
var bytesNeeded uint32
8788
var buf []byte
8889

89-
if err := windows.QueryServiceStatusEx(s.srv.Handle, windows.SC_STATUS_PROCESS_INFO, nil, 0, &bytesNeeded); err != windows.ERROR_INSUFFICIENT_BUFFER {
90+
if err := windows.QueryServiceStatusEx(s.srv.Handle, windows.SC_STATUS_PROCESS_INFO, nil, 0, &bytesNeeded); !errors.Is(err, windows.ERROR_INSUFFICIENT_BUFFER) {
9091
return ServiceStatus{}, err
9192
}
9293

0 commit comments

Comments
 (0)