Skip to content
15 changes: 15 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ linters:
enable:
- errorlint
- forbidigo
- gocritic
- godot
- misspell
- revive
Expand All @@ -12,6 +13,20 @@ linters:
forbid:
- pattern: ^fmt\.Print.*$
msg: Do not commit print statements.
gocritic:
enable-all: true
disabled-checks:
- commentFormatting
- commentedOutCode
- deferInLoop
- filepathJoin
- hugeParam
- importShadow
- paramTypeCombine
- rangeValCopy
- tooManyResultsChecker
- unnamedResult
- whyNoLint
godot:
exclude:
# Ignore "See: URL".
Expand Down
7 changes: 4 additions & 3 deletions arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ func parseARPEntries(data []byte) ([]ARPEntry, error) {
columns := strings.Fields(line)
width := len(columns)

if width == expectedHeaderWidth || width == 0 {
switch width {
case expectedHeaderWidth, 0:
continue
} else if width == expectedDataWidth {
case expectedDataWidth:
entry, err := parseARPEntry(columns)
if err != nil {
return []ARPEntry{}, fmt.Errorf("%w: Failed to parse ARP entry: %v: %w", ErrFileParse, entry, err)
}
entries = append(entries, entry)
} else {
default:
return []ARPEntry{}, fmt.Errorf("%w: %d columns found, but expected %d: %w", ErrFileParse, width, expectedDataWidth, err)
}

Expand Down
2 changes: 1 addition & 1 deletion bcache/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func parsePseudoFloat(str string) (float64, error) {
// v4.12-rc3).

// Restore the proper order:
fracPart = fracPart / 10.24
fracPart /= 10.24
return intPart + fracPart, nil
}

Expand Down
6 changes: 2 additions & 4 deletions buddyinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ func parseBuddyInfo(r io.Reader) ([]BuddyInfo, error) {

if bucketCount == -1 {
bucketCount = arraySize
} else {
if bucketCount != arraySize {
return nil, fmt.Errorf("%w: mismatch in number of buddyinfo buckets, previous count %d, new count %d", ErrFileParse, bucketCount, arraySize)
}
} else if bucketCount != arraySize {
return nil, fmt.Errorf("%w: mismatch in number of buddyinfo buckets, previous count %d, new count %d", ErrFileParse, bucketCount, arraySize)
}

sizes := make([]float64, arraySize)
Expand Down
7 changes: 4 additions & 3 deletions fscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,21 @@ func parseFscacheinfo(r io.Reader) (*Fscacheinfo, error) {
}
}
case "CacheOp:":
if strings.Split(fields[1], "=")[0] == "alo" {
switch strings.Split(fields[1], "=")[0] {
case "alo":
err := setFSCacheFields(fields[1:], &m.CacheopAllocationsinProgress, &m.CacheopLookupObjectInProgress,
&m.CacheopLookupCompleteInPorgress, &m.CacheopGrabObjectInProgress)
if err != nil {
return &m, err
}
} else if strings.Split(fields[1], "=")[0] == "inv" {
case "inv":
err := setFSCacheFields(fields[1:], &m.CacheopInvalidations, &m.CacheopUpdateObjectInProgress,
&m.CacheopDropObjectInProgress, &m.CacheopPutObjectInProgress, &m.CacheopAttributeChangeInProgress,
&m.CacheopSyncCacheInProgress)
if err != nil {
return &m, err
}
} else {
default:
err := setFSCacheFields(fields[1:], &m.CacheopReadOrAllocPageInProgress, &m.CacheopReadOrAllocPagesInProgress,
&m.CacheopAllocatePageInProgress, &m.CacheopAllocatePagesInProgress, &m.CacheopWritePagesInProgress,
&m.CacheopUncachePagesInProgress, &m.CacheopDissociatePagesInProgress)
Expand Down
4 changes: 2 additions & 2 deletions iscsi/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (fs FS) GetRBDMatch(rbdNumber string, poolImage string) (*RBD, error) {
}
systemImage = strings.TrimSpace(string(bSystemImage))

if strings.Compare(strconv.FormatInt(int64(systemRbdNumber), 10), rbdNumber) == 0 &&
if strconv.FormatInt(int64(systemRbdNumber), 10) == rbdNumber &&
matchPoolImage(systemPool, systemImage, poolImage) {
rbd.Pool = systemPool
rbd.Image = systemImage
Expand Down Expand Up @@ -241,5 +241,5 @@ func (fs FS) GetRDMCPPath(rdmcpNumber string, objectName string) (*RDMCP, error)

func matchPoolImage(pool string, image string, matchPoolImage string) (isEqual bool) {
var poolImage = fmt.Sprintf("%s-%s", pool, image)
return strings.Compare(poolImage, matchPoolImage) == 0
return poolImage == matchPoolImage
}
9 changes: 5 additions & 4 deletions mdstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,14 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {

// Append recovery and resyncing state info.
if recovering || resyncing || checking || reshaping {
if recovering {
switch {
case recovering:
state = "recovering"
} else if reshaping {
case reshaping:
state = "reshaping"
} else if checking {
case checking:
state = "checking"
} else {
default:
state = "resyncing"
}

Expand Down
6 changes: 3 additions & 3 deletions proc_cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ type CgroupSummary struct {

// parseCgroupSummary parses each line of the /proc/cgroup file
// Line format is `subsys_name hierarchy num_cgroups enabled`.
func parseCgroupSummaryString(CgroupSummaryStr string) (*CgroupSummary, error) {
func parseCgroupSummaryString(cgroupSummaryStr string) (*CgroupSummary, error) {
var err error

fields := strings.Fields(CgroupSummaryStr)
fields := strings.Fields(cgroupSummaryStr)
// require at least 4 fields
if len(fields) < 4 {
return nil, fmt.Errorf("%w: 4+ fields required, found %d fields in cgroup info string: %s", ErrFileParse, len(fields), CgroupSummaryStr)
return nil, fmt.Errorf("%w: 4+ fields required, found %d fields in cgroup info string: %s", ErrFileParse, len(fields), cgroupSummaryStr)
}

CgroupSummary := &CgroupSummary{
Expand Down
11 changes: 6 additions & 5 deletions proc_fdinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ func (p Proc) FDInfo(fd string) (*ProcFDInfo, error) {
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
text = scanner.Text()
if rPos.MatchString(text) {
switch {
case rPos.MatchString(text):
pos = rPos.FindStringSubmatch(text)[1]
} else if rFlags.MatchString(text) {
case rFlags.MatchString(text):
flags = rFlags.FindStringSubmatch(text)[1]
} else if rMntID.MatchString(text) {
case rMntID.MatchString(text):
mntid = rMntID.FindStringSubmatch(text)[1]
} else if rIno.MatchString(text) {
case rIno.MatchString(text):
ino = rIno.FindStringSubmatch(text)[1]
} else if rInotify.MatchString(text) {
case rInotify.MatchString(text):
newInotify, err := parseInotifyInfo(text)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion proc_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const (
)

var (
limitsMatch = regexp.MustCompile(`(Max \w+\s{0,1}?\w*\s{0,1}\w*)\s{2,}(\w+)\s+(\w+)`)
limitsMatch = regexp.MustCompile(`(Max \w+\s??\w*\s?\w*)\s{2,}(\w+)\s+(\w+)`)
)

// NewLimits returns the current soft limits of the process.
Expand Down
2 changes: 1 addition & 1 deletion sysfs/class_sas_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type SASDeviceClass map[string]*SASDevice

var (
sasTargetDeviceRegexp = regexp.MustCompile(`^target[0-9:]+$`)
sasTargetSubDeviceRegexp = regexp.MustCompile(`[0-9]+:.*`)
sasTargetSubDeviceRegexp = regexp.MustCompile(`\d+:.*`)
)

// sasDeviceClasses reads all of the SAS devices from a specific set
Expand Down
4 changes: 2 additions & 2 deletions sysfs/class_scsitape.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func (fs FS) SCSITapeClass() (SCSITapeClass, error) {
// There are n?st[0-9]+[a-b]? variants depending on device features.
// n/2 is probably overestimated but never underestimated
stc := make(SCSITapeClass, len(dirs)/2)
validDevice := regexp.MustCompile(`^st[0-9]+$`)
validDevice := regexp.MustCompile(`^st\d+$`)

for _, d := range dirs {
if !validDevice.Match([]byte(d.Name())) {
if !validDevice.MatchString(d.Name()) {
continue
}
tape, err := fs.parseSCSITape(d.Name())
Expand Down
7 changes: 4 additions & 3 deletions sysfs/class_thermal.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ func parseClassThermalZone(zone string) (ClassThermalZoneStats, error) {

var zonePassive *uint64
passive, err := util.SysReadUintFromFile(filepath.Join(zone, "passive"))
if os.IsNotExist(err) || os.IsPermission(err) {
switch {
case os.IsNotExist(err), os.IsPermission(err):
zonePassive = nil
} else if err != nil {
case err != nil:
return ClassThermalZoneStats{}, err
} else {
default:
zonePassive = &passive
}

Expand Down
11 changes: 6 additions & 5 deletions sysfs/net_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,16 @@ func (fs FS) NetClass() (NetClass, error) {
func canIgnoreError(err error) bool {
var errno syscall.Errno

if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return true
} else if os.IsPermission(err) {
case os.IsPermission(err):
return true
} else if err.Error() == "operation not supported" {
case err.Error() == "operation not supported":
return true
} else if errors.Is(err, os.ErrInvalid) {
case errors.Is(err, os.ErrInvalid):
return true
} else if errors.As(err, &errno) && (errno == syscall.EINVAL) {
case errors.As(err, &errno) && (errno == syscall.EINVAL):
return true
}
// all other errors are fatal
Expand Down
7 changes: 4 additions & 3 deletions sysfs/net_class_ecn.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ func ParseRoceRpEcnAttribute(ecnPath string, attrName string, ecn *RoceRpEcn) er
vp := util.NewValueParser(value)
switch attrName {
case "clamp_tgt_rate":
if *vp.PUInt64() == 0 {
switch *vp.PUInt64() {
case 0:
ecn.ClampTgtRate = false
} else if *vp.PUInt64() == 1 {
case 1:
ecn.ClampTgtRate = true
} else {
default:
return fmt.Errorf("failed to parse file %q: %w", attrPath, err)
}
case "dce_tcp_g":
Expand Down
12 changes: 7 additions & 5 deletions sysfs/system_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ func binSearch(elem uint16, elemSlice *[]uint16) bool {

for start <= end {
mid = (start + end) / 2
if (*elemSlice)[mid] == elem {
switch {
case (*elemSlice)[mid] == elem:
return true
} else if (*elemSlice)[mid] > elem {
case (*elemSlice)[mid] > elem:
end = mid - 1
} else if (*elemSlice)[mid] < elem {
case (*elemSlice)[mid] < elem:
start = mid + 1
}
}
Expand Down Expand Up @@ -254,7 +255,8 @@ func (fs FS) SystemCpufreq() ([]SystemCPUCpufreqStats, error) {
})
}

if err = g.Wait(); err != nil {
err = g.Wait()
if err != nil {
return nil, err
}

Expand Down Expand Up @@ -364,7 +366,7 @@ func parseCpufreqCpuinfo(cpuPath string) (*SystemCPUCpufreqStats, error) {
fields[0] = strings.TrimSuffix(fields[0], ":")
cpuinfoTransitionTableRow := make([]uint64, len(fields))
for i := range fields {
if len(fields[i]) == 0 {
if fields[i] == "" {
continue
}
f, err := strconv.ParseUint(fields[i], 10, 64)
Expand Down
2 changes: 1 addition & 1 deletion sysfs/vmstat_numa.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

var (
nodePattern = "devices/system/node/node[0-9]*"
nodeNumberRegexp = regexp.MustCompile(`.*devices/system/node/node([0-9]*)`)
nodeNumberRegexp = regexp.MustCompile(`.*devices/system/node/node(\d*)`)
)

type VMStat struct {
Expand Down