Skip to content

Commit 98a8ecf

Browse files
committed
enable all go-critic
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 2c7ae10 commit 98a8ecf

36 files changed

+80
-71
lines changed

.golangci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ linters:
4141
gocritic:
4242
disabled-checks:
4343
- captLocal
44+
- commentedOutCode
45+
- deferInLoop
46+
- emptyFallthrough
47+
- hexLiteral
48+
- hugeParam
49+
- nestingReduce
50+
- rangeValCopy
51+
- tooManyResultsChecker
52+
- unnamedResult
53+
enable-all: true
4454
gomodguard:
4555
blocked:
4656
modules:

cpu/cpu_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func finishCPUInfo(ctx context.Context, c *InfoStat) {
135135
var err error
136136
var value float64
137137

138-
if len(c.CoreID) == 0 {
138+
if c.CoreID == "" {
139139
lines, err = common.ReadLines(sysCPUPath(ctx, c.CPU, "topology/core_id"))
140140
if err == nil {
141141
c.CoreID = lines[0]

cpu/cpu_plan9_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var timesTests = []struct {
3535
func TestTimesPlan9(t *testing.T) {
3636
for _, tt := range timesTests {
3737
t.Run(tt.mockedRootFS, func(t *testing.T) {
38-
t.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
38+
t.Setenv("HOST_ROOT", filepath.Join("testdata", "plan9", tt.mockedRootFS))
3939
stats, err := Times(false)
4040
common.SkipIfNotImplementedErr(t, err)
4141
require.NoError(t, err)

cpu/cpu_solaris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
149149
return result, nil
150150
}
151151

152-
var flagsMatch = regexp.MustCompile(`[\w\.]+`)
152+
var flagsMatch = regexp.MustCompile(`[\w.]+`)
153153

154154
func parseISAInfo(cmdOutput string) ([]string, error) {
155155
words := flagsMatch.FindAllString(cmdOutput, -1)

cpu/cpu_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package cpu
33

44
import (
5-
"fmt"
65
"os"
76
"runtime"
87
"testing"
@@ -80,7 +79,7 @@ func TestTimeStat_String(t *testing.T) {
8079
Idle: 300.1,
8180
}
8281
e := `{"cpu":"cpu0","user":100.1,"system":200.1,"idle":300.1,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0}`
83-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "CPUTimesStat string is invalid: %v", v)
82+
assert.JSONEqf(t, e, v.String(), "CPUTimesStat string is invalid: %v", v)
8483
}
8584

8685
func TestInfo(t *testing.T) {

disk/disk_freebsd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
182182
break
183183
}
184184
}
185-
if err = s.Err(); err != nil {
185+
err = s.Err()
186+
if err != nil {
186187
return "", err
187188
}
188189
return serial, nil

disk/disk_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
486486
return ret, nil
487487
}
488488

489-
func udevData(ctx context.Context, major uint32, minor uint32, name string) (string, error) {
489+
func udevData(ctx context.Context, major, minor uint32, name string) (string, error) {
490490
udevDataPath := common.HostRunWithContext(ctx, fmt.Sprintf("udev/data/b%d:%d", major, minor))
491491
if f, err := os.Open(udevDataPath); err == nil {
492492
defer f.Close()

disk/disk_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package disk
33

44
import (
5-
"fmt"
65
"runtime"
76
"sync"
87
"testing"
@@ -81,7 +80,7 @@ func TestUsageStat_String(t *testing.T) {
8180
Fstype: "ext4",
8281
}
8382
e := `{"path":"/","fstype":"ext4","total":1000,"free":2000,"used":3000,"usedPercent":50.1,"inodesTotal":4000,"inodesUsed":5000,"inodesFree":6000,"inodesUsedPercent":49.1}`
84-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "DiskUsageStat string is invalid: %v", v)
83+
assert.JSONEqf(t, e, v.String(), "DiskUsageStat string is invalid: %v", v)
8584
}
8685

8786
func TestPartitionStat_String(t *testing.T) {
@@ -92,7 +91,7 @@ func TestPartitionStat_String(t *testing.T) {
9291
Opts: []string{"ro"},
9392
}
9493
e := `{"device":"sd01","mountpoint":"/","fstype":"ext4","opts":["ro"]}`
95-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "DiskUsageStat string is invalid: %v", v)
94+
assert.JSONEqf(t, e, v.String(), "DiskUsageStat string is invalid: %v", v)
9695
}
9796

9897
func TestIOCountersStat_String(t *testing.T) {
@@ -105,5 +104,5 @@ func TestIOCountersStat_String(t *testing.T) {
105104
SerialNumber: "SERIAL",
106105
}
107106
e := `{"readCount":100,"mergedReadCount":0,"writeCount":200,"mergedWriteCount":0,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"sd01","serialNumber":"SERIAL","label":""}`
108-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "DiskUsageStat string is invalid: %v", v)
107+
assert.JSONEqf(t, e, v.String(), "DiskUsageStat string is invalid: %v", v)
109108
}

docker/docker_linux.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,26 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
8787
// containerID is same as docker id if you use docker.
8888
// If you use container via systemd.slice, you could use
8989
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
90-
func CgroupCPU(containerID string, base string) (*CgroupCPUStat, error) {
90+
func CgroupCPU(containerID, base string) (*CgroupCPUStat, error) {
9191
return CgroupCPUWithContext(context.Background(), containerID, base)
9292
}
9393

9494
// CgroupCPUUsage returns specified cgroup id CPU usage.
9595
// containerID is same as docker id if you use docker.
9696
// If you use container via systemd.slice, you could use
9797
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
98-
func CgroupCPUUsage(containerID string, base string) (float64, error) {
98+
func CgroupCPUUsage(containerID, base string) (float64, error) {
9999
return CgroupCPUUsageWithContext(context.Background(), containerID, base)
100100
}
101101

102-
func CgroupCPUWithContext(ctx context.Context, containerID string, base string) (*CgroupCPUStat, error) {
102+
func CgroupCPUWithContext(ctx context.Context, containerID, base string) (*CgroupCPUStat, error) {
103103
statfile := getCgroupFilePath(ctx, containerID, base, "cpuacct", "cpuacct.stat")
104104
lines, err := common.ReadLines(statfile)
105105
if err != nil {
106106
return nil, err
107107
}
108108
// empty containerID means all cgroup
109-
if len(containerID) == 0 {
109+
if containerID == "" {
110110
containerID = "all"
111111
}
112112

@@ -166,15 +166,15 @@ func CgroupCPUDockerUsageWithContext(ctx context.Context, containerID string) (f
166166
return CgroupCPUUsageWithContext(ctx, containerID, common.HostSysWithContext(ctx, "fs/cgroup/cpuacct/docker"))
167167
}
168168

169-
func CgroupMem(containerID string, base string) (*CgroupMemStat, error) {
169+
func CgroupMem(containerID, base string) (*CgroupMemStat, error) {
170170
return CgroupMemWithContext(context.Background(), containerID, base)
171171
}
172172

173-
func CgroupMemWithContext(ctx context.Context, containerID string, base string) (*CgroupMemStat, error) {
173+
func CgroupMemWithContext(ctx context.Context, containerID, base string) (*CgroupMemStat, error) {
174174
statfile := getCgroupFilePath(ctx, containerID, base, "memory", "memory.stat")
175175

176176
// empty containerID means all cgroup
177-
if len(containerID) == 0 {
177+
if containerID == "" {
178178
containerID = "all"
179179
}
180180
lines, err := common.ReadLines(statfile)
@@ -276,7 +276,7 @@ func CgroupMemDockerWithContext(ctx context.Context, containerID string) (*Cgrou
276276

277277
// getCgroupFilePath constructs file path to get targeted stats file.
278278
func getCgroupFilePath(ctx context.Context, containerID, base, target, file string) string {
279-
if len(base) == 0 {
279+
if base == "" {
280280
base = common.HostSysWithContext(ctx, fmt.Sprintf("fs/cgroup/%s/docker", target))
281281
}
282282
statfile := path.Join(base, containerID, file)

docker/docker_notlinux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func GetDockerIDListWithContext(_ context.Context) ([]string, error) {
3333
// containerID is same as docker id if you use docker.
3434
// If you use container via systemd.slice, you could use
3535
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
36-
func CgroupCPU(containerID string, base string) (*CgroupCPUStat, error) {
36+
func CgroupCPU(containerID, base string) (*CgroupCPUStat, error) {
3737
return CgroupCPUWithContext(context.Background(), containerID, base)
3838
}
3939

40-
func CgroupCPUWithContext(_ context.Context, _ string, _ string) (*CgroupCPUStat, error) {
40+
func CgroupCPUWithContext(_ context.Context, _, _ string) (*CgroupCPUStat, error) {
4141
return nil, ErrCgroupNotAvailable
4242
}
4343

@@ -49,11 +49,11 @@ func CgroupCPUDockerWithContext(ctx context.Context, containerID string) (*Cgrou
4949
return CgroupCPUWithContext(ctx, containerID, common.HostSysWithContext(ctx, "fs/cgroup/cpuacct/docker"))
5050
}
5151

52-
func CgroupMem(containerID string, base string) (*CgroupMemStat, error) {
52+
func CgroupMem(containerID, base string) (*CgroupMemStat, error) {
5353
return CgroupMemWithContext(context.Background(), containerID, base)
5454
}
5555

56-
func CgroupMemWithContext(_ context.Context, _ string, _ string) (*CgroupMemStat, error) {
56+
func CgroupMemWithContext(_ context.Context, _, _ string) (*CgroupMemStat, error) {
5757
return nil, ErrCgroupNotAvailable
5858
}
5959

host/host_aix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) {
160160
}
161161

162162
// Much of this function could be static. However, to be future proofed, I've made it call the OS for the information in all instances.
163-
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
163+
func PlatformInformationWithContext(ctx context.Context) (platform, family, version string, err error) {
164164
// Set the platform (which should always, and only be, "AIX") from `uname -s`
165165
out, err := invoke.CommandWithContext(ctx, "uname", "-s")
166166
if err != nil {

host/host_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func getlsbStruct(ctx context.Context) (*lsbStruct, error) {
170170
return ret, nil
171171
}
172172

173-
func PlatformInformationWithContext(ctx context.Context) (platform string, family string, version string, err error) {
173+
func PlatformInformationWithContext(ctx context.Context) (platform, family, version string, err error) {
174174
lsb, err := getlsbStruct(ctx)
175175
if err != nil {
176176
lsb = &lsbStruct{}

host/host_solaris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func numProcs(_ context.Context) (uint64, error) {
6767
return uint64(len(dirs)), nil
6868
}
6969

70-
var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`)
70+
var kstatMatch = regexp.MustCompile(`(\S+)\s+(\S*)`)
7171

7272
func BootTimeWithContext(ctx context.Context) (uint64, error) {
7373
out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "unix:0:system_misc:boot_time")

host/host_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package host
33

44
import (
5-
"fmt"
65
"os"
76
"sync"
87
"testing"
@@ -86,7 +85,7 @@ func TestInfoStat_String(t *testing.T) {
8685
KernelArch: "x86_64",
8786
}
8887
e := `{"hostname":"test","uptime":3000,"bootTime":1447040000,"procs":100,"os":"linux","platform":"ubuntu","platformFamily":"","platformVersion":"","kernelVersion":"","kernelArch":"x86_64","virtualizationSystem":"","virtualizationRole":"","hostId":"edfd25ff-3c9c-b1a4-e660-bd826495ad35"}`
89-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "HostInfoStat string is invalid:\ngot %v\nwant %v", v, e)
88+
assert.JSONEqf(t, e, v.String(), "HostInfoStat string is invalid:\ngot %v\nwant %v", v, e)
9089
}
9190

9291
func TestUserStat_String(t *testing.T) {
@@ -97,7 +96,7 @@ func TestUserStat_String(t *testing.T) {
9796
Started: 100,
9897
}
9998
e := `{"user":"user","terminal":"term","host":"host","started":100}`
100-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "UserStat string is invalid: %v", v)
99+
assert.JSONEqf(t, e, v.String(), "UserStat string is invalid: %v", v)
101100
}
102101

103102
func TestGuid(t *testing.T) {

host/host_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func BootTimeWithContext(_ context.Context) (uint64, error) {
136136
return t, nil
137137
}
138138

139-
func PlatformInformationWithContext(_ context.Context) (platform string, family string, version string, err error) {
139+
func PlatformInformationWithContext(_ context.Context) (platform, family, version string, err error) {
140140
platform, family, _, displayVersion, err := platformInformation()
141141
if err != nil {
142142
return "", "", "", err

internal/common/common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func ReadLines(filename string) ([]string, error) {
115115
}
116116

117117
// ReadLine reads a file and returns the first occurrence of a line that is prefixed with prefix.
118-
func ReadLine(filename string, prefix string) (string, error) {
118+
func ReadLine(filename, prefix string) (string, error) {
119119
f, err := os.Open(filename)
120120
if err != nil {
121121
return "", err
@@ -156,7 +156,7 @@ func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
156156
for i := uint(0); i < uint(n)+offset || n < 0; i++ {
157157
line, err := r.ReadString('\n')
158158
if err != nil {
159-
if err == io.EOF && len(line) > 0 {
159+
if err == io.EOF && line != "" {
160160
ret = append(ret, strings.Trim(line, "\n"))
161161
}
162162
break
@@ -349,7 +349,7 @@ func PathExistsWithContents(filename string) bool {
349349

350350
// GetEnvWithContext retrieves the environment variable key. If it does not exist it returns the default.
351351
// The context may optionally contain a map superseding os.EnvKey.
352-
func GetEnvWithContext(ctx context.Context, key string, dfault string, combineWith ...string) string {
352+
func GetEnvWithContext(ctx context.Context, key, dfault string, combineWith ...string) string {
353353
var value string
354354
if env, ok := ctx.Value(common.EnvKey).(common.EnvMap); ok {
355355
value = env[common.EnvKeyType(key)]
@@ -365,7 +365,7 @@ func GetEnvWithContext(ctx context.Context, key string, dfault string, combineWi
365365
}
366366

367367
// GetEnv retrieves the environment variable key. If it does not exist it returns the default.
368-
func GetEnv(key string, dfault string, combineWith ...string) string {
368+
func GetEnv(key, dfault string, combineWith ...string) string {
369369
value := os.Getenv(key)
370370
if value == "" {
371371
value = dfault

internal/common/common_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,11 @@ func VirtualizationWithContext(ctx context.Context) (string, string, error) {
317317
return system, role, nil
318318
}
319319

320-
func GetOSRelease() (platform string, version string, err error) {
320+
func GetOSRelease() (platform, version string, err error) {
321321
return GetOSReleaseWithContext(context.Background())
322322
}
323323

324-
func GetOSReleaseWithContext(ctx context.Context) (platform string, version string, err error) {
324+
func GetOSReleaseWithContext(ctx context.Context) (platform, version string, err error) {
325325
contents, err := ReadLines(HostEtcWithContext(ctx, "os-release"))
326326
if err != nil {
327327
return "", "", nil // return empty

internal/common/common_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func CallLsofWithContext(ctx context.Context, invoke Invoker, pid int32, args ..
3333

3434
var ret []string
3535
for _, l := range lines[1:] {
36-
if len(l) == 0 {
36+
if l == "" {
3737
continue
3838
}
3939
ret = append(ret, l)

load/load_aix_nocgo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package load
55

66
import (
7+
"bytes"
78
"context"
89
"regexp"
910
"strconv"
@@ -20,7 +21,7 @@ func AvgWithContext(ctx context.Context) (*AvgStat, error) {
2021
return nil, err
2122
}
2223

23-
idx := strings.Index(string(line), "load average:")
24+
idx := bytes.Index(line, []byte("load average:"))
2425
if idx < 0 {
2526
return nil, common.ErrNotImplementedError
2627
}

load/load_solaris.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func AvgWithContext(ctx context.Context) (*AvgStat, error) {
4545
}
4646
*tgt = float64(v) / (1 << 8)
4747
}
48-
if err = scanner.Err(); err != nil {
48+
err = scanner.Err()
49+
if err != nil {
4950
return nil, err
5051
}
5152

load/load_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package load
33

44
import (
5-
"fmt"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
@@ -28,7 +27,7 @@ func TestAvgStat_String(t *testing.T) {
2827
Load15: 30.1,
2928
}
3029
e := `{"load1":10.1,"load5":20.1,"load15":30.1}`
31-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "LoadAvgStat string is invalid: %v", v)
30+
assert.JSONEqf(t, e, v.String(), "LoadAvgStat string is invalid: %v", v)
3231
t.Log(e)
3332
}
3433

@@ -51,7 +50,7 @@ func TestMiscStatString(t *testing.T) {
5150
Ctxt: 3,
5251
}
5352
e := `{"procsTotal":4,"procsCreated":5,"procsRunning":1,"procsBlocked":2,"ctxt":3}`
54-
assert.JSONEqf(t, e, fmt.Sprintf("%v", v), "TestMiscString string is invalid: %v", v)
53+
assert.JSONEqf(t, e, v.String(), "TestMiscString string is invalid: %v", v)
5554
t.Log(e)
5655
}
5756

mem/mem_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ var virtualMemoryTests = []struct {
126126
func TestVirtualMemoryLinux(t *testing.T) {
127127
for _, tt := range virtualMemoryTests {
128128
t.Run(tt.mockedRootFS, func(t *testing.T) {
129-
t.Setenv("HOST_PROC", filepath.Join("testdata/linux/virtualmemory/", tt.mockedRootFS, "proc"))
129+
t.Setenv("HOST_PROC", filepath.Join("testdata", "linux", "virtualmemory", tt.mockedRootFS, "proc"))
130130

131131
stat, err := VirtualMemory()
132132
common.SkipIfNotImplementedErr(t, err)

0 commit comments

Comments
 (0)