Skip to content

Commit 9197cfa

Browse files
authored
Merge pull request #154 from numtide/feat/more-stringent-golangci-lint
feat: more stringent golangci-lint rules
2 parents e966906 + 5b4a7a8 commit 9197cfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+711
-780
lines changed

.golangci.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
linters:
2+
enable:
3+
- errname
4+
- exhaustive
5+
- gci
6+
- goconst
7+
- godot
8+
- gofumpt
9+
- goheader
10+
- goimports
11+
- gosec
12+
- importas
13+
- ireturn
14+
- lll
15+
- makezero
16+
- misspell
17+
- nakedret
18+
- nestif
19+
- nilerr
20+
- nilnil
21+
- noctx
22+
- nolintlint
23+
- prealloc
24+
- predeclared
25+
- revive
26+
- rowserrcheck
27+
- stylecheck
28+
- tenv
29+
- testpackage
30+
- unconvert
31+
- unparam
32+
- wastedassign
33+
- whitespace
34+
- wsl

cmd/root.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ func init() {
2525
flag.StringVar(&outputPath, "output", "", "path to write the report")
2626
flag.StringVar(&outputPath, "o", "", "path to write the report")
2727
flag.BoolVar(&scanner.Swap, "swap", false, "capture swap entries")
28-
flag.BoolVar(&scanner.Ephemeral, "ephemeral", false, "capture all ephemeral properties e.g. swap, filesystems and so on")
28+
flag.BoolVar(
29+
&scanner.Ephemeral, "ephemeral", false,
30+
"capture all ephemeral properties e.g. swap, filesystems and so on",
31+
)
2932
flag.StringVar(&logLevel, "log-level", "info", "log level")
3033

3134
defaultFeatures := []string{
3235
"memory", "pci", "net", "serial", "cpu", "bios", "monitor", "scsi", "usb", "prom", "sbus", "sys", "sysfs",
3336
"udev", "block", "wlan",
3437
}
3538

36-
probeFeatures := hwinfo.ProbeFeatureStrings()
37-
filteredFeatures := []string{}
38-
for _, feature := range probeFeatures {
39+
var filteredFeatures []string
40+
41+
for _, feature := range hwinfo.ProbeFeatureStrings() {
3942
if feature != "default" && feature != "int" {
4043
filteredFeatures = append(filteredFeatures, feature)
4144
}
@@ -47,8 +50,10 @@ func init() {
4750
hardwareFeatures = strings.Split(flagValue, ",")
4851
return nil
4952
})
53+
5054
possibleValues := strings.Join(filteredFeatures, ",")
5155
defaultValues := strings.Join(defaultFeatures, ",")
56+
5257
const usage = `nixos-facter [flags]
5358
Hardware report generator
5459
@@ -84,6 +89,7 @@ func Execute() {
8489
if err != nil {
8590
log.Fatalf("invalid hardware feature: %v", err)
8691
}
92+
8793
scanner.Features = append(scanner.Features, probe)
8894
}
8995

@@ -114,8 +120,9 @@ func Execute() {
114120
if _, err = os.Stdout.Write(bytes); err != nil {
115121
log.Fatalf("failed to write report to stdout: %v", err)
116122
}
123+
117124
fmt.Println()
118-
} else if err = os.WriteFile(outputPath, bytes, 0o644); err != nil {
125+
} else if err = os.WriteFile(outputPath, bytes, 0o600); err != nil {
119126
log.Fatalf("failed to write report to output path: %v", err)
120127
}
121128
}

pkg/ephem/ephem.go

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func StableDevicePath(device string) (string, error) {
2727
if !strings.HasPrefix("/", device) {
2828
return device, nil
2929
}
30+
3031
stat, err := os.Stat(device)
3132
if err != nil {
3233
return "", err
@@ -41,14 +42,18 @@ func StableDevicePath(device string) (string, error) {
4142
// the only possible error is ErrBadPattern
4243
return "", err
4344
}
45+
4446
for _, match := range matches {
4547
matchStat, err := os.Stat(match)
4648
if err != nil {
4749
l.Debug("failed to stat match", "match", match, "error", err)
50+
4851
continue
4952
}
53+
5054
if os.SameFile(stat, matchStat) {
5155
l.Debug("match found for device", "match", match, "device", device)
56+
5257
return match, nil
5358
}
5459
}

pkg/ephem/swap.go

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func SwapEntries() ([]*SwapEntry, error) {
5757
if err != nil {
5858
return nil, err
5959
}
60+
6061
devices[idx].Filename = stablePath
6162
}
6263

@@ -73,6 +74,7 @@ func ReadSwapFile(reader io.Reader) ([]*SwapEntry, error) {
7374
}
7475

7576
var result []*SwapEntry
77+
7678
for scanner.Scan() {
7779
line := scanner.Text()
7880

pkg/ephem/swap_test.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package ephem
1+
package ephem_test
22

33
import (
44
"strings"
55
"testing"
66

7+
"github.com/numtide/nixos-facter/pkg/ephem"
78
"github.com/stretchr/testify/require"
89
)
910

@@ -25,40 +26,40 @@ foo bar baz
2526
func TestReadSwapFile(t *testing.T) {
2627
as := require.New(t)
2728

28-
_, err := ReadSwapFile(strings.NewReader(""))
29+
_, err := ephem.ReadSwapFile(strings.NewReader(""))
2930
as.Error(err, "swaps file is empty")
3031

31-
_, err = ReadSwapFile(strings.NewReader("foo bar baz hello world\n"))
32+
_, err = ephem.ReadSwapFile(strings.NewReader("foo bar baz hello world\n"))
3233
as.Errorf(err, "header in swaps file is malformed: '%s'", "foo bar baz hello world\n")
3334

34-
swaps, err := ReadSwapFile(strings.NewReader(empty))
35+
swaps, err := ephem.ReadSwapFile(strings.NewReader(empty))
3536
as.NoError(err)
3637
as.Empty(swaps)
3738

38-
_, err = ReadSwapFile(strings.NewReader(corrupt))
39+
_, err = ephem.ReadSwapFile(strings.NewReader(corrupt))
3940
as.Errorf(err, "malformed entry in swaps file: '%s'", "foo bar baz")
4041

41-
_, err = ReadSwapFile(strings.NewReader(badPartition))
42+
_, err = ephem.ReadSwapFile(strings.NewReader(badPartition))
4243
as.Errorf(err, "malformed entry in swaps file: '%s'", `/var/lib/swap-1 foo 1048576 123 -3`)
4344

44-
swaps, err = ReadSwapFile(strings.NewReader(sample))
45+
swaps, err = ephem.ReadSwapFile(strings.NewReader(sample))
4546
as.NoError(err)
4647
as.Len(swaps, 3)
4748

4849
as.Equal(swaps[0].Filename, "/dev/sda6")
49-
as.Equal(swaps[0].Type, SwapTypePartition)
50+
as.Equal(swaps[0].Type, ephem.SwapTypePartition)
5051
as.Equal(swaps[0].Size, uint64(4194300))
5152
as.Equal(swaps[0].Used, uint64(0))
5253
as.Equal(swaps[0].Priority, int32(-1))
5354

5455
as.Equal(swaps[1].Filename, "/var/lib/swap-1")
55-
as.Equal(swaps[1].Type, SwapTypeFile)
56+
as.Equal(swaps[1].Type, ephem.SwapTypeFile)
5657
as.Equal(swaps[1].Size, uint64(1048576))
5758
as.Equal(swaps[1].Used, uint64(123))
5859
as.Equal(swaps[1].Priority, int32(-3))
5960

6061
as.Equal(swaps[2].Filename, "/var/lib/swap-2")
61-
as.Equal(swaps[2].Type, SwapTypeFile)
62+
as.Equal(swaps[2].Type, ephem.SwapTypeFile)
6263
as.Equal(swaps[2].Size, uint64(2097152))
6364
as.Equal(swaps[2].Used, uint64(4567))
6465
as.Equal(swaps[2].Priority, int32(-2))

pkg/facter/facter.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ package facter
55
import (
66
"fmt"
77

8-
"github.com/numtide/nixos-facter/pkg/ephem"
9-
108
"github.com/numtide/nixos-facter/pkg/build"
9+
"github.com/numtide/nixos-facter/pkg/ephem"
1110
"github.com/numtide/nixos-facter/pkg/hwinfo"
1211
"github.com/numtide/nixos-facter/pkg/virt"
1312
)
@@ -27,7 +26,8 @@ type Report struct {
2726
// Hardware provides detailed information about the system’s hardware components, such as CPU, memory, and peripherals.
2827
Hardware Hardware `json:"hardware,omitempty"`
2928

30-
// Smbios provides detailed information about the system's SMBios data, such as BIOS, board, chassis, memory, and processors.
29+
// Smbios provides detailed information about the system's SMBios data, such as BIOS, board, chassis, memory,
30+
// and processors.
3131
Smbios Smbios `json:"smbios,omitempty"`
3232

3333
// Swap contains a list of swap entries representing the system's swap devices or files and their respective details.
@@ -51,17 +51,21 @@ type Scanner struct {
5151
// It also detects IOMMU groups and handles errors gracefully if scanning fails.
5252
func (s *Scanner) Scan() (*Report, error) {
5353
var err error
54+
5455
report := Report{
5556
Version: build.ReportVersion,
5657
}
5758

5859
if build.System == "" {
5960
return nil, fmt.Errorf("system is not set")
6061
}
62+
6163
report.System = build.System
6264

63-
var smbios []hwinfo.Smbios
64-
var devices []hwinfo.HardwareDevice
65+
var (
66+
smbios []hwinfo.Smbios
67+
devices []hwinfo.HardwareDevice
68+
)
6569

6670
smbios, devices, err = hwinfo.Scan(s.Features)
6771
if err != nil {
@@ -77,10 +81,12 @@ func (s *Scanner) Scan() (*Report, error) {
7781
for idx := range devices {
7882
// lookup iommu group before adding to the report
7983
device := devices[idx]
80-
groupId, ok := iommuGroups[device.SysfsId]
84+
85+
groupID, ok := iommuGroups[device.SysfsID]
8186
if ok {
82-
device.SysfsIOMMUGroupId = &groupId
87+
device.SysfsIOMMUGroupID = &groupID
8388
}
89+
8490
if err = report.Hardware.add(device); err != nil {
8591
return nil, fmt.Errorf("failed to add to hardware report: %w", err)
8692
}

pkg/facter/hardware.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ type Hardware struct {
3434
// ChipCard holds the list of chip card devices in the hardware.
3535
ChipCard []hwinfo.HardwareDevice `json:"chip_card,omitempty"`
3636

37-
// Cpu holds the list of CPU details in the hardware.
37+
// CPU holds the list of CPU details in the hardware.
3838
// There is one entry per physical id.
39-
Cpu []hwinfo.DetailCpu `json:"cpu,omitempty"`
39+
CPU []*hwinfo.DetailCPU `json:"cpu,omitempty"`
4040

4141
// Disk holds the list of disk devices in the hardware.
4242
Disk []hwinfo.HardwareDevice `json:"disk,omitempty"`
@@ -181,7 +181,7 @@ type Hardware struct {
181181
}
182182

183183
func compareDevice(a hwinfo.HardwareDevice, b hwinfo.HardwareDevice) int {
184-
return int(a.Index - b.Index)
184+
return int(a.Index - b.Index) //nolint:gosec
185185
}
186186

187187
func (h *Hardware) add(device hwinfo.HardwareDevice) error {
@@ -197,10 +197,10 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error {
197197
case hwinfo.HardwareClassBios:
198198
if h.Bios != nil {
199199
return fmt.Errorf("bios field is already set")
200-
} else if bios, ok := device.Detail.(hwinfo.DetailBios); !ok {
200+
} else if bios, ok := device.Detail.(*hwinfo.DetailBios); !ok {
201201
return fmt.Errorf("expected hwinfo.DetailBios, found %T", device.Detail)
202202
} else {
203-
h.Bios = &bios
203+
h.Bios = bios
204204
}
205205
case hwinfo.HardwareClassBlockDevice:
206206
h.BlockDevice = append(h.BlockDevice, device)
@@ -224,22 +224,23 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error {
224224
h.ChipCard = append(h.ChipCard, device)
225225
slices.SortFunc(h.ChipCard, compareDevice)
226226
case hwinfo.HardwareClassCpu:
227-
cpu, ok := device.Detail.(hwinfo.DetailCpu)
227+
cpu, ok := device.Detail.(*hwinfo.DetailCPU)
228228
if !ok {
229-
return fmt.Errorf("expected hwinfo.DetailCpu, found %T", device.Detail)
229+
return fmt.Errorf("expected hwinfo.DetailCPU, found %T", device.Detail)
230230
}
231231

232232
// We insert by physical id, as we only want one entry per core.
233-
requiredSize := int(cpu.PhysicalId) + 1
234-
if len(h.Cpu) < requiredSize {
235-
newItems := make([]hwinfo.DetailCpu, requiredSize-len(h.Cpu))
236-
h.Cpu = append(h.Cpu, newItems...)
233+
requiredSize := int(cpu.PhysicalID) + 1 //nolint:gosec
234+
if len(h.CPU) < requiredSize {
235+
newItems := make([]*hwinfo.DetailCPU, requiredSize-len(h.CPU))
236+
h.CPU = append(h.CPU, newItems...)
237237
}
238-
h.Cpu[cpu.PhysicalId] = cpu
238+
239+
h.CPU[cpu.PhysicalID] = cpu
239240

240241
// Sort in ascending order to ensure a stable output
241-
slices.SortFunc(h.Cpu, func(a, b hwinfo.DetailCpu) int {
242-
return int(a.PhysicalId - b.PhysicalId)
242+
slices.SortFunc(h.CPU, func(a, b *hwinfo.DetailCPU) int {
243+
return int(a.PhysicalID - b.PhysicalID) //nolint:gosec
243244
})
244245

245246
case hwinfo.HardwareClassDisk:
@@ -359,10 +360,10 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error {
359360
case hwinfo.HardwareClassSystem:
360361
if h.System != nil {
361362
return fmt.Errorf("system field is already set")
362-
} else if system, ok := device.Detail.(hwinfo.DetailSys); !ok {
363+
} else if system, ok := device.Detail.(*hwinfo.DetailSys); !ok {
363364
return fmt.Errorf("expected hwinfo.DetailSys, found %T", device.Detail)
364365
} else {
365-
h.System = &system
366+
h.System = system
366367
}
367368
case hwinfo.HardwareClassTape:
368369
h.Tape = append(h.Tape, device)
@@ -388,8 +389,8 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error {
388389
case hwinfo.HardwareClassZip:
389390
h.Zip = append(h.Zip, device)
390391
slices.SortFunc(h.Zip, compareDevice)
391-
case hwinfo.HardwareClassAll:
392-
// Do nothing, this is used by the hwinfo cli exclusively.
392+
case hwinfo.HardwareClassAll: // Do nothing, this is used by the hwinfo cli exclusively.
393393
}
394+
394395
return nil
395396
}

0 commit comments

Comments
 (0)