From a122a15f3fd5947081b165c00e6e0175d11913fa Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Mon, 9 Dec 2024 20:00:02 +0200 Subject: [PATCH] sysfs: golangci-lint fixes. Signed-off-by: Krisztian Litkey --- pkg/sysfs/parsers.go | 38 ++++++++-------- pkg/sysfs/system.go | 13 +++--- pkg/sysfs/utils.go | 100 +++++++++++++++++++++---------------------- 3 files changed, 77 insertions(+), 74 deletions(-) diff --git a/pkg/sysfs/parsers.go b/pkg/sysfs/parsers.go index 74b2c72a5..2c127fecb 100644 --- a/pkg/sysfs/parsers.go +++ b/pkg/sysfs/parsers.go @@ -15,7 +15,7 @@ package sysfs import ( - "io/ioutil" + "os" "strconv" "strings" ) @@ -70,43 +70,43 @@ func parseNumeric(path, value string, ptr interface{}) error { return err } - switch ptr.(type) { + switch ptr := ptr.(type) { case *int: num, err = strconv.ParseInt(numstr, 0, strconv.IntSize) - *ptr.(*int) = int(num * unit) + *ptr = int(num * unit) case *int8: num, err = strconv.ParseInt(numstr, 0, 8) - *ptr.(*int8) = int8(num * unit) + *ptr = int8(num * unit) case *int16: num, err = strconv.ParseInt(numstr, 0, 16) - *ptr.(*int16) = int16(num * unit) + *ptr = int16(num * unit) case *int32: num, err = strconv.ParseInt(numstr, 0, 32) - *ptr.(*int32) = int32(num * unit) + *ptr = int32(num * unit) case *int64: num, err = strconv.ParseInt(numstr, 0, 64) - *ptr.(*int64) = int64(num * unit) + *ptr = int64(num * unit) case *uint: num, err = strconv.ParseInt(numstr, 0, strconv.IntSize) - *ptr.(*uint) = uint(num * unit) + *ptr = uint(num * unit) case *uint8: num, err = strconv.ParseInt(numstr, 0, 8) - *ptr.(*uint8) = uint8(num * unit) + *ptr = uint8(num * unit) case *uint16: num, err = strconv.ParseInt(numstr, 0, 16) - *ptr.(*uint16) = uint16(num * unit) + *ptr = uint16(num * unit) case *uint32: num, err = strconv.ParseInt(numstr, 0, 32) - *ptr.(*uint32) = uint32(num * unit) + *ptr = uint32(num * unit) case *uint64: num, err = strconv.ParseInt(numstr, 0, 64) - *ptr.(*uint64) = uint64(num * unit) + *ptr = uint64(num * unit) case *float32: f, err = strconv.ParseFloat(numstr, 32) - *ptr.(*float32) = float32(f) * float32(unit) + *ptr = float32(f) * float32(unit) case *float64: f, err = strconv.ParseFloat(numstr, 64) - *ptr.(*float64) = f * float64(unit) + *ptr = f * float64(unit) default: err = sysfsError(path, "can't parse numeric value '%s' into type %T", value, ptr) @@ -119,9 +119,9 @@ func parseNumeric(path, value string, ptr interface{}) error { func ParseFileEntries(path string, values map[string]interface{}, pickFn PickEntryFn) error { var err error - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { - sysfsError(path, "failed to read file: %v", err) + return sysfsError(path, "failed to read file: %v", err) } left := len(values) @@ -136,7 +136,7 @@ func ParseFileEntries(path string, values map[string]interface{}, pickFn PickEnt continue } - switch ptr.(type) { + switch ptr := ptr.(type) { case *int, *int8, *int32, *int16, *int64, *uint, *uint8, *uint16, *uint32, *uint64: if err = parseNumeric(path, value, ptr); err != nil { return err @@ -146,9 +146,9 @@ func ParseFileEntries(path string, values map[string]interface{}, pickFn PickEnt return err } case *string: - *ptr.(*string) = value + *ptr = value case *bool: - *ptr.(*bool), err = strconv.ParseBool(value) + *ptr, err = strconv.ParseBool(value) if err != nil { return sysfsError(path, "failed to parse line %s, value '%s' for boolean key '%s'", line, value, key) diff --git a/pkg/sysfs/system.go b/pkg/sysfs/system.go index 7381a6460..7bb5b6e89 100644 --- a/pkg/sysfs/system.go +++ b/pkg/sysfs/system.go @@ -243,9 +243,8 @@ type cpu struct { // CPUFreq is a CPU frequency scaling range type CPUFreq struct { - min uint64 // minimum frequency (kHz) - max uint64 // maximum frequency (kHz) - all []uint64 // discrete set of frequencies if applicable/known + min uint64 // minimum frequency (kHz) + max uint64 // maximum frequency (kHz) } // EPP represents the value of a CPU energy performance profile @@ -926,8 +925,12 @@ func (sys *system) discoverCPU(path string) error { if _, err := readSysfsEntry(path, "topology/physical_package_id", &cpu.pkg); err != nil { return err } - readSysfsEntry(path, "topology/die_id", &cpu.die) - readSysfsEntry(path, "topology/cluster_id", &cpu.cluster) + if _, err := readSysfsEntry(path, "topology/die_id", &cpu.die); err != nil { + log.Warnf("failed to read die ID for CPU %d: %v", cpu.id, err) + } + if _, err := readSysfsEntry(path, "topology/cluster_id", &cpu.cluster); err != nil { + log.Warnf("failed to read cluster ID for CPU %d: %v", cpu.id, err) + } if _, err := readSysfsEntry(path, "topology/core_id", &cpu.core); err != nil { return err } diff --git a/pkg/sysfs/utils.go b/pkg/sysfs/utils.go index 754b9e843..377fb81f2 100644 --- a/pkg/sysfs/utils.go +++ b/pkg/sysfs/utils.go @@ -16,7 +16,6 @@ package sysfs import ( "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -54,7 +53,7 @@ func readSysfsEntry(base, entry string, ptr interface{}, args ...interface{}) (s path := filepath.Join(base, entry) - blob, err := ioutil.ReadFile(path) + blob, err := os.ReadFile(path) if err != nil { return "", sysfsError(path, "failed to read sysfs entry: %w", err) } @@ -64,7 +63,7 @@ func readSysfsEntry(base, entry string, ptr interface{}, args ...interface{}) (s return buf, nil } - switch ptr.(type) { + switch ptr := ptr.(type) { case *string, *int, *uint, *int8, *uint8, *int16, *uint16, *int32, *uint32, *int64, *uint64: err := parseValue(buf, ptr) if err != nil { @@ -83,7 +82,7 @@ func readSysfsEntry(base, entry string, ptr interface{}, args ...interface{}) (s } return buf, nil case *EPP: - *ptr.(*EPP) = EPPFromString(buf) + *ptr = EPPFromString(buf) return buf, nil } @@ -151,9 +150,9 @@ func getSeparator(defaultVal string, args []interface{}) (string, error) { // Parse a value from a string. func parseValue(str string, value interface{}) error { - switch value.(type) { + switch value := value.(type) { case *string: - *value.(*string) = str + *value = str case *int, *int8, *int16, *int32, *int64: v, err := strconv.ParseInt(str, 0, 0) @@ -161,17 +160,17 @@ func parseValue(str string, value interface{}) error { return fmt.Errorf("invalid entry '%s': %w", str, err) } - switch value.(type) { + switch value := value.(type) { case *int: - *value.(*int) = int(v) + *value = int(v) case *int8: - *value.(*int8) = int8(v) + *value = int8(v) case *int16: - *value.(*int16) = int16(v) + *value = int16(v) case *int32: - *value.(*int32) = int32(v) - case int64: - *value.(*int64) = v + *value = int32(v) + case *int64: + *value = v } case *uint, *uint8, *uint16, *uint32, *uint64: @@ -180,17 +179,17 @@ func parseValue(str string, value interface{}) error { return fmt.Errorf("invalid entry: '%s': %w", str, err) } - switch value.(type) { + switch value := value.(type) { case *uint: - *value.(*uint) = uint(v) + *value = uint(v) case *uint8: - *value.(*uint8) = uint8(v) + *value = uint8(v) case *uint16: - *value.(*uint16) = uint16(v) + *value = uint16(v) case *uint32: - *value.(*uint32) = uint32(v) + *value = uint32(v) case *uint64: - *value.(*uint64) = v + *value = v } } @@ -232,14 +231,14 @@ func parseValueList(str, sep string, valuep interface{}) error { if s == "" { break } - switch value.(type) { + switch val := value.(type) { case idset.IDSet: if rng := strings.Split(s, "-"); len(rng) == 1 { id, err := strconv.Atoi(s) if err != nil { return fmt.Errorf("invalid entry '%s': %w", s, err) } - value.(idset.IDSet).Add(idset.ID(id)) + val.Add(idset.ID(id)) } else { beg, err := strconv.Atoi(rng[0]) if err != nil { @@ -250,26 +249,27 @@ func parseValueList(str, sep string, valuep interface{}) error { return fmt.Errorf("invalid entry '%s': %w", s, err) } for id := beg; id <= end; id++ { - value.(idset.IDSet).Add(idset.ID(id)) + val.Add(idset.ID(id)) } } + value = val case []int, []int8, []int16, []int32, []int64: v, err := strconv.ParseInt(s, 0, 0) if err != nil { return fmt.Errorf("invalid entry '%s': %w", s, err) } - switch value.(type) { + switch val := value.(type) { case []int: - value = append(value.([]int), int(v)) + value = append(val, int(v)) case []int8: - value = append(value.([]int8), int8(v)) + value = append(val, int8(v)) case []int16: - value = append(value.([]int16), int16(v)) + value = append(val, int16(v)) case []int32: - value = append(value.([]int32), int32(v)) + value = append(val, int32(v)) case []int64: - value = append(value.([]int64), v) + value = append(val, v) } case []uint, []uint8, []uint16, []uint32, []uint64: @@ -277,44 +277,44 @@ func parseValueList(str, sep string, valuep interface{}) error { if err != nil { return fmt.Errorf("invalid entry '%s': %w", s, err) } - switch value.(type) { + switch val := value.(type) { case []uint: - value = append(value.([]uint), uint(v)) + value = append(val, uint(v)) case []uint8: - value = append(value.([]uint8), uint8(v)) + value = append(val, uint8(v)) case []uint16: - value = append(value.([]uint16), uint16(v)) + value = append(val, uint16(v)) case []uint32: - value = append(value.([]uint32), uint32(v)) + value = append(val, uint32(v)) case []uint64: - value = append(value.([]uint64), v) + value = append(val, v) } } } - switch valuep.(type) { + switch valuep := valuep.(type) { case *idset.IDSet: - *valuep.(*idset.IDSet) = value.(idset.IDSet) + *valuep = value.(idset.IDSet) case *[]int: - *valuep.(*[]int) = value.([]int) + *valuep = value.([]int) case *[]uint: - *valuep.(*[]uint) = value.([]uint) + *valuep = value.([]uint) case *[]int8: - *valuep.(*[]int8) = value.([]int8) + *valuep = value.([]int8) case *[]uint8: - *valuep.(*[]uint8) = value.([]uint8) + *valuep = value.([]uint8) case *[]int16: - *valuep.(*[]int16) = value.([]int16) + *valuep = value.([]int16) case *[]uint16: - *valuep.(*[]uint16) = value.([]uint16) + *valuep = value.([]uint16) case *[]int32: - *valuep.(*[]int32) = value.([]int32) + *valuep = value.([]int32) case *[]uint32: - *valuep.(*[]uint32) = value.([]uint32) + *valuep = value.([]uint32) case *[]int64: - *valuep.(*[]int64) = value.([]int64) + *valuep = value.([]int64) case *[]uint64: - *valuep.(*[]uint64) = value.([]uint64) + *valuep = value.([]uint64) } return nil @@ -322,9 +322,9 @@ func parseValueList(str, sep string, valuep interface{}) error { // Format a value into a string. func formatValue(value interface{}) (string, error) { - switch value.(type) { + switch value := value.(type) { case string: - return value.(string), nil + return value, nil case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64: return fmt.Sprintf("%d", value), nil default: @@ -336,9 +336,9 @@ func formatValue(value interface{}) (string, error) { func formatValueList(sep string, value interface{}) (string, error) { var v []interface{} - switch value.(type) { + switch value := value.(type) { case idset.IDSet: - return value.(idset.IDSet).StringWithSeparator(sep), nil + return value.StringWithSeparator(sep), nil case []int, []uint, []int8, []uint8, []int16, []uint16, []int32, []uint32, []int64, []uint64: v = value.([]interface{}) default: