diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..3224da8 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,34 @@ +linters: + enable: + - errname + - exhaustive + - gci + - goconst + - godot + - gofumpt + - goheader + - goimports + - gosec + - importas + - ireturn + - lll + - makezero + - misspell + - nakedret + - nestif + - nilerr + - nilnil + - noctx + - nolintlint + - prealloc + - predeclared + - revive + - rowserrcheck + - stylecheck + - tenv + - testpackage + - unconvert + - unparam + - wastedassign + - whitespace + - wsl diff --git a/cmd/root.go b/cmd/root.go index 77d5bec..ff5f6a4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,7 +25,10 @@ func init() { flag.StringVar(&outputPath, "output", "", "path to write the report") flag.StringVar(&outputPath, "o", "", "path to write the report") flag.BoolVar(&scanner.Swap, "swap", false, "capture swap entries") - flag.BoolVar(&scanner.Ephemeral, "ephemeral", false, "capture all ephemeral properties e.g. swap, filesystems and so on") + flag.BoolVar( + &scanner.Ephemeral, "ephemeral", false, + "capture all ephemeral properties e.g. swap, filesystems and so on", + ) flag.StringVar(&logLevel, "log-level", "info", "log level") defaultFeatures := []string{ @@ -33,9 +36,9 @@ func init() { "udev", "block", "wlan", } - probeFeatures := hwinfo.ProbeFeatureStrings() - filteredFeatures := []string{} - for _, feature := range probeFeatures { + var filteredFeatures []string + + for _, feature := range hwinfo.ProbeFeatureStrings() { if feature != "default" && feature != "int" { filteredFeatures = append(filteredFeatures, feature) } @@ -47,8 +50,10 @@ func init() { hardwareFeatures = strings.Split(flagValue, ",") return nil }) + possibleValues := strings.Join(filteredFeatures, ",") defaultValues := strings.Join(defaultFeatures, ",") + const usage = `nixos-facter [flags] Hardware report generator @@ -84,6 +89,7 @@ func Execute() { if err != nil { log.Fatalf("invalid hardware feature: %v", err) } + scanner.Features = append(scanner.Features, probe) } @@ -114,8 +120,9 @@ func Execute() { if _, err = os.Stdout.Write(bytes); err != nil { log.Fatalf("failed to write report to stdout: %v", err) } + fmt.Println() - } else if err = os.WriteFile(outputPath, bytes, 0o644); err != nil { + } else if err = os.WriteFile(outputPath, bytes, 0o600); err != nil { log.Fatalf("failed to write report to output path: %v", err) } } diff --git a/pkg/ephem/ephem.go b/pkg/ephem/ephem.go index 99476d9..24337c5 100644 --- a/pkg/ephem/ephem.go +++ b/pkg/ephem/ephem.go @@ -27,6 +27,7 @@ func StableDevicePath(device string) (string, error) { if !strings.HasPrefix("/", device) { return device, nil } + stat, err := os.Stat(device) if err != nil { return "", err @@ -41,14 +42,18 @@ func StableDevicePath(device string) (string, error) { // the only possible error is ErrBadPattern return "", err } + for _, match := range matches { matchStat, err := os.Stat(match) if err != nil { l.Debug("failed to stat match", "match", match, "error", err) + continue } + if os.SameFile(stat, matchStat) { l.Debug("match found for device", "match", match, "device", device) + return match, nil } } diff --git a/pkg/ephem/swap.go b/pkg/ephem/swap.go index 34d0b5c..83d6b75 100644 --- a/pkg/ephem/swap.go +++ b/pkg/ephem/swap.go @@ -57,6 +57,7 @@ func SwapEntries() ([]*SwapEntry, error) { if err != nil { return nil, err } + devices[idx].Filename = stablePath } @@ -73,6 +74,7 @@ func ReadSwapFile(reader io.Reader) ([]*SwapEntry, error) { } var result []*SwapEntry + for scanner.Scan() { line := scanner.Text() diff --git a/pkg/ephem/swap_test.go b/pkg/ephem/swap_test.go index f103d77..b82757f 100644 --- a/pkg/ephem/swap_test.go +++ b/pkg/ephem/swap_test.go @@ -1,9 +1,10 @@ -package ephem +package ephem_test import ( "strings" "testing" + "github.com/numtide/nixos-facter/pkg/ephem" "github.com/stretchr/testify/require" ) @@ -25,40 +26,40 @@ foo bar baz func TestReadSwapFile(t *testing.T) { as := require.New(t) - _, err := ReadSwapFile(strings.NewReader("")) + _, err := ephem.ReadSwapFile(strings.NewReader("")) as.Error(err, "swaps file is empty") - _, err = ReadSwapFile(strings.NewReader("foo bar baz hello world\n")) + _, err = ephem.ReadSwapFile(strings.NewReader("foo bar baz hello world\n")) as.Errorf(err, "header in swaps file is malformed: '%s'", "foo bar baz hello world\n") - swaps, err := ReadSwapFile(strings.NewReader(empty)) + swaps, err := ephem.ReadSwapFile(strings.NewReader(empty)) as.NoError(err) as.Empty(swaps) - _, err = ReadSwapFile(strings.NewReader(corrupt)) + _, err = ephem.ReadSwapFile(strings.NewReader(corrupt)) as.Errorf(err, "malformed entry in swaps file: '%s'", "foo bar baz") - _, err = ReadSwapFile(strings.NewReader(badPartition)) + _, err = ephem.ReadSwapFile(strings.NewReader(badPartition)) as.Errorf(err, "malformed entry in swaps file: '%s'", `/var/lib/swap-1 foo 1048576 123 -3`) - swaps, err = ReadSwapFile(strings.NewReader(sample)) + swaps, err = ephem.ReadSwapFile(strings.NewReader(sample)) as.NoError(err) as.Len(swaps, 3) as.Equal(swaps[0].Filename, "/dev/sda6") - as.Equal(swaps[0].Type, SwapTypePartition) + as.Equal(swaps[0].Type, ephem.SwapTypePartition) as.Equal(swaps[0].Size, uint64(4194300)) as.Equal(swaps[0].Used, uint64(0)) as.Equal(swaps[0].Priority, int32(-1)) as.Equal(swaps[1].Filename, "/var/lib/swap-1") - as.Equal(swaps[1].Type, SwapTypeFile) + as.Equal(swaps[1].Type, ephem.SwapTypeFile) as.Equal(swaps[1].Size, uint64(1048576)) as.Equal(swaps[1].Used, uint64(123)) as.Equal(swaps[1].Priority, int32(-3)) as.Equal(swaps[2].Filename, "/var/lib/swap-2") - as.Equal(swaps[2].Type, SwapTypeFile) + as.Equal(swaps[2].Type, ephem.SwapTypeFile) as.Equal(swaps[2].Size, uint64(2097152)) as.Equal(swaps[2].Used, uint64(4567)) as.Equal(swaps[2].Priority, int32(-2)) diff --git a/pkg/facter/facter.go b/pkg/facter/facter.go index d165bdf..0ab5b6c 100644 --- a/pkg/facter/facter.go +++ b/pkg/facter/facter.go @@ -5,9 +5,8 @@ package facter import ( "fmt" - "github.com/numtide/nixos-facter/pkg/ephem" - "github.com/numtide/nixos-facter/pkg/build" + "github.com/numtide/nixos-facter/pkg/ephem" "github.com/numtide/nixos-facter/pkg/hwinfo" "github.com/numtide/nixos-facter/pkg/virt" ) @@ -27,7 +26,8 @@ type Report struct { // Hardware provides detailed information about the system’s hardware components, such as CPU, memory, and peripherals. Hardware Hardware `json:"hardware,omitempty"` - // Smbios provides detailed information about the system's SMBios data, such as BIOS, board, chassis, memory, and processors. + // Smbios provides detailed information about the system's SMBios data, such as BIOS, board, chassis, memory, + // and processors. Smbios Smbios `json:"smbios,omitempty"` // Swap contains a list of swap entries representing the system's swap devices or files and their respective details. @@ -51,6 +51,7 @@ type Scanner struct { // It also detects IOMMU groups and handles errors gracefully if scanning fails. func (s *Scanner) Scan() (*Report, error) { var err error + report := Report{ Version: build.ReportVersion, } @@ -58,10 +59,13 @@ func (s *Scanner) Scan() (*Report, error) { if build.System == "" { return nil, fmt.Errorf("system is not set") } + report.System = build.System - var smbios []hwinfo.Smbios - var devices []hwinfo.HardwareDevice + var ( + smbios []hwinfo.Smbios + devices []hwinfo.HardwareDevice + ) smbios, devices, err = hwinfo.Scan(s.Features) if err != nil { @@ -77,10 +81,12 @@ func (s *Scanner) Scan() (*Report, error) { for idx := range devices { // lookup iommu group before adding to the report device := devices[idx] - groupId, ok := iommuGroups[device.SysfsId] + + groupID, ok := iommuGroups[device.SysfsID] if ok { - device.SysfsIOMMUGroupId = &groupId + device.SysfsIOMMUGroupID = &groupID } + if err = report.Hardware.add(device); err != nil { return nil, fmt.Errorf("failed to add to hardware report: %w", err) } diff --git a/pkg/facter/hardware.go b/pkg/facter/hardware.go index 55b7eca..23e8661 100644 --- a/pkg/facter/hardware.go +++ b/pkg/facter/hardware.go @@ -34,9 +34,9 @@ type Hardware struct { // ChipCard holds the list of chip card devices in the hardware. ChipCard []hwinfo.HardwareDevice `json:"chip_card,omitempty"` - // Cpu holds the list of CPU details in the hardware. + // CPU holds the list of CPU details in the hardware. // There is one entry per physical id. - Cpu []hwinfo.DetailCpu `json:"cpu,omitempty"` + CPU []*hwinfo.DetailCPU `json:"cpu,omitempty"` // Disk holds the list of disk devices in the hardware. Disk []hwinfo.HardwareDevice `json:"disk,omitempty"` @@ -181,7 +181,7 @@ type Hardware struct { } func compareDevice(a hwinfo.HardwareDevice, b hwinfo.HardwareDevice) int { - return int(a.Index - b.Index) + return int(a.Index - b.Index) //nolint:gosec } func (h *Hardware) add(device hwinfo.HardwareDevice) error { @@ -197,10 +197,10 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error { case hwinfo.HardwareClassBios: if h.Bios != nil { return fmt.Errorf("bios field is already set") - } else if bios, ok := device.Detail.(hwinfo.DetailBios); !ok { + } else if bios, ok := device.Detail.(*hwinfo.DetailBios); !ok { return fmt.Errorf("expected hwinfo.DetailBios, found %T", device.Detail) } else { - h.Bios = &bios + h.Bios = bios } case hwinfo.HardwareClassBlockDevice: h.BlockDevice = append(h.BlockDevice, device) @@ -224,22 +224,23 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error { h.ChipCard = append(h.ChipCard, device) slices.SortFunc(h.ChipCard, compareDevice) case hwinfo.HardwareClassCpu: - cpu, ok := device.Detail.(hwinfo.DetailCpu) + cpu, ok := device.Detail.(*hwinfo.DetailCPU) if !ok { - return fmt.Errorf("expected hwinfo.DetailCpu, found %T", device.Detail) + return fmt.Errorf("expected hwinfo.DetailCPU, found %T", device.Detail) } // We insert by physical id, as we only want one entry per core. - requiredSize := int(cpu.PhysicalId) + 1 - if len(h.Cpu) < requiredSize { - newItems := make([]hwinfo.DetailCpu, requiredSize-len(h.Cpu)) - h.Cpu = append(h.Cpu, newItems...) + requiredSize := int(cpu.PhysicalID) + 1 //nolint:gosec + if len(h.CPU) < requiredSize { + newItems := make([]*hwinfo.DetailCPU, requiredSize-len(h.CPU)) + h.CPU = append(h.CPU, newItems...) } - h.Cpu[cpu.PhysicalId] = cpu + + h.CPU[cpu.PhysicalID] = cpu // Sort in ascending order to ensure a stable output - slices.SortFunc(h.Cpu, func(a, b hwinfo.DetailCpu) int { - return int(a.PhysicalId - b.PhysicalId) + slices.SortFunc(h.CPU, func(a, b *hwinfo.DetailCPU) int { + return int(a.PhysicalID - b.PhysicalID) //nolint:gosec }) case hwinfo.HardwareClassDisk: @@ -359,10 +360,10 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error { case hwinfo.HardwareClassSystem: if h.System != nil { return fmt.Errorf("system field is already set") - } else if system, ok := device.Detail.(hwinfo.DetailSys); !ok { + } else if system, ok := device.Detail.(*hwinfo.DetailSys); !ok { return fmt.Errorf("expected hwinfo.DetailSys, found %T", device.Detail) } else { - h.System = &system + h.System = system } case hwinfo.HardwareClassTape: h.Tape = append(h.Tape, device) @@ -388,8 +389,8 @@ func (h *Hardware) add(device hwinfo.HardwareDevice) error { case hwinfo.HardwareClassZip: h.Zip = append(h.Zip, device) slices.SortFunc(h.Zip, compareDevice) - case hwinfo.HardwareClassAll: - // Do nothing, this is used by the hwinfo cli exclusively. + case hwinfo.HardwareClassAll: // Do nothing, this is used by the hwinfo cli exclusively. } + return nil } diff --git a/pkg/facter/smbios.go b/pkg/facter/smbios.go index 94e4b78..40f2ad3 100644 --- a/pkg/facter/smbios.go +++ b/pkg/facter/smbios.go @@ -78,36 +78,36 @@ func (s *Smbios) add(item hwinfo.Smbios) error { case hwinfo.SmbiosTypeBios: if s.Bios != nil { return fmt.Errorf("bios field is already set") - } else if bios, ok := item.(hwinfo.SmbiosBios); !ok { + } else if bios, ok := item.(*hwinfo.SmbiosBios); !ok { return fmt.Errorf("expected hwinfo.SmbiosBios, found %T", item) } else { - s.Bios = &bios + s.Bios = bios } case hwinfo.SmbiosTypeBoard: if s.Board != nil { return fmt.Errorf("board field is already set") - } else if board, ok := item.(hwinfo.SmbiosBoard); !ok { + } else if board, ok := item.(*hwinfo.SmbiosBoard); !ok { return fmt.Errorf("expected hwinfo.SmbiosBoard, found %T", item) } else { - s.Board = &board + s.Board = board } case hwinfo.SmbiosTypeCache: s.Cache = append(s.Cache, item) case hwinfo.SmbiosTypeChassis: if s.Chassis != nil { return fmt.Errorf("chassis field is already set") - } else if chassis, ok := item.(hwinfo.SmbiosChassis); !ok { + } else if chassis, ok := item.(*hwinfo.SmbiosChassis); !ok { return fmt.Errorf("expected hwinfo.SmbiosChassis, found %T", item) } else { - s.Chassis = &chassis + s.Chassis = chassis } case hwinfo.SmbiosTypeConfig: if s.Config != nil { return fmt.Errorf("config field is already set") - } else if config, ok := item.(hwinfo.SmbiosConfig); !ok { + } else if config, ok := item.(*hwinfo.SmbiosConfig); !ok { return fmt.Errorf("expected hwinfo.SmbiosConfig, found %T", item) } else { - s.Config = &config + s.Config = config } case hwinfo.SmbiosTypeGroupAssociations: s.GroupAssociations = append(s.GroupAssociations, item) @@ -142,13 +142,24 @@ func (s *Smbios) add(item hwinfo.Smbios) error { case hwinfo.SmbiosTypeSystem: if s.System != nil { return fmt.Errorf("system field is already set") - } else if system, ok := item.(hwinfo.SmbiosSystem); !ok { + } else if system, ok := item.(*hwinfo.SmbiosSystem); !ok { return fmt.Errorf("expected hwinfo.SmbiosSystem, found %T", item) } else { - s.System = &system + s.System = system } + + case hwinfo.SmbiosTypeMemoryController, hwinfo.SmbiosTypeMemoryModule, hwinfo.SmbiosTypeOEMStrings, + hwinfo.SmbiosTypeEventLog, hwinfo.SmbiosTypeBattery, hwinfo.SmbiosTypeSystemReset, hwinfo.SmbiosTypeVoltage, + hwinfo.SmbiosTypeCoolingDevice, hwinfo.SmbiosTypeTemperature, hwinfo.SmbiosTypeCurrent, + hwinfo.SmbiosTypeOutOfBandRemoteAccess, hwinfo.SmbiosTypeBootIntegrityServices, hwinfo.SmbiosTypeSystemBoot, + hwinfo.SmbiosTypeManagementDevice, hwinfo.SmbiosTypeManDeviceComponent, hwinfo.SmbiosTypeManDeviceThreshold, + hwinfo.SmbiosTypeMemoryChannel, hwinfo.SmbiosTypeIPMIDevice, hwinfo.SmbiosTypeSystemPowerSupply, + hwinfo.SmbiosTypeAdditionalInfo, hwinfo.SmbiosTypeOnboardExtended, + hwinfo.SmbiosTypeManagementControllerHostInterface, hwinfo.SmbiosTypeTPM, hwinfo.SmbiosTypeProcessorAdditional, + hwinfo.SmbiosTypeFirmwareInventory, hwinfo.SmbiosTypeInactive, hwinfo.SmbiosTypeEndOfTable: + // currently not supported default: - // Do nothing for the rest of the types, we currently don't map them + return fmt.Errorf("unknown smbios type %d", item.SmbiosType()) } return nil diff --git a/pkg/hwinfo/detail.go b/pkg/hwinfo/detail.go index 942f982..b4a77ee 100644 --- a/pkg/hwinfo/detail.go +++ b/pkg/hwinfo/detail.go @@ -19,12 +19,14 @@ import "C" import ( "encoding/hex" + "fmt" "unsafe" ) //go:generate enumer -type=DetailType -json -transform=snake -trimprefix DetailType -output=./detail_enum_type.go type DetailType uint +//nolint:revive,stylecheck const ( DetailTypePci DetailType = iota DetailTypeUsb @@ -49,34 +51,36 @@ type Detail interface { DetailType() DetailType } -func NewDetail(detail *C.hd_detail_t) (Detail, error) { +//nolint:ireturn +func NewDetail(detail *C.hd_detail_t) (result Detail, err error) { if detail == nil { - return nil, nil + return result, err } - detailType := DetailType(C.hd_detail_get_type(detail)) - - switch detailType { + switch DetailType(C.hd_detail_get_type(detail)) { case DetailTypePci: - return NewDetailPci(C.hd_detail_get_pci(detail)) + result, err = NewDetailPci(C.hd_detail_get_pci(detail)) case DetailTypeUsb: - return NewDetailUsb(C.hd_detail_get_usb(detail)) + result, err = NewDetailUsb(C.hd_detail_get_usb(detail)) case DetailTypeIsaPnp: - return NewDetailIsaPnpDevice(C.hd_detail_get_isapnp(detail)) + result, err = NewDetailIsaPnpDevice(C.hd_detail_get_isapnp(detail)) case DetailTypeCpu: - return NewDetailCpu(C.hd_detail_get_cpu(detail)) + result, err = NewDetailCPU(C.hd_detail_get_cpu(detail)) case DetailTypeMonitor: - return NewDetailMonitor(C.hd_detail_get_monitor(detail)) + result, err = NewDetailMonitor(C.hd_detail_get_monitor(detail)) case DetailTypeBios: - return NewDetailBios(C.hd_detail_get_bios(detail)) + result, err = NewDetailBios(C.hd_detail_get_bios(detail)) case DetailTypeSys: - return NewDetailSys(C.hd_detail_get_sys(detail)) + result, err = NewDetailSys(C.hd_detail_get_sys(detail)) + case DetailTypeCdrom, DetailTypeFloppy, DetailTypeProm, DetailTypeScsi, DetailTypeDevtree, DetailTypeCcw, + DetailTypeJoystick: + // do nothing for now + default: - return nil, nil - // return nil, fmt.Errorf("unexpected detail type: %v", detailType) + err = fmt.Errorf("unknown detail type %d", DetailType(C.hd_detail_get_type(detail))) } - // todo cdrom, floppy, prom, sys, scsi, devtree, ccw, joystick + return result, err } type MemoryRange struct { diff --git a/pkg/hwinfo/detail_bios.go b/pkg/hwinfo/detail_bios.go index 044ac05..921ebce 100644 --- a/pkg/hwinfo/detail_bios.go +++ b/pkg/hwinfo/detail_bios.go @@ -50,10 +50,10 @@ func (d DetailBios) DetailType() DetailType { return DetailTypeBios } -func NewDetailBios(dev C.hd_detail_bios_t) (Detail, error) { +func NewDetailBios(dev C.hd_detail_bios_t) (*DetailBios, error) { data := dev.data - return DetailBios{ + return &DetailBios{ Type: DetailTypeBios, ApmInfo: ApmInfo{ Supported: bool(C.bios_info_is_apm_supported(data)), diff --git a/pkg/hwinfo/detail_cpu.go b/pkg/hwinfo/detail_cpu.go index e2f30fa..96e20ee 100644 --- a/pkg/hwinfo/detail_cpu.go +++ b/pkg/hwinfo/detail_cpu.go @@ -12,27 +12,27 @@ bool cpu_info_write_protect(cpu_info_t *info) { return info->write_protect; } import "C" import "regexp" -//go:generate enumer -type=CpuArch -json -transform=snake -trimprefix CpuArch -output=./detail_enum_cpu_arch.go -type CpuArch uint +//go:generate enumer -type=CPUArch -json -transform=snake -trimprefix CPUArch -output=./detail_enum_cpu_arch.go +type CPUArch uint const ( - CpuArchUnknown CpuArch = iota - CpuArchIntel - CpuArchAlpha - CpuArchSparc - CpuArchSparc64 - CpuArchPpc - CpuArchPpc64 + CPUArchUnknown CPUArch = iota + CPUArchIntel + CPUArchAlpha + CPUArchSparc + CPUArchSparc64 + CPUArchPpc + CPUArchPpc64 CpiArch68k - CpuArchIa64 - CpuArchS390 - CpuArchS390x - CpuArchArm - CpuArchMips - CpuArchx86_64 - CpuArchAarch64 - CpuArchLoongarch - CpuArchRiscv + CPUArchIa64 + CPUArchS390 + CPUArchS390x + CPUArchArm + CPUArchMips + CPUArchX86_64 + CPUArchAarch64 + CPUArchLoongarch + CPUArchRiscv ) type AddressSizes struct { @@ -40,10 +40,10 @@ type AddressSizes struct { Virtual uint `json:"virtual,omitempty"` } -type DetailCpu struct { +type DetailCPU struct { Type DetailType `json:"-"` - Architecture CpuArch `json:"architecture"` + Architecture CPUArch `json:"architecture"` VendorName string `json:"vendor_name,omitempty"` ModelName string `json:"model_name,omitempty"` @@ -64,10 +64,10 @@ type DetailCpu struct { Clock uint `json:"-"` // x86 only fields - PhysicalId uint `json:"physical_id"` + PhysicalID uint `json:"physical_id"` Siblings uint `json:"siblings,omitempty"` Cores uint `json:"cores,omitempty"` - CoreId uint `json:"-"` + CoreID uint `json:"-"` Fpu bool `json:"fpu"` FpuException bool `json:"fpu_exception"` CpuidLevel uint `json:"cpuid_level,omitempty"` @@ -82,20 +82,20 @@ type DetailCpu struct { var matchCPUFreq = regexp.MustCompile(`, \d+ MHz$`) -func stripCpuFreq(s string) string { +func stripCPUFreq(s string) string { // strip frequency of the model name as it is not stable. return matchCPUFreq.ReplaceAllString(s, "") } -func NewDetailCpu(cpu C.hd_detail_cpu_t) (Detail, error) { +func NewDetailCPU(cpu C.hd_detail_cpu_t) (*DetailCPU, error) { data := cpu.data - return DetailCpu{ + return &DetailCPU{ Type: DetailTypeCpu, - Architecture: CpuArch(data.architecture), + Architecture: CPUArch(data.architecture), VendorName: C.GoString(data.vend_name), - ModelName: stripCpuFreq(C.GoString(data.model_name)), + ModelName: stripCPUFreq(C.GoString(data.model_name)), Family: uint(data.family), Model: uint(data.model), @@ -112,10 +112,10 @@ func NewDetailCpu(cpu C.hd_detail_cpu_t) (Detail, error) { Cache: uint(data.cache), Units: uint(data.units), - PhysicalId: uint(data.physical_id), + PhysicalID: uint(data.physical_id), Siblings: uint(data.siblings), Cores: uint(data.cores), - CoreId: uint(data.core_id), + CoreID: uint(data.core_id), Apicid: uint(data.apicid), ApicidInitial: uint(data.apicid_initial), Fpu: bool(C.cpu_info_fpu(data)), @@ -132,6 +132,6 @@ func NewDetailCpu(cpu C.hd_detail_cpu_t) (Detail, error) { }, nil } -func (d DetailCpu) DetailType() DetailType { +func (d DetailCPU) DetailType() DetailType { return DetailTypeCpu } diff --git a/pkg/hwinfo/detail_enum_cpu_arch.go b/pkg/hwinfo/detail_enum_cpu_arch.go index ca56b7d..9d1ba45 100644 --- a/pkg/hwinfo/detail_enum_cpu_arch.go +++ b/pkg/hwinfo/detail_enum_cpu_arch.go @@ -1,4 +1,4 @@ -// Code generated by "enumer -type=CpuArch -json -transform=snake -trimprefix CpuArch -output=./detail_enum_cpu_arch.go"; DO NOT EDIT. +// Code generated by "enumer -type=CPUArch -json -transform=snake -trimprefix CPUArch -output=./detail_enum_cpu_arch.go"; DO NOT EDIT. package hwinfo @@ -8,129 +8,129 @@ import ( "strings" ) -const _CpuArchName = "unknownintelalphasparcsparc64ppcppc64cpi_arch68kia64s390s390xarmmipsx86_64aarch64loongarchriscv" +const _CPUArchName = "unknownintelalphasparcsparc64ppcppc64cpi_arch68kia64s390s390xarmmipsx86_64aarch64loongarchriscv" -var _CpuArchIndex = [...]uint8{0, 7, 12, 17, 22, 29, 32, 37, 48, 52, 56, 61, 64, 68, 74, 81, 90, 95} +var _CPUArchIndex = [...]uint8{0, 7, 12, 17, 22, 29, 32, 37, 48, 52, 56, 61, 64, 68, 74, 81, 90, 95} -const _CpuArchLowerName = "unknownintelalphasparcsparc64ppcppc64cpi_arch68kia64s390s390xarmmipsx86_64aarch64loongarchriscv" +const _CPUArchLowerName = "unknownintelalphasparcsparc64ppcppc64cpi_arch68kia64s390s390xarmmipsx86_64aarch64loongarchriscv" -func (i CpuArch) String() string { - if i >= CpuArch(len(_CpuArchIndex)-1) { - return fmt.Sprintf("CpuArch(%d)", i) +func (i CPUArch) String() string { + if i >= CPUArch(len(_CPUArchIndex)-1) { + return fmt.Sprintf("CPUArch(%d)", i) } - return _CpuArchName[_CpuArchIndex[i]:_CpuArchIndex[i+1]] + return _CPUArchName[_CPUArchIndex[i]:_CPUArchIndex[i+1]] } // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. -func _CpuArchNoOp() { +func _CPUArchNoOp() { var x [1]struct{} - _ = x[CpuArchUnknown-(0)] - _ = x[CpuArchIntel-(1)] - _ = x[CpuArchAlpha-(2)] - _ = x[CpuArchSparc-(3)] - _ = x[CpuArchSparc64-(4)] - _ = x[CpuArchPpc-(5)] - _ = x[CpuArchPpc64-(6)] + _ = x[CPUArchUnknown-(0)] + _ = x[CPUArchIntel-(1)] + _ = x[CPUArchAlpha-(2)] + _ = x[CPUArchSparc-(3)] + _ = x[CPUArchSparc64-(4)] + _ = x[CPUArchPpc-(5)] + _ = x[CPUArchPpc64-(6)] _ = x[CpiArch68k-(7)] - _ = x[CpuArchIa64-(8)] - _ = x[CpuArchS390-(9)] - _ = x[CpuArchS390x-(10)] - _ = x[CpuArchArm-(11)] - _ = x[CpuArchMips-(12)] - _ = x[CpuArchx86_64-(13)] - _ = x[CpuArchAarch64-(14)] - _ = x[CpuArchLoongarch-(15)] - _ = x[CpuArchRiscv-(16)] + _ = x[CPUArchIa64-(8)] + _ = x[CPUArchS390-(9)] + _ = x[CPUArchS390x-(10)] + _ = x[CPUArchArm-(11)] + _ = x[CPUArchMips-(12)] + _ = x[CPUArchX86_64-(13)] + _ = x[CPUArchAarch64-(14)] + _ = x[CPUArchLoongarch-(15)] + _ = x[CPUArchRiscv-(16)] } -var _CpuArchValues = []CpuArch{CpuArchUnknown, CpuArchIntel, CpuArchAlpha, CpuArchSparc, CpuArchSparc64, CpuArchPpc, CpuArchPpc64, CpiArch68k, CpuArchIa64, CpuArchS390, CpuArchS390x, CpuArchArm, CpuArchMips, CpuArchx86_64, CpuArchAarch64, CpuArchLoongarch, CpuArchRiscv} - -var _CpuArchNameToValueMap = map[string]CpuArch{ - _CpuArchName[0:7]: CpuArchUnknown, - _CpuArchLowerName[0:7]: CpuArchUnknown, - _CpuArchName[7:12]: CpuArchIntel, - _CpuArchLowerName[7:12]: CpuArchIntel, - _CpuArchName[12:17]: CpuArchAlpha, - _CpuArchLowerName[12:17]: CpuArchAlpha, - _CpuArchName[17:22]: CpuArchSparc, - _CpuArchLowerName[17:22]: CpuArchSparc, - _CpuArchName[22:29]: CpuArchSparc64, - _CpuArchLowerName[22:29]: CpuArchSparc64, - _CpuArchName[29:32]: CpuArchPpc, - _CpuArchLowerName[29:32]: CpuArchPpc, - _CpuArchName[32:37]: CpuArchPpc64, - _CpuArchLowerName[32:37]: CpuArchPpc64, - _CpuArchName[37:48]: CpiArch68k, - _CpuArchLowerName[37:48]: CpiArch68k, - _CpuArchName[48:52]: CpuArchIa64, - _CpuArchLowerName[48:52]: CpuArchIa64, - _CpuArchName[52:56]: CpuArchS390, - _CpuArchLowerName[52:56]: CpuArchS390, - _CpuArchName[56:61]: CpuArchS390x, - _CpuArchLowerName[56:61]: CpuArchS390x, - _CpuArchName[61:64]: CpuArchArm, - _CpuArchLowerName[61:64]: CpuArchArm, - _CpuArchName[64:68]: CpuArchMips, - _CpuArchLowerName[64:68]: CpuArchMips, - _CpuArchName[68:74]: CpuArchx86_64, - _CpuArchLowerName[68:74]: CpuArchx86_64, - _CpuArchName[74:81]: CpuArchAarch64, - _CpuArchLowerName[74:81]: CpuArchAarch64, - _CpuArchName[81:90]: CpuArchLoongarch, - _CpuArchLowerName[81:90]: CpuArchLoongarch, - _CpuArchName[90:95]: CpuArchRiscv, - _CpuArchLowerName[90:95]: CpuArchRiscv, +var _CPUArchValues = []CPUArch{CPUArchUnknown, CPUArchIntel, CPUArchAlpha, CPUArchSparc, CPUArchSparc64, CPUArchPpc, CPUArchPpc64, CpiArch68k, CPUArchIa64, CPUArchS390, CPUArchS390x, CPUArchArm, CPUArchMips, CPUArchX86_64, CPUArchAarch64, CPUArchLoongarch, CPUArchRiscv} + +var _CPUArchNameToValueMap = map[string]CPUArch{ + _CPUArchName[0:7]: CPUArchUnknown, + _CPUArchLowerName[0:7]: CPUArchUnknown, + _CPUArchName[7:12]: CPUArchIntel, + _CPUArchLowerName[7:12]: CPUArchIntel, + _CPUArchName[12:17]: CPUArchAlpha, + _CPUArchLowerName[12:17]: CPUArchAlpha, + _CPUArchName[17:22]: CPUArchSparc, + _CPUArchLowerName[17:22]: CPUArchSparc, + _CPUArchName[22:29]: CPUArchSparc64, + _CPUArchLowerName[22:29]: CPUArchSparc64, + _CPUArchName[29:32]: CPUArchPpc, + _CPUArchLowerName[29:32]: CPUArchPpc, + _CPUArchName[32:37]: CPUArchPpc64, + _CPUArchLowerName[32:37]: CPUArchPpc64, + _CPUArchName[37:48]: CpiArch68k, + _CPUArchLowerName[37:48]: CpiArch68k, + _CPUArchName[48:52]: CPUArchIa64, + _CPUArchLowerName[48:52]: CPUArchIa64, + _CPUArchName[52:56]: CPUArchS390, + _CPUArchLowerName[52:56]: CPUArchS390, + _CPUArchName[56:61]: CPUArchS390x, + _CPUArchLowerName[56:61]: CPUArchS390x, + _CPUArchName[61:64]: CPUArchArm, + _CPUArchLowerName[61:64]: CPUArchArm, + _CPUArchName[64:68]: CPUArchMips, + _CPUArchLowerName[64:68]: CPUArchMips, + _CPUArchName[68:74]: CPUArchX86_64, + _CPUArchLowerName[68:74]: CPUArchX86_64, + _CPUArchName[74:81]: CPUArchAarch64, + _CPUArchLowerName[74:81]: CPUArchAarch64, + _CPUArchName[81:90]: CPUArchLoongarch, + _CPUArchLowerName[81:90]: CPUArchLoongarch, + _CPUArchName[90:95]: CPUArchRiscv, + _CPUArchLowerName[90:95]: CPUArchRiscv, } -var _CpuArchNames = []string{ - _CpuArchName[0:7], - _CpuArchName[7:12], - _CpuArchName[12:17], - _CpuArchName[17:22], - _CpuArchName[22:29], - _CpuArchName[29:32], - _CpuArchName[32:37], - _CpuArchName[37:48], - _CpuArchName[48:52], - _CpuArchName[52:56], - _CpuArchName[56:61], - _CpuArchName[61:64], - _CpuArchName[64:68], - _CpuArchName[68:74], - _CpuArchName[74:81], - _CpuArchName[81:90], - _CpuArchName[90:95], +var _CPUArchNames = []string{ + _CPUArchName[0:7], + _CPUArchName[7:12], + _CPUArchName[12:17], + _CPUArchName[17:22], + _CPUArchName[22:29], + _CPUArchName[29:32], + _CPUArchName[32:37], + _CPUArchName[37:48], + _CPUArchName[48:52], + _CPUArchName[52:56], + _CPUArchName[56:61], + _CPUArchName[61:64], + _CPUArchName[64:68], + _CPUArchName[68:74], + _CPUArchName[74:81], + _CPUArchName[81:90], + _CPUArchName[90:95], } -// CpuArchString retrieves an enum value from the enum constants string name. +// CPUArchString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. -func CpuArchString(s string) (CpuArch, error) { - if val, ok := _CpuArchNameToValueMap[s]; ok { +func CPUArchString(s string) (CPUArch, error) { + if val, ok := _CPUArchNameToValueMap[s]; ok { return val, nil } - if val, ok := _CpuArchNameToValueMap[strings.ToLower(s)]; ok { + if val, ok := _CPUArchNameToValueMap[strings.ToLower(s)]; ok { return val, nil } - return 0, fmt.Errorf("%s does not belong to CpuArch values", s) + return 0, fmt.Errorf("%s does not belong to CPUArch values", s) } -// CpuArchValues returns all values of the enum -func CpuArchValues() []CpuArch { - return _CpuArchValues +// CPUArchValues returns all values of the enum +func CPUArchValues() []CPUArch { + return _CPUArchValues } -// CpuArchStrings returns a slice of all String values of the enum -func CpuArchStrings() []string { - strs := make([]string, len(_CpuArchNames)) - copy(strs, _CpuArchNames) +// CPUArchStrings returns a slice of all String values of the enum +func CPUArchStrings() []string { + strs := make([]string, len(_CPUArchNames)) + copy(strs, _CPUArchNames) return strs } -// IsACpuArch returns "true" if the value is listed in the enum definition. "false" otherwise -func (i CpuArch) IsACpuArch() bool { - for _, v := range _CpuArchValues { +// IsACPUArch returns "true" if the value is listed in the enum definition. "false" otherwise +func (i CPUArch) IsACPUArch() bool { + for _, v := range _CPUArchValues { if i == v { return true } @@ -138,19 +138,19 @@ func (i CpuArch) IsACpuArch() bool { return false } -// MarshalJSON implements the json.Marshaler interface for CpuArch -func (i CpuArch) MarshalJSON() ([]byte, error) { +// MarshalJSON implements the json.Marshaler interface for CPUArch +func (i CPUArch) MarshalJSON() ([]byte, error) { return json.Marshal(i.String()) } -// UnmarshalJSON implements the json.Unmarshaler interface for CpuArch -func (i *CpuArch) UnmarshalJSON(data []byte) error { +// UnmarshalJSON implements the json.Unmarshaler interface for CPUArch +func (i *CPUArch) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { - return fmt.Errorf("CpuArch should be a string, got %s", data) + return fmt.Errorf("CPUArch should be a string, got %s", data) } var err error - *i, err = CpuArchString(s) + *i, err = CPUArchString(s) return err } diff --git a/pkg/hwinfo/detail_isa_pnp_dev.go b/pkg/hwinfo/detail_isa_pnp_dev.go index 12b38f0..11cce93 100644 --- a/pkg/hwinfo/detail_isa_pnp_dev.go +++ b/pkg/hwinfo/detail_isa_pnp_dev.go @@ -12,6 +12,7 @@ import "C" import ( "encoding/hex" + "fmt" "unsafe" ) @@ -45,8 +46,9 @@ type IsaPnpCard struct { func NewIsaPnpCard(card *C.isapnp_card_t) (*IsaPnpCard, error) { if card == nil { - return nil, nil + return nil, fmt.Errorf("card is nil") } + return &IsaPnpCard{ Csn: int(card.csn), LogDevs: int(card.log_devs), @@ -70,7 +72,7 @@ func (d DetailIsaPnpDevice) DetailType() DetailType { return DetailTypeIsaPnp } -func NewDetailIsaPnpDevice(pnp C.hd_detail_isapnp_t) (Detail, error) { +func NewDetailIsaPnpDevice(pnp C.hd_detail_isapnp_t) (*DetailIsaPnpDevice, error) { data := pnp.data card, err := NewIsaPnpCard(data.card) @@ -78,7 +80,7 @@ func NewDetailIsaPnpDevice(pnp C.hd_detail_isapnp_t) (Detail, error) { return nil, err } - return DetailIsaPnpDevice{ + return &DetailIsaPnpDevice{ Type: DetailTypeIsaPnp, Card: card, Device: int(data.dev), diff --git a/pkg/hwinfo/detail_monitor.go b/pkg/hwinfo/detail_monitor.go index 4c78e1b..12f22d1 100644 --- a/pkg/hwinfo/detail_monitor.go +++ b/pkg/hwinfo/detail_monitor.go @@ -43,10 +43,10 @@ func (d DetailMonitor) DetailType() DetailType { return DetailTypeMonitor } -func NewDetailMonitor(mon C.hd_detail_monitor_t) (Detail, error) { +func NewDetailMonitor(mon C.hd_detail_monitor_t) (*DetailMonitor, error) { data := mon.data - return DetailMonitor{ + return &DetailMonitor{ Type: DetailTypeMonitor, ManufactureYear: uint(data.manu_year), ManufactureWeek: uint(data.manu_week), diff --git a/pkg/hwinfo/detail_pci.go b/pkg/hwinfo/detail_pci.go index a1ab297..518293d 100644 --- a/pkg/hwinfo/detail_pci.go +++ b/pkg/hwinfo/detail_pci.go @@ -44,7 +44,8 @@ type DetailPci struct { SecondaryBus uint `json:"secondary_bus"` // > 0 for PCI & CB bridges Irq uint `json:"irq"` // used irq if any - // Programming Interface Byte: a read-only register that specifies a register-level programming interface for the device. + // Programming Interface Byte: a read-only register that specifies a register-level programming interface for the + // device. ProgIf uint `json:"prog_if"` // already included in the parent model, so we omit from JSON output @@ -67,8 +68,8 @@ type DetailPci struct { RomBaseAddress uint64 `json:"-"` // memory base for card ROM RomBaseLength uint64 `json:"-"` // memory range for card ROM - SysfsId string `json:"-"` // sysfs path - SysfsBusId string `json:"-"` // sysfs bus id + SysfsID string `json:"-"` // sysfs path + SysfsBusID string `json:"-"` // sysfs bus id ModuleAlias string `json:"-"` // module alias Label string `json:"-"` // Consistent Device Name (CDN), pci firmware 3.1, chapter 4.6.7 @@ -83,10 +84,10 @@ func (p DetailPci) DetailType() DetailType { return DetailTypePci } -func NewDetailPci(pci C.hd_detail_pci_t) (Detail, error) { +func NewDetailPci(pci C.hd_detail_pci_t) (*DetailPci, error) { data := pci.data - return DetailPci{ + return &DetailPci{ Type: DetailTypePci, Data: hex.EncodeToString(C.GoBytes(unsafe.Pointer(&data.data), 256)), DataLength: uint(data.data_len), @@ -113,8 +114,8 @@ func NewDetailPci(pci C.hd_detail_pci_t) (Detail, error) { AddressFlags: [7]uint(ReadUintArray(unsafe.Pointer(&data.addr_flags), 7)), RomBaseAddress: uint64(data.rom_base_addr), RomBaseLength: uint64(data.rom_base_len), - SysfsId: C.GoString(data.sysfs_id), - SysfsBusId: C.GoString(data.sysfs_bus_id), + SysfsID: C.GoString(data.sysfs_id), + SysfsBusID: C.GoString(data.sysfs_bus_id), ModuleAlias: C.GoString(data.modalias), Label: C.GoString(data.label), }, nil diff --git a/pkg/hwinfo/detail_sys.go b/pkg/hwinfo/detail_sys.go index 0a319e9..dc6fccb 100644 --- a/pkg/hwinfo/detail_sys.go +++ b/pkg/hwinfo/detail_sys.go @@ -21,9 +21,9 @@ func (d DetailSys) DetailType() DetailType { return DetailTypeSys } -func NewDetailSys(sys C.hd_detail_sys_t) (Detail, error) { +func NewDetailSys(sys C.hd_detail_sys_t) (*DetailSys, error) { data := sys.data - return DetailSys{ + return &DetailSys{ Type: DetailTypeSys, SystemType: C.GoString(data.system_type), Generation: C.GoString(data.generation), diff --git a/pkg/hwinfo/detail_usb.go b/pkg/hwinfo/detail_usb.go index 5a468cf..530a646 100644 --- a/pkg/hwinfo/detail_usb.go +++ b/pkg/hwinfo/detail_usb.go @@ -38,12 +38,12 @@ const ( type DetailUsb struct { Type DetailType `json:"-"` - DeviceClass Id `json:"device_class"` - DeviceSubclass Id `json:"device_subclass"` + DeviceClass ID `json:"device_class"` + DeviceSubclass ID `json:"device_subclass"` DeviceProtocol int `json:"device_protocol"` - InterfaceClass Id `json:"interface_class"` - InterfaceSubclass Id `json:"interface_subclass"` + InterfaceClass ID `json:"interface_class"` + InterfaceSubclass ID `json:"interface_subclass"` InterfaceProtocol int `json:"interface_protocol"` InterfaceNumber int `json:"interface_number"` InterfaceAlternateSetting int `json:"interface_alternate_setting"` @@ -52,8 +52,8 @@ type DetailUsb struct { } type DetailUsbInterfaceAssociation struct { - FunctionClass Id `json:"function_class"` - FunctionSubclass Id `json:"function_subclass"` + FunctionClass ID `json:"function_class"` + FunctionSubclass ID `json:"function_subclass"` FunctionProtocol int `json:"function_protocol"` InterfaceCount int `json:"interface_count"` FirstInterface int `json:"first_interface"` @@ -63,33 +63,33 @@ func (d DetailUsb) DetailType() DetailType { return DetailTypeUsb } -func NewDetailUsb(usb C.hd_detail_usb_t) (Detail, error) { +func NewDetailUsb(usb C.hd_detail_usb_t) (*DetailUsb, error) { data := usb.data if data.next != nil { println("usb next is not nil") } - detail := DetailUsb{ + detail := &DetailUsb{ Type: DetailTypeUsb, - DeviceClass: Id{ - Type: IdTagUsb, + DeviceClass: ID{ + Type: IDTagUsb, Value: uint16(data.d_cls), Name: UsbClass(data.d_cls).String(), }, - DeviceSubclass: Id{ - Type: IdTagUsb, + DeviceSubclass: ID{ + Type: IDTagUsb, Value: uint16(data.d_sub), Name: UsbClass(data.d_sub).String(), }, DeviceProtocol: int(data.d_prot), - InterfaceClass: Id{ - Type: IdTagUsb, + InterfaceClass: ID{ + Type: IDTagUsb, Value: uint16(data.i_cls), Name: UsbClass(data.i_cls).String(), }, - InterfaceSubclass: Id{ - Type: IdTagUsb, + InterfaceSubclass: ID{ + Type: IDTagUsb, Value: uint16(data.i_sub), Name: UsbClass(data.i_sub).String(), }, @@ -103,13 +103,13 @@ func NewDetailUsb(usb C.hd_detail_usb_t) (Detail, error) { // the audio input interface together. if data.iad_i_count > 0 { detail.InterfaceAssociation = &DetailUsbInterfaceAssociation{ - FunctionClass: Id{ - Type: IdTagUsb, + FunctionClass: ID{ + Type: IDTagUsb, Value: uint16(data.iad_f_cls), Name: UsbClass(data.iad_f_cls).String(), }, - FunctionSubclass: Id{ - Type: IdTagUsb, + FunctionSubclass: ID{ + Type: IDTagUsb, Value: uint16(data.iad_f_sub), Name: UsbClass(data.iad_f_sub).String(), }, diff --git a/pkg/hwinfo/driver_info.go b/pkg/hwinfo/driver_info.go index e6d3b7a..95a54a7 100644 --- a/pkg/hwinfo/driver_info.go +++ b/pkg/hwinfo/driver_info.go @@ -37,27 +37,30 @@ type DriverInfo interface { DriverInfoType() DriverInfoType } -func NewDriverInfo(info *C.driver_info_t) (DriverInfo, error) { +//nolint:ireturn +func NewDriverInfo(info *C.driver_info_t) (result DriverInfo, err error) { if info == nil { - return nil, nil + return result, err } - infoType := DriverInfoType(C.driver_info_get_type(info)) - switch infoType { + + switch DriverInfoType(C.driver_info_get_type(info)) { case DriverInfoTypeModule: - return NewDriverInfoModule(C.driver_info_get_module(info)), nil + result, err = NewDriverInfoModule(C.driver_info_get_module(info)), nil case DriverInfoTypeMouse: - return NewDriverInfoMouse(C.driver_info_get_mouse(info)), nil + result, err = NewDriverInfoMouse(C.driver_info_get_mouse(info)), nil case DriverInfoTypeDisplay: - return NewDriverInfoDisplay(C.driver_info_get_display(info)), nil + result, err = NewDriverInfoDisplay(C.driver_info_get_display(info)), nil case DriverInfoTypeKeyboard: - return NewDriverInfoKeyboard(C.driver_info_get_kbd(info)), nil + result, err = NewDriverInfoKeyboard(C.driver_info_get_kbd(info)), nil case DriverInfoTypeDsl: - return NewDriverInfoDsl(C.driver_info_get_dsl(info)), nil + result, err = NewDriverInfoDsl(C.driver_info_get_dsl(info)), nil case DriverInfoTypeIsdn: - return NewDriverInfoIsdn(C.driver_info_get_isdn(info)), nil + result, err = NewDriverInfoIsdn(C.driver_info_get_isdn(info)), nil case DriverInfoTypeX11: - return NewDriverInfoX11(C.driver_info_get_x11(info)), nil + result, err = NewDriverInfoX11(C.driver_info_get_x11(info)), nil default: - return nil, errors.New("unknown driver info type") + err = errors.New("unknown driver info type") } + + return result, err } diff --git a/pkg/hwinfo/driver_info_display.go b/pkg/hwinfo/driver_info_display.go index 04021d7..a2bf495 100644 --- a/pkg/hwinfo/driver_info_display.go +++ b/pkg/hwinfo/driver_info_display.go @@ -9,8 +9,8 @@ import "C" type DriverInfoDisplay struct { Type DriverInfoType `json:"type,omitempty"` // actual driver database entries - DbEntry0 []string `json:"db_entry_0,omitempty"` - DbEntry1 []string `json:"db_entry_1,omitempty"` + DBEntry0 []string `json:"db_entry_0,omitempty"` + DBEntry1 []string `json:"db_entry_1,omitempty"` Width uint `json:"width"` Height uint `json:"height"` @@ -30,8 +30,8 @@ func (d DriverInfoDisplay) DriverInfoType() DriverInfoType { func NewDriverInfoDisplay(info C.driver_info_display_t) DriverInfoDisplay { return DriverInfoDisplay{ Type: DriverInfoTypeDisplay, - DbEntry0: ReadStringList(info.hddb0), - DbEntry1: ReadStringList(info.hddb1), + DBEntry0: ReadStringList(info.hddb0), + DBEntry1: ReadStringList(info.hddb1), Width: uint(info.width), Height: uint(info.height), VerticalSync: SyncRange{ diff --git a/pkg/hwinfo/driver_info_dsl.go b/pkg/hwinfo/driver_info_dsl.go index 92fb120..58fb2ec 100644 --- a/pkg/hwinfo/driver_info_dsl.go +++ b/pkg/hwinfo/driver_info_dsl.go @@ -9,8 +9,8 @@ import "C" type DriverInfoDsl struct { Type DriverInfoType `json:"type,omitempty"` // actual driver database entries - DbEntry0 []string `json:"db_entry_0,omitempty"` - DbEntry1 []string `json:"db_entry_1,omitempty"` + DBEntry0 []string `json:"db_entry_0,omitempty"` + DBEntry1 []string `json:"db_entry_1,omitempty"` Mode string `json:"mode,omitempty"` // DSL driver types Name string `json:"name,omitempty"` // DSL driver name @@ -23,8 +23,8 @@ func (d DriverInfoDsl) DriverInfoType() DriverInfoType { func NewDriverInfoDsl(info C.driver_info_dsl_t) DriverInfoDsl { return DriverInfoDsl{ Type: DriverInfoTypeDsl, - DbEntry0: ReadStringList(info.hddb0), - DbEntry1: ReadStringList(info.hddb1), + DBEntry0: ReadStringList(info.hddb0), + DBEntry1: ReadStringList(info.hddb1), Mode: C.GoString(info.mode), Name: C.GoString(info.name), } diff --git a/pkg/hwinfo/driver_info_isdn.go b/pkg/hwinfo/driver_info_isdn.go index 36d896a..24b60dd 100644 --- a/pkg/hwinfo/driver_info_isdn.go +++ b/pkg/hwinfo/driver_info_isdn.go @@ -9,8 +9,8 @@ import "C" type DriverInfoIsdn struct { Type DriverInfoType `json:"type,omitempty"` // actual driver database entries - DbEntry0 []string `json:"db_entry_0,omitempty"` - DbEntry1 []string `json:"db_entry_1,omitempty"` + DBEntry0 []string `json:"db_entry_0,omitempty"` + DBEntry1 []string `json:"db_entry_1,omitempty"` I4lType int `json:"i4l_type"` I4lSubtype int `json:"i4l_sub_type"` @@ -25,8 +25,8 @@ func (d DriverInfoIsdn) DriverInfoType() DriverInfoType { func NewDriverInfoIsdn(info C.driver_info_isdn_t) DriverInfoIsdn { return DriverInfoIsdn{ Type: DriverInfoTypeIsdn, - DbEntry0: ReadStringList(info.hddb0), - DbEntry1: ReadStringList(info.hddb1), + DBEntry0: ReadStringList(info.hddb0), + DBEntry1: ReadStringList(info.hddb1), I4lType: int(info.i4l_type), I4lSubtype: int(info.i4l_subtype), I4lName: C.GoString(info.i4l_name), diff --git a/pkg/hwinfo/driver_info_keyboard.go b/pkg/hwinfo/driver_info_keyboard.go index 3bcf289..c93ff0b 100644 --- a/pkg/hwinfo/driver_info_keyboard.go +++ b/pkg/hwinfo/driver_info_keyboard.go @@ -9,8 +9,8 @@ import "C" type DriverInfoKeyboard struct { Type DriverInfoType `json:"type,omitempty"` // actual driver database entries - DbEntry0 []string `json:"db_entry_0,omitempty"` - DbEntry1 []string `json:"db_entry_1,omitempty"` + DBEntry0 []string `json:"db_entry_0,omitempty"` + DBEntry1 []string `json:"db_entry_1,omitempty"` XkbRules string `json:"xkb_rules,omitempty"` XkbModel string `json:"xkb_model,omitempty"` @@ -25,8 +25,8 @@ func (d DriverInfoKeyboard) DriverInfoType() DriverInfoType { func NewDriverInfoKeyboard(info C.driver_info_kbd_t) DriverInfoKeyboard { return DriverInfoKeyboard{ Type: DriverInfoTypeKeyboard, - DbEntry0: ReadStringList(info.hddb0), - DbEntry1: ReadStringList(info.hddb1), + DBEntry0: ReadStringList(info.hddb0), + DBEntry1: ReadStringList(info.hddb1), XkbRules: C.GoString(info.XkbRules), XkbModel: C.GoString(info.XkbModel), XkbLayout: C.GoString(info.XkbLayout), diff --git a/pkg/hwinfo/driver_info_module.go b/pkg/hwinfo/driver_info_module.go index 50c405f..0ff0fa9 100644 --- a/pkg/hwinfo/driver_info_module.go +++ b/pkg/hwinfo/driver_info_module.go @@ -14,8 +14,8 @@ import "C" type DriverInfoModule struct { Type DriverInfoType `json:"type,omitempty"` // actual driver database entries - DbEntry0 []string `json:"db_entry_0,omitempty"` - DbEntry1 []string `json:"db_entry_1,omitempty"` + DBEntry0 []string `json:"db_entry_0,omitempty"` + DBEntry1 []string `json:"db_entry_1,omitempty"` Active bool `json:"active"` // if the module is currently active Modprobe bool `json:"modprobe"` // modprobe or insmod @@ -31,8 +31,8 @@ func (d DriverInfoModule) DriverInfoType() DriverInfoType { func NewDriverInfoModule(info C.driver_info_module_t) DriverInfoModule { return DriverInfoModule{ Type: DriverInfoTypeModule, - DbEntry0: ReadStringList(info.hddb0), - DbEntry1: ReadStringList(info.hddb1), + DBEntry0: ReadStringList(info.hddb0), + DBEntry1: ReadStringList(info.hddb1), Active: bool(C.driver_info_module_is_active(info)), Modprobe: bool(C.driver_info_module_is_modprobe(info)), Names: ReadStringList(info.names), diff --git a/pkg/hwinfo/driver_info_mouse.go b/pkg/hwinfo/driver_info_mouse.go index b77b027..241c630 100644 --- a/pkg/hwinfo/driver_info_mouse.go +++ b/pkg/hwinfo/driver_info_mouse.go @@ -9,8 +9,8 @@ import "C" type DriverInfoMouse struct { Type DriverInfoType `json:"type,omitempty"` // actual driver database entries - DbEntry0 []string `json:"db_entry_0,omitempty"` - DbEntry1 []string `json:"db_entry_1,omitempty"` + DBEntry0 []string `json:"db_entry_0,omitempty"` + DBEntry1 []string `json:"db_entry_1,omitempty"` XF86 string `json:"xf86,omitempty"` // XF86 protocol name GPM string `json:"gpm,omitempty"` // dto, gpm @@ -25,8 +25,8 @@ func (d DriverInfoMouse) DriverInfoType() DriverInfoType { func NewDriverInfoMouse(info C.driver_info_mouse_t) DriverInfoMouse { return DriverInfoMouse{ Type: DriverInfoTypeMouse, - DbEntry0: ReadStringList(info.hddb0), - DbEntry1: ReadStringList(info.hddb1), + DBEntry0: ReadStringList(info.hddb0), + DBEntry1: ReadStringList(info.hddb1), XF86: C.GoString(info.xf86), GPM: C.GoString(info.gpm), Buttons: int(info.buttons), diff --git a/pkg/hwinfo/driver_info_x11.go b/pkg/hwinfo/driver_info_x11.go index 65369df..f9b1f86 100644 --- a/pkg/hwinfo/driver_info_x11.go +++ b/pkg/hwinfo/driver_info_x11.go @@ -19,8 +19,8 @@ import "C" type DriverInfoX11 struct { Type DriverInfoType `json:"type,omitempty"` // actual driver database entries - DbEntry0 []string `json:"db_entry_0,omitempty"` - DbEntry1 []string `json:"db_entry_1,omitempty"` + DBEntry0 []string `json:"db_entry_0,omitempty"` + DBEntry1 []string `json:"db_entry_1,omitempty"` Server string `json:"server,omitempty"` // the server/module name XF86Version string `json:"xf86_version,omitempty"` // XFree86 version (3 or 4) @@ -48,8 +48,8 @@ func (d DriverInfoX11) DriverInfoType() DriverInfoType { func NewDriverInfoX11(info C.driver_info_x11_t) DriverInfoX11 { result := DriverInfoX11{ Type: DriverInfoTypeX11, - DbEntry0: ReadStringList(info.hddb0), - DbEntry1: ReadStringList(info.hddb1), + DBEntry0: ReadStringList(info.hddb0), + DBEntry1: ReadStringList(info.hddb1), Server: C.GoString(info.server), XF86Version: C.GoString(info.xf86_ver), Supports3D: bool(C.driver_info_x11_supports_3d(info)), diff --git a/pkg/hwinfo/hardware.go b/pkg/hwinfo/hardware.go index 531fe6e..fa5dd40 100644 --- a/pkg/hwinfo/hardware.go +++ b/pkg/hwinfo/hardware.go @@ -1,7 +1,5 @@ package hwinfo -import "C" - /* #cgo pkg-config: hwinfo #include @@ -19,6 +17,7 @@ import ( //go:generate enumer -type=ProbeFeature -json -transform=snake -trimprefix ProbeFeature -output=./hardware_enum_probe_feature.go type ProbeFeature uint +//nolint:revive,stylecheck const ( ProbeFeatureMemory ProbeFeature = iota + 1 ProbeFeaturePci @@ -122,6 +121,7 @@ const ( //go:generate enumer -type=HardwareClass -json -transform=snake -trimprefix HardwareClass -output=./hardware_enum_hardware_class.go type HardwareClass uint +//nolint:revive,stylecheck const ( HardwareClassNone HardwareClass = iota HardwareClassSystem @@ -220,8 +220,8 @@ type DeviceNumber struct { Type int `json:"type"` // Major identifies the driver for a device. Major uint `json:"major"` - // Minor is used by the device driver to distinguish between different devices it controls, or different instances of the - // same device. + // Minor is used by the device driver to distinguish between different devices it controls, or different instances + // of the same device. Minor uint `json:"minor"` Range uint `json:"range"` } @@ -275,59 +275,60 @@ type HardwareDevice struct { ClassList []HardwareClass `json:"class_list,omitempty"` // BusType represents the type of bus to which the hardware device is connected. - BusType *Id `json:"bus_type,omitempty"` + BusType *ID `json:"bus_type,omitempty"` // Slot represents a bus and slot number for the hardware device. Slot *Slot `json:"slot,omitempty"` // BaseClass specifies the base classification of the hardware device. - BaseClass *Id `json:"base_class,omitempty"` + BaseClass *ID `json:"base_class,omitempty"` - // SubClass represents the specific subclass of the hardware component, providing more granular identification within its class. - SubClass *Id `json:"sub_class,omitempty"` + // SubClass represents the specific subclass of the hardware component, providing more granular identification + // within its class. + SubClass *ID `json:"sub_class,omitempty"` // PciInterface specifies the PCI interface identifier of the hardware device. - PciInterface *Id `json:"pci_interface,omitempty"` + PciInterface *ID `json:"pci_interface,omitempty"` // Vendor represents the vendor ID of the hardware device. - Vendor *Id `json:"vendor,omitempty"` + Vendor *ID `json:"vendor,omitempty"` // SubVendor represents the ID of the subsystem vendor. - SubVendor *Id `json:"sub_vendor,omitempty"` + SubVendor *ID `json:"sub_vendor,omitempty"` // Device represents the unique identifier for the hardware device. - Device *Id `json:"device,omitempty"` + Device *ID `json:"device,omitempty"` // SubDevice represents the identifier of a sub-device in the hardware - SubDevice *Id `json:"sub_device,omitempty"` + SubDevice *ID `json:"sub_device,omitempty"` // Revision specifies the hardware revision identifier. - Revision *Id `json:"revision,omitempty"` + Revision *ID `json:"revision,omitempty"` Serial string `json:"serial,omitempty"` // CompatVendor is a vendor id and name of some compatible hardware. // Used mainly for ISA-PnP devices. - CompatVendor *Id `json:"compat_vendor,omitempty"` + CompatVendor *ID `json:"compat_vendor,omitempty"` // CompatDevice is a device id and name of some compatible hardware. // Used mainly for ISA-PnP devices. - CompatDevice *Id `json:"compat_device,omitempty"` + CompatDevice *ID `json:"compat_device,omitempty"` // Model is a combination of vendor and device names. Some heuristics are used to make it more presentable. Model string `json:"model,omitempty"` - // SysfsId is a sysfs entry for this hardware, if any. - SysfsId string `json:"sysfs_id,omitempty"` + // SysfsID is a sysfs entry for this hardware, if any. + SysfsID string `json:"sysfs_id,omitempty"` - // SysfsBusId is a sysfs bus entry for this hardware, if any. - SysfsBusId string `json:"sysfs_bus_id,omitempty"` + // SysfsBusID is a sysfs bus entry for this hardware, if any. + SysfsBusID string `json:"sysfs_bus_id,omitempty"` // SysfsDeviceLink is the string path to the system file system (sysfs) link for this hardware device. SysfsDeviceLink string `json:"sysfs_device_link,omitempty"` - // SysfsIOMMUGroupId represents the IOMMU group ID associated with the hardware device in sysfs, if any. - SysfsIOMMUGroupId *IOMMUGroup `json:"sysfs_iommu_group_id,omitempty"` + // SysfsIOMMUGroupID represents the IOMMU group ID associated with the hardware device in sysfs, if any. + SysfsIOMMUGroupID *IOMMUGroup `json:"sysfs_iommu_group_id,omitempty"` // UnixDeviceName is a path to a device file used to access this hardware. // Normally something below /dev. @@ -351,10 +352,10 @@ type HardwareDevice struct { // UnixDeviceNumber2 is an alternative device type and number according to sysfs. UnixDeviceNumber2 *DeviceNumber `json:"unix_device_number2,omitempty"` - // RomId represents a BIOS/PROM id. + // RomID represents a BIOS/PROM id. // Where appropriate, this is a special BIOS/PROM id (e.g. "0x80" for the first harddisk on Intel-PCs). // CHPID for s390. - RomId string `json:"rom_id,omitempty"` + RomID string `json:"rom_id,omitempty"` // Udi is a HAL unique device identifier. Udi string `json:"udi,omitempty"` @@ -372,8 +373,8 @@ type HardwareDevice struct { Hotplug *Hotplug `json:"hotplug,omitempty"` // HotplugSlot indicates the slot this device is connected to, if any (e.g. PCMCIA socket). - // Counts are 1-based. - HotplugSlot uint `json:"hotplug_slot,omitempty"` // slot the hotplug device is connected to (e.g. PCMCIA socket), count is 1-based (0: no info available) + // Counts are 1-based (0: no info available). + HotplugSlot uint `json:"hotplug_slot,omitempty"` // Driver is the currently active driver, if any. Driver string `json:"driver,omitempty"` @@ -390,10 +391,11 @@ type HardwareDevice struct { // DriverInfo is available driver information for the currently active driver, if any. DriverInfo DriverInfo `json:"driver_info,omitempty"` // device driver info - // UsbGuid is a USB Global Unique Identifier. + // UsbGUID is a USB Global Unique Identifier. // Available for USB devices. - // This may be set even if the bus type is not USB (e.g. USB storage devices will have bus set to SCSI due to SCSI emulation) - UsbGuid string `json:"usb_guid,omitempty"` // USB Global Unique Identifier. + // This may be set even if the bus type is not USB (e.g. USB storage devices will have bus set to SCSI due to SCSI + // emulation) + UsbGUID string `json:"usb_guid,omitempty"` // USB Global Unique Identifier. // todo hal_prop // todo persistent_prop @@ -407,7 +409,7 @@ type HardwareDevice struct { func NewHardwareDevice(hd *C.hd_t) (*HardwareDevice, error) { if hd == nil { - return nil, nil + return nil, fmt.Errorf("hd is nil") } resources, err := NewResources(hd) @@ -428,7 +430,7 @@ func NewHardwareDevice(hd *C.hd_t) (*HardwareDevice, error) { hwClass := HardwareClass(hd.hw_class) if hwClass == HardwareClassCpu { - model = stripCpuFreq(model) + model = stripCPUFreq(model) } var hwClassList []HardwareClass @@ -441,30 +443,30 @@ func NewHardwareDevice(hd *C.hd_t) (*HardwareDevice, error) { result := &HardwareDevice{ Index: uint(hd.idx), AttachedTo: uint(hd.attached_to), - BusType: NewId(hd.bus), - BaseClass: NewId(hd.base_class), - SubClass: NewId(hd.sub_class), - PciInterface: NewId(hd.prog_if), - Vendor: NewId(hd.vendor), - SubVendor: NewId(hd.sub_vendor), - Device: NewId(hd.device), - SubDevice: NewId(hd.sub_device), - Revision: NewId(hd.revision), + BusType: NewID(hd.bus), + BaseClass: NewID(hd.base_class), + SubClass: NewID(hd.sub_class), + PciInterface: NewID(hd.prog_if), + Vendor: NewID(hd.vendor), + SubVendor: NewID(hd.sub_vendor), + Device: NewID(hd.device), + SubDevice: NewID(hd.sub_device), + Revision: NewID(hd.revision), Serial: C.GoString(hd.serial), - CompatVendor: NewId(hd.compat_vendor), - CompatDevice: NewId(hd.compat_device), + CompatVendor: NewID(hd.compat_vendor), + CompatDevice: NewID(hd.compat_device), Class: hwClass, ClassList: hwClassList, Model: model, - SysfsId: C.GoString(hd.sysfs_id), - SysfsBusId: C.GoString(hd.sysfs_bus_id), + SysfsID: C.GoString(hd.sysfs_id), + SysfsBusID: C.GoString(hd.sysfs_bus_id), SysfsDeviceLink: C.GoString(hd.sysfs_device_link), UnixDeviceName: C.GoString(hd.unix_dev_name), UnixDeviceNumber: NewDeviceNumber(hd.unix_dev_num), UnixDeviceName2: C.GoString(hd.unix_dev_name2), UnixDeviceNames: ReadStringList(hd.unix_dev_names), UnixDeviceNumber2: NewDeviceNumber(hd.unix_dev_num2), - RomId: C.GoString(hd.rom_id), + RomID: C.GoString(hd.rom_id), Udi: C.GoString(hd.udi), ParentUdi: C.GoString(hd.parent_udi), Resources: resources, @@ -473,7 +475,7 @@ func NewHardwareDevice(hd *C.hd_t) (*HardwareDevice, error) { DriverModule: C.GoString(hd.driver_module), Drivers: ReadStringList(hd.drivers), DriverModules: ReadStringList(hd.driver_modules), - UsbGuid: C.GoString(hd.usb_guid), + UsbGUID: C.GoString(hd.usb_guid), DriverInfo: driverInfo, ModuleAlias: C.GoString(hd.modalias), Label: C.GoString(hd.label), diff --git a/pkg/hwinfo/hwinfo.go b/pkg/hwinfo/hwinfo.go index ad2724d..b3216af 100644 --- a/pkg/hwinfo/hwinfo.go +++ b/pkg/hwinfo/hwinfo.go @@ -33,7 +33,7 @@ func excludeDevice(item *HardwareDevice) bool { // Scan returns a list of SMBIOS entries and detected hardware devices based on the provided probe features. func Scan(probes []ProbeFeature) ([]Smbios, []HardwareDevice, error) { // initialise the struct to hold scan data - data := (*C.hd_data_t)(unsafe.Pointer(C.calloc(1, C.size_t(unsafe.Sizeof(C.hd_data_t{}))))) + data := (*C.hd_data_t)(C.calloc(1, C.size_t(unsafe.Sizeof(C.hd_data_t{})))) // ProbeFeatureInt needs to always be set, otherwise we don't get pci and usb vendor id lookups. // https://github.com/openSUSE/hwinfo/blob/c87f449f1d4882c71b0a1e6dc80638224a5baeed/src/hd/hd.c#L597-L605 @@ -61,14 +61,16 @@ func Scan(probes []ProbeFeature) ([]Smbios, []HardwareDevice, error) { var hardwareItems []HardwareDevice for hd := data.hd; hd != nil; hd = hd.next { - if item, err := NewHardwareDevice(hd); err != nil { + item, err := NewHardwareDevice(hd) + if err != nil { return nil, nil, err - } else { - if excludeDevice(item) { - continue - } - hardwareItems = append(hardwareItems, *item) } + + if excludeDevice(item) { + continue + } + + hardwareItems = append(hardwareItems, *item) } return smbiosItems, hardwareItems, nil diff --git a/pkg/hwinfo/id.go b/pkg/hwinfo/id.go index 1ad3680..b5a29a2 100644 --- a/pkg/hwinfo/id.go +++ b/pkg/hwinfo/id.go @@ -12,77 +12,40 @@ import ( "slices" ) -//go:generate enumer -type=Vendor -json -transform=snake -trimprefix Vendor -output=./id_vendor_enum.go -type Vendor uint16 +//go:generate enumer -type=IDTag -json -transform=snake -trimprefix IDTag -output=./id_tag_enum.go +type IDTag byte const ( - VendorBroadcom Vendor = 0x14e4 - VendorRedHat Vendor = 0x1af4 - VendorIntelCorporation Vendor = 0x8086 + IDTagPci IDTag = iota + 1 + IDTagEisa + IDTagUsb + IDTagSpecial + IDTagPcmcia + IDTagSdio ) -var ( - // DevicesSTA list taken from https://github.com/NixOS/nixpkgs/blob/dac9cdf8c930c0af98a63cbfe8005546ba0125fb/nixos/modules/installer/tools/nixos-generate-config.pl#L152-L158 - DevicesSTA = []uint16{ - 0x4311, 0x4312, 0x4313, 0x4315, 0x4327, 0x4328, - 0x4329, 0x432a, 0x432b, 0x432c, 0x432d, 0x4353, - 0x4357, 0x4358, 0x4359, 0x4331, 0x43a0, 0x43b1, - } - - // DevicesFullMac list taken from https://wireless.wiki.kernel.org/en/users/Drivers/brcm80211#brcmfmac - DevicesFullMac = []uint16{ - 0x43a3, 0x43df, 0x43ec, 0x43d3, 0x43d9, 0x43e9, - 0x43ba, 0x43bb, 0x43bc, 0xaa52, 0x43ca, 0x43cb, - 0x43cc, 0x43c3, 0x43c4, 0x43c5, - } - - DevicesVirtioSCSI = []uint16{ - 0x1004, 0x1048, - } - - DevicesIntel2200BG = []uint16{ - 0x1043, 0x104f, 0x4220, 0x4221, 0x4223, 0x4224, - } - - DevicesIntel3945ABG = []uint16{ - 0x4229, 0x4230, 0x4222, 0x4227, - } -) - -//go:generate enumer -type=IdTag -json -transform=snake -trimprefix IdTag -output=./id_tag_enum.go -type IdTag byte - -const ( - IdTagPci IdTag = iota + 1 - IdTagEisa - IdTagUsb - IdTagSpecial - IdTagPcmcia - IdTagSdio -) - -type Id struct { - Type IdTag +type ID struct { + Type IDTag Value uint16 // Name (if any) Name string } -type idJson struct { +type idJSON struct { Hex string `json:"hex,omitempty"` Name string `json:"name,omitempty"` Value uint16 `json:"value"` } -func (i Id) MarshalJSON() ([]byte, error) { +func (i ID) MarshalJSON() ([]byte, error) { switch i.Type { - case IdTagSpecial: + case IDTagSpecial: return json.Marshal(i.Name) - case 0, IdTagPci, IdTagEisa, IdTagUsb, IdTagPcmcia, IdTagSdio: + case 0, IDTagPci, IDTagEisa, IDTagUsb, IDTagPcmcia, IDTagSdio: - return json.Marshal(idJson{ + return json.Marshal(idJSON{ Hex: fmt.Sprintf("%04x", i.Value), Name: i.Name, Value: i.Value, @@ -93,34 +56,25 @@ func (i Id) MarshalJSON() ([]byte, error) { } } -func (i Id) IsEmpty() bool { +func (i ID) IsEmpty() bool { return i.Type == 0 && i.Value == 0 && (i.Name == "" || i.Name == "None") } -func (i Id) String() string { +func (i ID) String() string { return fmt.Sprintf("%d:%s", i.Value, i.Name) } -func (i Id) Is(ids ...uint16) bool { +func (i ID) Is(ids ...uint16) bool { return slices.Contains(ids, i.Value) } -func (i Id) IsVendor(vendors ...Vendor) bool { - for idx := range vendors { - if i.Value == uint16(vendors[idx]) { - return true - } - } - return false -} - -func NewId(id C.hd_id_t) *Id { - result := Id{ +func NewID(id C.hd_id_t) *ID { + result := ID{ /* - Id is actually a combination of some tag to differentiate the various id types and the real id. + ID is actually a combination of some tag to differentiate the various id types and the real id. We do the same thing as the ID_VALUE macro in hd.h to get the true value. */ - Type: IdTag((id.id >> 16) & 0xf), + Type: IDTag((id.id >> 16) & 0xf), Value: uint16(id.id), Name: C.GoString(id.name), } diff --git a/pkg/hwinfo/id_tag_enum.go b/pkg/hwinfo/id_tag_enum.go index f5c7fc2..b43a76f 100644 --- a/pkg/hwinfo/id_tag_enum.go +++ b/pkg/hwinfo/id_tag_enum.go @@ -1,4 +1,4 @@ -// Code generated by "enumer -type=IdTag -json -transform=snake -trimprefix IdTag -output=./id_tag_enum.go"; DO NOT EDIT. +// Code generated by "enumer -type=IDTag -json -transform=snake -trimprefix IDTag -output=./id_tag_enum.go"; DO NOT EDIT. package hwinfo @@ -8,86 +8,86 @@ import ( "strings" ) -const _IdTagName = "pcieisausbspecialpcmciasdio" +const _IDTagName = "pcieisausbspecialpcmciasdio" -var _IdTagIndex = [...]uint8{0, 3, 7, 10, 17, 23, 27} +var _IDTagIndex = [...]uint8{0, 3, 7, 10, 17, 23, 27} -const _IdTagLowerName = "pcieisausbspecialpcmciasdio" +const _IDTagLowerName = "pcieisausbspecialpcmciasdio" -func (i IdTag) String() string { +func (i IDTag) String() string { i -= 1 - if i >= IdTag(len(_IdTagIndex)-1) { - return fmt.Sprintf("IdTag(%d)", i+1) + if i >= IDTag(len(_IDTagIndex)-1) { + return fmt.Sprintf("IDTag(%d)", i+1) } - return _IdTagName[_IdTagIndex[i]:_IdTagIndex[i+1]] + return _IDTagName[_IDTagIndex[i]:_IDTagIndex[i+1]] } // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. -func _IdTagNoOp() { +func _IDTagNoOp() { var x [1]struct{} - _ = x[IdTagPci-(1)] - _ = x[IdTagEisa-(2)] - _ = x[IdTagUsb-(3)] - _ = x[IdTagSpecial-(4)] - _ = x[IdTagPcmcia-(5)] - _ = x[IdTagSdio-(6)] + _ = x[IDTagPci-(1)] + _ = x[IDTagEisa-(2)] + _ = x[IDTagUsb-(3)] + _ = x[IDTagSpecial-(4)] + _ = x[IDTagPcmcia-(5)] + _ = x[IDTagSdio-(6)] } -var _IdTagValues = []IdTag{IdTagPci, IdTagEisa, IdTagUsb, IdTagSpecial, IdTagPcmcia, IdTagSdio} - -var _IdTagNameToValueMap = map[string]IdTag{ - _IdTagName[0:3]: IdTagPci, - _IdTagLowerName[0:3]: IdTagPci, - _IdTagName[3:7]: IdTagEisa, - _IdTagLowerName[3:7]: IdTagEisa, - _IdTagName[7:10]: IdTagUsb, - _IdTagLowerName[7:10]: IdTagUsb, - _IdTagName[10:17]: IdTagSpecial, - _IdTagLowerName[10:17]: IdTagSpecial, - _IdTagName[17:23]: IdTagPcmcia, - _IdTagLowerName[17:23]: IdTagPcmcia, - _IdTagName[23:27]: IdTagSdio, - _IdTagLowerName[23:27]: IdTagSdio, +var _IDTagValues = []IDTag{IDTagPci, IDTagEisa, IDTagUsb, IDTagSpecial, IDTagPcmcia, IDTagSdio} + +var _IDTagNameToValueMap = map[string]IDTag{ + _IDTagName[0:3]: IDTagPci, + _IDTagLowerName[0:3]: IDTagPci, + _IDTagName[3:7]: IDTagEisa, + _IDTagLowerName[3:7]: IDTagEisa, + _IDTagName[7:10]: IDTagUsb, + _IDTagLowerName[7:10]: IDTagUsb, + _IDTagName[10:17]: IDTagSpecial, + _IDTagLowerName[10:17]: IDTagSpecial, + _IDTagName[17:23]: IDTagPcmcia, + _IDTagLowerName[17:23]: IDTagPcmcia, + _IDTagName[23:27]: IDTagSdio, + _IDTagLowerName[23:27]: IDTagSdio, } -var _IdTagNames = []string{ - _IdTagName[0:3], - _IdTagName[3:7], - _IdTagName[7:10], - _IdTagName[10:17], - _IdTagName[17:23], - _IdTagName[23:27], +var _IDTagNames = []string{ + _IDTagName[0:3], + _IDTagName[3:7], + _IDTagName[7:10], + _IDTagName[10:17], + _IDTagName[17:23], + _IDTagName[23:27], } -// IdTagString retrieves an enum value from the enum constants string name. +// IDTagString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. -func IdTagString(s string) (IdTag, error) { - if val, ok := _IdTagNameToValueMap[s]; ok { +func IDTagString(s string) (IDTag, error) { + if val, ok := _IDTagNameToValueMap[s]; ok { return val, nil } - if val, ok := _IdTagNameToValueMap[strings.ToLower(s)]; ok { + if val, ok := _IDTagNameToValueMap[strings.ToLower(s)]; ok { return val, nil } - return 0, fmt.Errorf("%s does not belong to IdTag values", s) + return 0, fmt.Errorf("%s does not belong to IDTag values", s) } -// IdTagValues returns all values of the enum -func IdTagValues() []IdTag { - return _IdTagValues +// IDTagValues returns all values of the enum +func IDTagValues() []IDTag { + return _IDTagValues } -// IdTagStrings returns a slice of all String values of the enum -func IdTagStrings() []string { - strs := make([]string, len(_IdTagNames)) - copy(strs, _IdTagNames) +// IDTagStrings returns a slice of all String values of the enum +func IDTagStrings() []string { + strs := make([]string, len(_IDTagNames)) + copy(strs, _IDTagNames) return strs } -// IsAIdTag returns "true" if the value is listed in the enum definition. "false" otherwise -func (i IdTag) IsAIdTag() bool { - for _, v := range _IdTagValues { +// IsAIDTag returns "true" if the value is listed in the enum definition. "false" otherwise +func (i IDTag) IsAIDTag() bool { + for _, v := range _IDTagValues { if i == v { return true } @@ -95,19 +95,19 @@ func (i IdTag) IsAIdTag() bool { return false } -// MarshalJSON implements the json.Marshaler interface for IdTag -func (i IdTag) MarshalJSON() ([]byte, error) { +// MarshalJSON implements the json.Marshaler interface for IDTag +func (i IDTag) MarshalJSON() ([]byte, error) { return json.Marshal(i.String()) } -// UnmarshalJSON implements the json.Unmarshaler interface for IdTag -func (i *IdTag) UnmarshalJSON(data []byte) error { +// UnmarshalJSON implements the json.Unmarshaler interface for IDTag +func (i *IDTag) UnmarshalJSON(data []byte) error { var s string if err := json.Unmarshal(data, &s); err != nil { - return fmt.Errorf("IdTag should be a string, got %s", data) + return fmt.Errorf("IDTag should be a string, got %s", data) } var err error - *i, err = IdTagString(s) + *i, err = IDTagString(s) return err } diff --git a/pkg/hwinfo/id_vendor_enum.go b/pkg/hwinfo/id_vendor_enum.go deleted file mode 100644 index a8eaf1f..0000000 --- a/pkg/hwinfo/id_vendor_enum.go +++ /dev/null @@ -1,115 +0,0 @@ -// Code generated by "enumer -type=Vendor -json -transform=snake -trimprefix Vendor -output=./id_vendor_enum.go"; DO NOT EDIT. - -package hwinfo - -import ( - "encoding/json" - "fmt" - "strings" -) - -const ( - _VendorName_0 = "broadcom" - _VendorLowerName_0 = "broadcom" - _VendorName_1 = "red_hat" - _VendorLowerName_1 = "red_hat" - _VendorName_2 = "intel_corporation" - _VendorLowerName_2 = "intel_corporation" -) - -var ( - _VendorIndex_0 = [...]uint8{0, 8} - _VendorIndex_1 = [...]uint8{0, 7} - _VendorIndex_2 = [...]uint8{0, 17} -) - -func (i Vendor) String() string { - switch { - case i == 5348: - return _VendorName_0 - case i == 6900: - return _VendorName_1 - case i == 32902: - return _VendorName_2 - default: - return fmt.Sprintf("Vendor(%d)", i) - } -} - -// An "invalid array index" compiler error signifies that the constant values have changed. -// Re-run the stringer command to generate them again. -func _VendorNoOp() { - var x [1]struct{} - _ = x[VendorBroadcom-(5348)] - _ = x[VendorRedHat-(6900)] - _ = x[VendorIntelCorporation-(32902)] -} - -var _VendorValues = []Vendor{VendorBroadcom, VendorRedHat, VendorIntelCorporation} - -var _VendorNameToValueMap = map[string]Vendor{ - _VendorName_0[0:8]: VendorBroadcom, - _VendorLowerName_0[0:8]: VendorBroadcom, - _VendorName_1[0:7]: VendorRedHat, - _VendorLowerName_1[0:7]: VendorRedHat, - _VendorName_2[0:17]: VendorIntelCorporation, - _VendorLowerName_2[0:17]: VendorIntelCorporation, -} - -var _VendorNames = []string{ - _VendorName_0[0:8], - _VendorName_1[0:7], - _VendorName_2[0:17], -} - -// VendorString retrieves an enum value from the enum constants string name. -// Throws an error if the param is not part of the enum. -func VendorString(s string) (Vendor, error) { - if val, ok := _VendorNameToValueMap[s]; ok { - return val, nil - } - - if val, ok := _VendorNameToValueMap[strings.ToLower(s)]; ok { - return val, nil - } - return 0, fmt.Errorf("%s does not belong to Vendor values", s) -} - -// VendorValues returns all values of the enum -func VendorValues() []Vendor { - return _VendorValues -} - -// VendorStrings returns a slice of all String values of the enum -func VendorStrings() []string { - strs := make([]string, len(_VendorNames)) - copy(strs, _VendorNames) - return strs -} - -// IsAVendor returns "true" if the value is listed in the enum definition. "false" otherwise -func (i Vendor) IsAVendor() bool { - for _, v := range _VendorValues { - if i == v { - return true - } - } - return false -} - -// MarshalJSON implements the json.Marshaler interface for Vendor -func (i Vendor) MarshalJSON() ([]byte, error) { - return json.Marshal(i.String()) -} - -// UnmarshalJSON implements the json.Unmarshaler interface for Vendor -func (i *Vendor) UnmarshalJSON(data []byte) error { - var s string - if err := json.Unmarshal(data, &s); err != nil { - return fmt.Errorf("Vendor should be a string, got %s", data) - } - - var err error - *i, err = VendorString(s) - return err -} diff --git a/pkg/hwinfo/iommu.go b/pkg/hwinfo/iommu.go index 73d8728..1e07ccc 100644 --- a/pkg/hwinfo/iommu.go +++ b/pkg/hwinfo/iommu.go @@ -20,13 +20,13 @@ func IOMMUGroups() (map[string]IOMMUGroup, error) { } result := make(map[string]IOMMUGroup) - for _, group := range groups { + for _, group := range groups { if !group.IsDir() { return nil, fmt.Errorf("non-directory entry found in %s: %s", iommuSysfsPath, group.Name()) } - groupId, err := strconv.Atoi(group.Name()) + groupID, err := strconv.Atoi(group.Name()) if err != nil { return nil, fmt.Errorf("failed to parse iommu group id '%s': %w", group.Name(), err) } @@ -37,15 +37,17 @@ func IOMMUGroups() (map[string]IOMMUGroup, error) { if err != nil { return nil, fmt.Errorf("failed to read devices from iommu group %s: %w", devicesPath, err) } + for _, device := range devices { devicePath := filepath.Join(devicesPath, device.Name()) + resolvedPath, err := filepath.EvalSymlinks(devicePath) if err != nil { return nil, fmt.Errorf("failed to resolve device symlink '%s': %w", devicePath, err) } // sysfs id -> iommu group id - result[resolvedPath[4:]] = IOMMUGroup(groupId) + result[resolvedPath[4:]] = IOMMUGroup(groupID) } } diff --git a/pkg/hwinfo/resource.go b/pkg/hwinfo/resource.go index 2957434..0972ec6 100644 --- a/pkg/hwinfo/resource.go +++ b/pkg/hwinfo/resource.go @@ -46,52 +46,54 @@ const ( ResourceTypePhwaddr ) -func NewResource(res *C.hd_res_t) (Resource, error) { +//nolint:ireturn +func NewResource(res *C.hd_res_t) (result Resource, err error) { if res == nil { - return nil, nil + return result, err } resourceType := ResourceType(C.hd_res_get_type(res)) switch resourceType { case ResourceTypeFc: - return NewResourceFc(res, resourceType) + result, err = NewResourceFc(res, resourceType) case ResourceTypePhysMem: - return NewResourcePhysicalMemory(res, resourceType) + result, err = NewResourcePhysicalMemory(res, resourceType) case ResourceTypeMem: - return NewResourceMemory(res, resourceType) + result, err = NewResourceMemory(res, resourceType) case ResourceTypeIo: - return NewResourceIO(res, resourceType) + result, err = NewResourceIO(res, resourceType) case ResourceTypeIrq: - return NewResourceIrq(res, resourceType) + result, err = NewResourceIrq(res, resourceType) case ResourceTypeDma: - return NewResourceDma(res, resourceType) + result, err = NewResourceDma(res, resourceType) case ResourceTypeMonitor: - return NewResourceMonitor(res, resourceType) + result, err = NewResourceMonitor(res, resourceType) case ResourceTypeSize: - return NewResourceSize(res, resourceType) + result, err = NewResourceSize(res, resourceType) case ResourceTypeDiskGeo: - return NewResourceDiskGeo(res, resourceType) + result, err = NewResourceDiskGeo(res, resourceType) case ResourceTypeCache: - return NewResourceCache(res, resourceType) + result, err = NewResourceCache(res, resourceType) case ResourceTypeBaud: - return NewResourceBaud(res, resourceType) + result, err = NewResourceBaud(res, resourceType) case ResourceTypeInitStrings: - return NewResourceInitStrings(res, resourceType) + result, err = NewResourceInitStrings(res, resourceType) case ResourceTypePppdOption: - return NewResourcePppdOption(res, resourceType) + result, err = NewResourcePppdOption(res, resourceType) case ResourceTypeFramebuffer: - return NewResourceFrameBuffer(res, resourceType) + result, err = NewResourceFrameBuffer(res, resourceType) case ResourceTypeHwaddr, ResourceTypePhwaddr: - return NewResourceHardwareAddress(res, resourceType) + result, err = NewResourceHardwareAddress(res, resourceType) case ResourceTypeLink: // this is the link status of a network interface and can change when we plug/unplug a cable - return nil, nil case ResourceTypeWlan: - return NewResourceWlan(res, resourceType) + result, err = NewResourceWlan(res, resourceType) default: - return nil, fmt.Errorf("unexpected resource type: %v", resourceType) + err = fmt.Errorf("unexpected resource type: %v", resourceType) } + + return result, err } func NewResources(hd *C.hd_t) ([]Resource, error) { diff --git a/pkg/hwinfo/resource_baud.go b/pkg/hwinfo/resource_baud.go index e98bdd7..cc52105 100644 --- a/pkg/hwinfo/resource_baud.go +++ b/pkg/hwinfo/resource_baud.go @@ -25,7 +25,7 @@ func (r ResourceBaud) ResourceType() ResourceType { func NewResourceBaud(res *C.hd_res_t, resType ResourceType) (*ResourceBaud, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeBaud { diff --git a/pkg/hwinfo/resource_cache.go b/pkg/hwinfo/resource_cache.go index f9218e9..49b6e37 100644 --- a/pkg/hwinfo/resource_cache.go +++ b/pkg/hwinfo/resource_cache.go @@ -21,7 +21,7 @@ func (r ResourceCache) ResourceType() ResourceType { func NewResourceCache(res *C.hd_res_t, resType ResourceType) (*ResourceCache, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeCache { diff --git a/pkg/hwinfo/resource_disk_geo.go b/pkg/hwinfo/resource_disk_geo.go index 81e8f15..f5c7b38 100644 --- a/pkg/hwinfo/resource_disk_geo.go +++ b/pkg/hwinfo/resource_disk_geo.go @@ -35,7 +35,7 @@ func (r ResourceDiskGeo) ResourceType() ResourceType { func NewResourceDiskGeo(res *C.hd_res_t, resType ResourceType) (*ResourceDiskGeo, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeDiskGeo { diff --git a/pkg/hwinfo/resource_dma.go b/pkg/hwinfo/resource_dma.go index 2d472f2..d3605c9 100644 --- a/pkg/hwinfo/resource_dma.go +++ b/pkg/hwinfo/resource_dma.go @@ -27,7 +27,7 @@ func (r ResourceDma) ResourceType() ResourceType { func NewResourceDma(res *C.hd_res_t, resType ResourceType) (*ResourceDma, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeDma { diff --git a/pkg/hwinfo/resource_fc.go b/pkg/hwinfo/resource_fc.go index 50a5c02..4ad1060 100644 --- a/pkg/hwinfo/resource_fc.go +++ b/pkg/hwinfo/resource_fc.go @@ -21,11 +21,11 @@ type ResourceFc struct { Type ResourceType `json:"type"` WwpnOk bool `json:"wwpn_ok"` FcpLunOk bool `json:"fcp_lun_ok"` - PortIdOk bool `json:"port_id_ok"` + PortIDOk bool `json:"port_id_ok"` Wwpn uint64 `json:"wwpn"` FcpLun uint64 `json:"fcp_lun"` - PortId uint `json:"port_id"` - ControllerId byte `json:"controller_id"` + PortID uint `json:"port_id"` + ControllerID byte `json:"controller_id"` } func (r ResourceFc) ResourceType() ResourceType { @@ -34,7 +34,7 @@ func (r ResourceFc) ResourceType() ResourceType { func NewResourceFc(res *C.hd_res_t, resType ResourceType) (*ResourceFc, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeFc { @@ -46,10 +46,10 @@ func NewResourceFc(res *C.hd_res_t, resType ResourceType) (*ResourceFc, error) { return &ResourceFc{ WwpnOk: bool(C.hd_res_fc_get_wwpn_ok(fc)), FcpLunOk: bool(C.hd_res_fc_get_fcp_lun_ok(fc)), - PortIdOk: bool(C.hd_res_fc_get_port_id_ok(fc)), + PortIDOk: bool(C.hd_res_fc_get_port_id_ok(fc)), Wwpn: uint64(fc.wwpn), FcpLun: uint64(fc.fcp_lun), - PortId: uint(fc.port_id), - ControllerId: byte(*fc.controller_id), + PortID: uint(fc.port_id), + ControllerID: byte(*fc.controller_id), }, nil } diff --git a/pkg/hwinfo/resource_frame_buffer.go b/pkg/hwinfo/resource_frame_buffer.go index 56a0da3..3c041fb 100644 --- a/pkg/hwinfo/resource_frame_buffer.go +++ b/pkg/hwinfo/resource_frame_buffer.go @@ -25,7 +25,7 @@ func (r ResourceFrameBuffer) ResourceType() ResourceType { func NewResourceFrameBuffer(res *C.hd_res_t, resType ResourceType) (*ResourceFrameBuffer, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeFramebuffer { diff --git a/pkg/hwinfo/resource_hw_addr.go b/pkg/hwinfo/resource_hw_addr.go index 48c0367..b448af9 100644 --- a/pkg/hwinfo/resource_hw_addr.go +++ b/pkg/hwinfo/resource_hw_addr.go @@ -21,11 +21,14 @@ func (r ResourceHardwareAddress) ResourceType() ResourceType { func NewResourceHardwareAddress(res *C.hd_res_t, resType ResourceType) (*ResourceHardwareAddress, error) { if !(resType == ResourceTypeHwaddr || resType == ResourceTypePhwaddr) { - return nil, fmt.Errorf("invalid resource type %s, must be either %s or %s", resType, ResourceTypeHwaddr, ResourceTypePhwaddr) + return nil, fmt.Errorf( + "invalid resource type %s, must be either %s or %s", + resType, ResourceTypeHwaddr, ResourceTypePhwaddr, + ) } if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } hwaddr := C.hd_res_get_hwaddr(res) diff --git a/pkg/hwinfo/resource_init_strings.go b/pkg/hwinfo/resource_init_strings.go index cdd6853..5577fa5 100644 --- a/pkg/hwinfo/resource_init_strings.go +++ b/pkg/hwinfo/resource_init_strings.go @@ -22,7 +22,7 @@ func (r ResourceInitStrings) ResourceType() ResourceType { func NewResourceInitStrings(res *C.hd_res_t, resType ResourceType) (*ResourceInitStrings, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeInitStrings { diff --git a/pkg/hwinfo/resource_io.go b/pkg/hwinfo/resource_io.go index 57e3d5a..6eaeca2 100644 --- a/pkg/hwinfo/resource_io.go +++ b/pkg/hwinfo/resource_io.go @@ -29,7 +29,7 @@ func (r ResourceIO) ResourceType() ResourceType { func NewResourceIO(res *C.hd_res_t, resType ResourceType) (*ResourceIO, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeIo { diff --git a/pkg/hwinfo/resource_irq.go b/pkg/hwinfo/resource_irq.go index 14f583e..141be2b 100644 --- a/pkg/hwinfo/resource_irq.go +++ b/pkg/hwinfo/resource_irq.go @@ -27,7 +27,7 @@ func (r ResourceIrq) ResourceType() ResourceType { func NewResourceIrq(res *C.hd_res_t, resType ResourceType) (*ResourceIrq, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeIrq { diff --git a/pkg/hwinfo/resource_link.go b/pkg/hwinfo/resource_link.go index 78d259d..008126d 100644 --- a/pkg/hwinfo/resource_link.go +++ b/pkg/hwinfo/resource_link.go @@ -25,7 +25,7 @@ func (r ResourceLink) ResourceType() ResourceType { func NewResourceLink(res *C.hd_res_t, resType ResourceType) (*ResourceLink, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeLink { diff --git a/pkg/hwinfo/resource_mem.go b/pkg/hwinfo/resource_mem.go index 1ec5835..a31afd3 100644 --- a/pkg/hwinfo/resource_mem.go +++ b/pkg/hwinfo/resource_mem.go @@ -55,7 +55,7 @@ func (r ResourceMemory) ResourceType() ResourceType { func NewResourceMemory(res *C.hd_res_t, resType ResourceType) (*ResourceMemory, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeMem { diff --git a/pkg/hwinfo/resource_monitor.go b/pkg/hwinfo/resource_monitor.go index cba44d3..9c1e010 100644 --- a/pkg/hwinfo/resource_monitor.go +++ b/pkg/hwinfo/resource_monitor.go @@ -28,7 +28,7 @@ func (r ResourceMonitor) ResourceType() ResourceType { func NewResourceMonitor(res *C.hd_res_t, resType ResourceType) (*ResourceMonitor, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeMonitor { diff --git a/pkg/hwinfo/resource_phys_mem.go b/pkg/hwinfo/resource_phys_mem.go index 14df705..dc3b28e 100644 --- a/pkg/hwinfo/resource_phys_mem.go +++ b/pkg/hwinfo/resource_phys_mem.go @@ -21,7 +21,7 @@ func (r ResourcePhysicalMemory) ResourceType() ResourceType { func NewResourcePhysicalMemory(res *C.hd_res_t, resType ResourceType) (*ResourcePhysicalMemory, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypePhysMem { diff --git a/pkg/hwinfo/resource_pppd_option.go b/pkg/hwinfo/resource_pppd_option.go index e169e65..87f73a3 100644 --- a/pkg/hwinfo/resource_pppd_option.go +++ b/pkg/hwinfo/resource_pppd_option.go @@ -21,7 +21,7 @@ func (r ResourcePppdOption) ResourceType() ResourceType { func NewResourcePppdOption(res *C.hd_res_t, resType ResourceType) (*ResourcePppdOption, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypePppdOption { diff --git a/pkg/hwinfo/resource_size.go b/pkg/hwinfo/resource_size.go index 088e39a..8db92c7 100644 --- a/pkg/hwinfo/resource_size.go +++ b/pkg/hwinfo/resource_size.go @@ -39,7 +39,7 @@ func (r ResourceSize) ResourceType() ResourceType { func NewResourceSize(res *C.hd_res_t, resType ResourceType) (*ResourceSize, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeSize { diff --git a/pkg/hwinfo/resource_wlan.go b/pkg/hwinfo/resource_wlan.go index af73406..1e5322a 100644 --- a/pkg/hwinfo/resource_wlan.go +++ b/pkg/hwinfo/resource_wlan.go @@ -25,7 +25,7 @@ func (r ResourceWlan) ResourceType() ResourceType { func NewResourceWlan(res *C.hd_res_t, resType ResourceType) (*ResourceWlan, error) { if res == nil { - return nil, nil + return nil, fmt.Errorf("res is nil") } if resType != ResourceTypeWlan { diff --git a/pkg/hwinfo/smbios.go b/pkg/hwinfo/smbios.go index ec0a4d9..c470bd2 100644 --- a/pkg/hwinfo/smbios.go +++ b/pkg/hwinfo/smbios.go @@ -103,63 +103,61 @@ type Smbios interface { SmbiosType() SmbiosType } -func NewSmbios(smbios *C.hd_smbios_t) (Smbios, error) { +//nolint:ireturn +func NewSmbios(smbios *C.hd_smbios_t) (result Smbios, err error) { if smbios == nil { - return nil, nil + return result, err } - smbiosType := SmbiosType(C.hd_smbios_get_type(smbios)) - - switch smbiosType { + switch SmbiosType(C.hd_smbios_get_type(smbios)) { case SmbiosTypeBios: - return NewSmbiosBiosInfo(C.hd_smbios_get_biosinfo(smbios)) + result, err = NewSmbiosBiosInfo(C.hd_smbios_get_biosinfo(smbios)) case SmbiosTypeBoard: - return NewSmbiosBoardInfo(C.hd_smbios_get_boardinfo(smbios)) + result, err = NewSmbiosBoardInfo(C.hd_smbios_get_boardinfo(smbios)) case SmbiosTypeCache: - return NewSmbiosCache(C.hd_smbios_get_cache(smbios)) + result, err = NewSmbiosCache(C.hd_smbios_get_cache(smbios)) case SmbiosTypeChassis: - return NewSmbiosChassis(C.hd_smbios_get_chassis(smbios)) + result, err = NewSmbiosChassis(C.hd_smbios_get_chassis(smbios)) case SmbiosTypeConfig: - return NewSmbiosConfig(C.hd_smbios_get_config(smbios)) + result, err = NewSmbiosConfig(C.hd_smbios_get_config(smbios)) case SmbiosTypeGroupAssociations: - return NewSmbiosGroup(C.hd_smbios_get_group(smbios)) + result, err = NewSmbiosGroup(C.hd_smbios_get_group(smbios)) case SmbiosTypeHardwareSecurity: - return NewSmbiosSecure(C.hd_smbios_get_secure(smbios)) + result, err = NewSmbiosSecure(C.hd_smbios_get_secure(smbios)) case SmbiosTypeLanguage: - return NewSmbiosLang(C.hd_smbios_get_lang(smbios)) + result, err = NewSmbiosLang(C.hd_smbios_get_lang(smbios)) case SmbiosTypeMemory64Error: - return NewSmbiosMem64Error(C.hd_smbios_get_mem64error(smbios)) + result, err = NewSmbiosMem64Error(C.hd_smbios_get_mem64error(smbios)) case SmbiosTypeMemoryArray: - return NewSmbiosMemArray(C.hd_smbios_get_memarray(smbios)) + result, err = NewSmbiosMemArray(C.hd_smbios_get_memarray(smbios)) case SmbiosTypeMemoryArrayMappedAddress: - return NewSmbiosMemArrayMap(C.hd_smbios_get_memarraymap(smbios)) + result, err = NewSmbiosMemArrayMap(C.hd_smbios_get_memarraymap(smbios)) case SmbiosTypeMemoryDevice: - return NewSmbiosMemDevice(C.hd_smbios_get_memdevice(smbios)) + result, err = NewSmbiosMemDevice(C.hd_smbios_get_memdevice(smbios)) case SmbiosTypeMemoryDeviceMappedAddress: - return NewSmbiosMemDeviceMap(C.hd_smbios_get_memdevicemap(smbios)) + result, err = NewSmbiosMemDeviceMap(C.hd_smbios_get_memdevicemap(smbios)) case SmbiosTypeMemoryError: - return NewSmbiosMemError(C.hd_smbios_get_memerror(smbios)) + result, err = NewSmbiosMemError(C.hd_smbios_get_memerror(smbios)) case SmbiosTypeOEMStrings: - // at least for framework this contains asset_tags. since it's unstructured informtation we skip it for now - // return NewSmbiosOEM(C.hd_smbios_get_oem(smbios)) - return nil, nil + // At least for framework, this contains asset_tags. Since it's unstructured information, we skip it for now case SmbiosTypeOnboard: - return NewSmbiosOnboard(C.hd_smbios_get_onboard(smbios)) + result, err = NewSmbiosOnboard(C.hd_smbios_get_onboard(smbios)) case SmbiosTypePointingDevice: - return NewSmbiosMouse(C.hd_smbios_get_mouse(smbios)) + result, err = NewSmbiosMouse(C.hd_smbios_get_mouse(smbios)) case SmbiosTypePortConnector: - return NewSmbiosConnect(C.hd_smbios_get_connect(smbios)) + result, err = NewSmbiosConnect(C.hd_smbios_get_connect(smbios)) case SmbiosTypePowerControls: - return NewSmbiosPower(C.hd_smbios_get_power(smbios)) + result, err = NewSmbiosPower(C.hd_smbios_get_power(smbios)) case SmbiosTypeProcessor: - return NewSmbiosProcessor(C.hd_smbios_get_processor(smbios)) + result, err = NewSmbiosProcessor(C.hd_smbios_get_processor(smbios)) case SmbiosTypeSlot: - return NewSmbiosSlot(C.hd_smbios_get_slot(smbios)) + result, err = NewSmbiosSlot(C.hd_smbios_get_slot(smbios)) case SmbiosTypeSystem: - return NewSmbiosSysInfo(C.hd_smbios_get_sysinfo(smbios)) + result, err = NewSmbiosSysInfo(C.hd_smbios_get_sysinfo(smbios)) default: // We could return Any for this, but it's just noise in the report. // As we support new types, users can run it again. - return nil, nil } + + return result, err } diff --git a/pkg/hwinfo/smbios_any.go b/pkg/hwinfo/smbios_any.go index 1d58095..be083c4 100644 --- a/pkg/hwinfo/smbios_any.go +++ b/pkg/hwinfo/smbios_any.go @@ -23,8 +23,8 @@ func (s SmbiosAny) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosAny(smbiosType SmbiosType, info C.smbios_any_t) (Smbios, error) { - return SmbiosAny{ +func NewSmbiosAny(smbiosType SmbiosType, info C.smbios_any_t) (*SmbiosAny, error) { + return &SmbiosAny{ Type: smbiosType, Handle: int(info.handle), Data: fmt.Sprintf("0x%x", ReadByteArray(unsafe.Pointer(info.data), int(info.data_len))), diff --git a/pkg/hwinfo/smbios_bios.go b/pkg/hwinfo/smbios_bios.go index 55fe855..2abe2c0 100644 --- a/pkg/hwinfo/smbios_bios.go +++ b/pkg/hwinfo/smbios_bios.go @@ -26,8 +26,8 @@ func (s SmbiosBios) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosBiosInfo(info C.smbios_biosinfo_t) (Smbios, error) { - return SmbiosBios{ +func NewSmbiosBiosInfo(info C.smbios_biosinfo_t) (*SmbiosBios, error) { + return &SmbiosBios{ Type: SmbiosTypeBios, Handle: int(info.handle), Vendor: C.GoString(info.vendor), diff --git a/pkg/hwinfo/smbios_board.go b/pkg/hwinfo/smbios_board.go index 061a242..14bfb98 100644 --- a/pkg/hwinfo/smbios_board.go +++ b/pkg/hwinfo/smbios_board.go @@ -16,7 +16,7 @@ type SmbiosBoard struct { Version string `json:"version"` Serial string `json:"-"` // omit from json output AssetTag string `json:"-"` - BoardType *Id `json:"board_type"` + BoardType *ID `json:"board_type"` Features []string `json:"features"` Location string `json:"location"` // location in chassis Chassis int `json:"chassis"` // handle of chassis @@ -27,8 +27,8 @@ func (s SmbiosBoard) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosBoardInfo(info C.smbios_boardinfo_t) (Smbios, error) { - return SmbiosBoard{ +func NewSmbiosBoardInfo(info C.smbios_boardinfo_t) (*SmbiosBoard, error) { + return &SmbiosBoard{ Type: SmbiosTypeBoard, Handle: int(info.handle), Manufacturer: C.GoString(info.manuf), @@ -36,7 +36,7 @@ func NewSmbiosBoardInfo(info C.smbios_boardinfo_t) (Smbios, error) { Version: C.GoString(info.version), Serial: C.GoString(info.serial), AssetTag: C.GoString(info.asset), - BoardType: NewId(info.board_type), + BoardType: NewID(info.board_type), Features: ReadStringList(info.feature.str), Location: C.GoString(info.location), Chassis: int(info.chassis), diff --git a/pkg/hwinfo/smbios_cache.go b/pkg/hwinfo/smbios_cache.go index 3eadd35..955f164 100644 --- a/pkg/hwinfo/smbios_cache.go +++ b/pkg/hwinfo/smbios_cache.go @@ -14,14 +14,14 @@ type SmbiosCache struct { SizeMax uint `json:"size_max"` // max cache size in kbytes SizeCurrent uint `json:"size_current"` // current size in kbytes Speed uint `json:"speed"` // cache speed in nanoseconds - Mode *Id `json:"mode"` // operational mode + Mode *ID `json:"mode"` // operational mode Enabled bool `json:"enabled"` - Location *Id `json:"location"` // cache location + Location *ID `json:"location"` // cache location Socketed bool `json:"socketed"` Level uint `json:"level"` // cache level (0 = L1, 1 = L2, ...) - ECC *Id `json:"ecc"` // error correction type - CacheType *Id `json:"cache_type"` // logical cache type - Associativity *Id `json:"associativity"` // cache associativity + ECC *ID `json:"ecc"` // error correction type + CacheType *ID `json:"cache_type"` // logical cache type + Associativity *ID `json:"associativity"` // cache associativity SRAMType []string `json:"sram_type_current"` SRAMTypes []string `json:"sram_type_supported"` } @@ -30,22 +30,22 @@ func (s SmbiosCache) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosCache(info C.smbios_cache_t) (Smbios, error) { - return SmbiosCache{ +func NewSmbiosCache(info C.smbios_cache_t) (*SmbiosCache, error) { + return &SmbiosCache{ Type: SmbiosTypeCache, Handle: int(info.handle), Socket: C.GoString(info.socket), SizeMax: uint(info.max_size), SizeCurrent: uint(info.current_size), Speed: uint(info.speed), - Mode: NewId(info.mode), + Mode: NewID(info.mode), Enabled: uint(info.state) == 1, - Location: NewId(info.location), + Location: NewID(info.location), Socketed: uint(info.socketed) == 1, Level: uint(info.level), - ECC: NewId(info.ecc), - CacheType: NewId(info.cache_type), - Associativity: NewId(info.assoc), + ECC: NewID(info.ecc), + CacheType: NewID(info.cache_type), + Associativity: NewID(info.assoc), SRAMType: ReadStringList(info.sram.str), SRAMTypes: ReadStringList(info.supp_sram.str), }, nil diff --git a/pkg/hwinfo/smbios_chassis.go b/pkg/hwinfo/smbios_chassis.go index 9354e46..ea029d1 100644 --- a/pkg/hwinfo/smbios_chassis.go +++ b/pkg/hwinfo/smbios_chassis.go @@ -15,12 +15,12 @@ type SmbiosChassis struct { Version string `json:"version"` Serial string `json:"-"` // omit from json output AssetTag string `json:"-"` // asset tag - ChassisType *Id `json:"chassis_type"` + ChassisType *ID `json:"chassis_type"` LockPresent bool `json:"lock_present"` // true: lock present, false: not present or unknown - BootupState *Id `json:"bootup_state"` - PowerState *Id `json:"power_state"` // power supply state (at last boot) - ThermalState *Id `json:"thermal_state"` // thermal state (at last boot) - SecurityState *Id `json:"security_state"` // security state (at last boot) + BootupState *ID `json:"bootup_state"` + PowerState *ID `json:"power_state"` // power supply state (at last boot) + ThermalState *ID `json:"thermal_state"` // thermal state (at last boot) + SecurityState *ID `json:"security_state"` // security state (at last boot) OEM string `json:"oem"` // oem-specific information" } @@ -28,20 +28,20 @@ func (s SmbiosChassis) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosChassis(info C.smbios_chassis_t) (Smbios, error) { - return SmbiosChassis{ +func NewSmbiosChassis(info C.smbios_chassis_t) (*SmbiosChassis, error) { + return &SmbiosChassis{ Type: SmbiosTypeChassis, Handle: int(info.handle), Manufacturer: C.GoString(info.manuf), Version: C.GoString(info.version), Serial: C.GoString(info.serial), AssetTag: C.GoString(info.asset), - ChassisType: NewId(info.ch_type), + ChassisType: NewID(info.ch_type), LockPresent: uint(info.lock) == 1, - BootupState: NewId(info.bootup), - PowerState: NewId(info.power), - ThermalState: NewId(info.thermal), - SecurityState: NewId(info.security), + BootupState: NewID(info.bootup), + PowerState: NewID(info.power), + ThermalState: NewID(info.thermal), + SecurityState: NewID(info.security), OEM: fmt.Sprintf("0x%x", uint(info.oem)), }, nil } diff --git a/pkg/hwinfo/smbios_config.go b/pkg/hwinfo/smbios_config.go index 850a44f..eb214f1 100644 --- a/pkg/hwinfo/smbios_config.go +++ b/pkg/hwinfo/smbios_config.go @@ -17,8 +17,8 @@ func (s SmbiosConfig) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosConfig(info C.smbios_config_t) (Smbios, error) { - return SmbiosConfig{ +func NewSmbiosConfig(info C.smbios_config_t) (*SmbiosConfig, error) { + return &SmbiosConfig{ Type: SmbiosTypeConfig, Handle: int(info.handle), Options: ReadStringList(info.options), diff --git a/pkg/hwinfo/smbios_group_associations.go b/pkg/hwinfo/smbios_group_associations.go index 2bc9bbb..cbaeca2 100644 --- a/pkg/hwinfo/smbios_group_associations.go +++ b/pkg/hwinfo/smbios_group_associations.go @@ -19,8 +19,8 @@ func (s SmbiosGroupAssociations) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosGroup(info C.smbios_group_t) (Smbios, error) { - return SmbiosGroupAssociations{ +func NewSmbiosGroup(info C.smbios_group_t) (*SmbiosGroupAssociations, error) { + return &SmbiosGroupAssociations{ Type: SmbiosTypeGroupAssociations, Handle: int(info.handle), Name: C.GoString(info.name), diff --git a/pkg/hwinfo/smbios_hardware_security.go b/pkg/hwinfo/smbios_hardware_security.go index efa9d2b..3867e49 100644 --- a/pkg/hwinfo/smbios_hardware_security.go +++ b/pkg/hwinfo/smbios_hardware_security.go @@ -10,23 +10,23 @@ import "C" type SmbiosHardwareSecurity struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` - Power *Id `json:"power"` // power-on password status - Keyboard *Id `json:"keyboard"` // keyboard password status - Admin *Id `json:"admin"` // admin password status - Reset *Id `json:"reset"` // front panel reset status + Power *ID `json:"power"` // power-on password status + Keyboard *ID `json:"keyboard"` // keyboard password status + Admin *ID `json:"admin"` // admin password status + Reset *ID `json:"reset"` // front panel reset status } func (s SmbiosHardwareSecurity) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosSecure(info C.smbios_secure_t) (Smbios, error) { - return SmbiosHardwareSecurity{ +func NewSmbiosSecure(info C.smbios_secure_t) (*SmbiosHardwareSecurity, error) { + return &SmbiosHardwareSecurity{ Type: SmbiosTypeHardwareSecurity, Handle: int(info.handle), - Power: NewId(info.power), - Keyboard: NewId(info.keyboard), - Admin: NewId(info.admin), - Reset: NewId(info.reset), + Power: NewID(info.power), + Keyboard: NewID(info.keyboard), + Admin: NewID(info.admin), + Reset: NewID(info.reset), }, nil } diff --git a/pkg/hwinfo/smbios_language.go b/pkg/hwinfo/smbios_language.go index 856f4ea..20bf387 100644 --- a/pkg/hwinfo/smbios_language.go +++ b/pkg/hwinfo/smbios_language.go @@ -18,8 +18,8 @@ func (s SmbiosLanguage) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosLang(info C.smbios_lang_t) (Smbios, error) { - return SmbiosLanguage{ +func NewSmbiosLang(info C.smbios_lang_t) (*SmbiosLanguage, error) { + return &SmbiosLanguage{ Type: SmbiosTypeLanguage, Handle: int(info.handle), Languages: ReadStringList(info.strings), diff --git a/pkg/hwinfo/smbios_memory64_error.go b/pkg/hwinfo/smbios_memory64_error.go index 0dfaec5..64eba64 100644 --- a/pkg/hwinfo/smbios_memory64_error.go +++ b/pkg/hwinfo/smbios_memory64_error.go @@ -10,26 +10,26 @@ import "C" type SmbiosMemory64Error struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` - ErrorType *Id `json:"error_type"` // error type memory - Granularity *Id `json:"granularity"` // memory array or memory partition - Operation *Id `json:"operation"` // mem operation causing the rror + ErrorType *ID `json:"error_type"` // error type memory + Granularity *ID `json:"granularity"` // memory array or memory partition + Operation *ID `json:"operation"` // mem operation causing the rror Syndrome uint `json:"syndrome"` // vendor-specific ECC syndrome; 0: unknown ArrayAddress uint `json:"array_address"` // fault address relative to mem array; 0x80000000: unknown DeviceAddress uint `json:"device_address"` // fault address relative to mem array; 0x80000000: unknown - Range uint `json:"range"` // range, within which the error can be determined; 0x80000000: unknown + Range uint `json:"range"` // range within which the error can be determined; 0x80000000: unknown } func (s SmbiosMemory64Error) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosMem64Error(info C.smbios_mem64error_t) (Smbios, error) { - return SmbiosMemory64Error{ +func NewSmbiosMem64Error(info C.smbios_mem64error_t) (*SmbiosMemory64Error, error) { + return &SmbiosMemory64Error{ Type: SmbiosTypeMemory64Error, Handle: int(info.handle), - ErrorType: NewId(info.err_type), - Granularity: NewId(info.granularity), - Operation: NewId(info.operation), + ErrorType: NewID(info.err_type), + Granularity: NewID(info.granularity), + Operation: NewID(info.operation), Syndrome: uint(info.syndrome), ArrayAddress: uint(info.array_addr), DeviceAddress: uint(info.device_addr), diff --git a/pkg/hwinfo/smbios_memory_array.go b/pkg/hwinfo/smbios_memory_array.go index e38ebf7..71248bd 100644 --- a/pkg/hwinfo/smbios_memory_array.go +++ b/pkg/hwinfo/smbios_memory_array.go @@ -10,9 +10,9 @@ import "C" type SmbiosMemoryArray struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` - Location *Id `json:"location"` // memory device location - Usage *Id `json:"usage"` // memory usage - ECC *Id `json:"ecc"` // ECC types + Location *ID `json:"location"` // memory device location + Usage *ID `json:"usage"` // memory usage + ECC *ID `json:"ecc"` // ECC types MaxSize uint `json:"max_size"` // max memory size in KB ErrorHandle int `json:"error_handle"` // points to error info record; 0xfffe: not supported, 0xffff: no error Slots uint `json:"slots"` // slots or sockets for this device @@ -22,13 +22,13 @@ func (s SmbiosMemoryArray) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosMemArray(info C.smbios_memarray_t) (Smbios, error) { - return SmbiosMemoryArray{ +func NewSmbiosMemArray(info C.smbios_memarray_t) (*SmbiosMemoryArray, error) { + return &SmbiosMemoryArray{ Type: SmbiosTypeMemoryArray, Handle: int(info.handle), - Location: NewId(info.location), - Usage: NewId(info.use), - ECC: NewId(info.ecc), + Location: NewID(info.location), + Usage: NewID(info.use), + ECC: NewID(info.ecc), MaxSize: uint(info.max_size), ErrorHandle: int(info.error_handle), Slots: uint(info.slots), diff --git a/pkg/hwinfo/smbios_memory_array_mapped_address.go b/pkg/hwinfo/smbios_memory_array_mapped_address.go index 6f1cc02..cc52a0e 100644 --- a/pkg/hwinfo/smbios_memory_array_mapped_address.go +++ b/pkg/hwinfo/smbios_memory_array_mapped_address.go @@ -20,8 +20,8 @@ func (s SmbiosMemoryArrayMappedAddress) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosMemArrayMap(info C.smbios_memarraymap_t) (Smbios, error) { - return SmbiosMemoryArrayMappedAddress{ +func NewSmbiosMemArrayMap(info C.smbios_memarraymap_t) (*SmbiosMemoryArrayMappedAddress, error) { + return &SmbiosMemoryArrayMappedAddress{ Type: SmbiosTypeMemoryArrayMappedAddress, Handle: int(info.handle), ArrayHandle: int(info.array_handle), diff --git a/pkg/hwinfo/smbios_memory_device.go b/pkg/hwinfo/smbios_memory_device.go index c511d31..fbab60e 100644 --- a/pkg/hwinfo/smbios_memory_device.go +++ b/pkg/hwinfo/smbios_memory_device.go @@ -7,6 +7,8 @@ package hwinfo import "C" // SmbiosMemoryDevice captures system slot information. +// +//nolint:lll type SmbiosMemoryDevice struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` @@ -21,9 +23,9 @@ type SmbiosMemoryDevice struct { Width uint `json:"width"` // data width in bits ECCBits uint `json:"ecc_bits"` // ecc bits Size uint `json:"size"` // kB - FormFactor *Id `json:"form_factor"` + FormFactor *ID `json:"form_factor"` Set uint `json:"set"` // 0: does not belong to a set; 1-0xfe: set number; 0xff: unknown - MemoryType *Id `json:"memory_type"` + MemoryType *ID `json:"memory_type"` MemoryTypeDetails []string `json:"memory_type_details"` Speed uint `json:"speed"` // MHz } @@ -32,8 +34,8 @@ func (s SmbiosMemoryDevice) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosMemDevice(info C.smbios_memdevice_t) (Smbios, error) { - return SmbiosMemoryDevice{ +func NewSmbiosMemDevice(info C.smbios_memdevice_t) (*SmbiosMemoryDevice, error) { + return &SmbiosMemoryDevice{ Type: SmbiosTypeMemoryDevice, Handle: int(info.handle), Location: C.GoString(info.location), @@ -47,9 +49,9 @@ func NewSmbiosMemDevice(info C.smbios_memdevice_t) (Smbios, error) { Width: uint(info.width), ECCBits: uint(info.eccbits), Size: uint(info.size), - FormFactor: NewId(info.form), + FormFactor: NewID(info.form), Set: uint(info.set), - MemoryType: NewId(info.mem_type), + MemoryType: NewID(info.mem_type), MemoryTypeDetails: ReadStringList(info.type_detail.str), Speed: uint(info.speed), }, nil diff --git a/pkg/hwinfo/smbios_memory_device_mapped_address.go b/pkg/hwinfo/smbios_memory_device_mapped_address.go index 2a75d5b..99e617c 100644 --- a/pkg/hwinfo/smbios_memory_device_mapped_address.go +++ b/pkg/hwinfo/smbios_memory_device_mapped_address.go @@ -7,6 +7,8 @@ package hwinfo import "C" // SmbiosMemoryDeviceMappedAddress captures physical memory array information (consists of several memory devices). +// +//nolint:lll type SmbiosMemoryDeviceMappedAddress struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` @@ -23,8 +25,8 @@ func (s SmbiosMemoryDeviceMappedAddress) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosMemDeviceMap(info C.smbios_memdevicemap_t) (Smbios, error) { - return SmbiosMemoryDeviceMappedAddress{ +func NewSmbiosMemDeviceMap(info C.smbios_memdevicemap_t) (*SmbiosMemoryDeviceMappedAddress, error) { + return &SmbiosMemoryDeviceMappedAddress{ Type: SmbiosTypeMemoryDeviceMappedAddress, Handle: int(info.handle), MemoryDeviceHandle: int(info.memdevice_handle), diff --git a/pkg/hwinfo/smbios_memory_error.go b/pkg/hwinfo/smbios_memory_error.go index 0abe4e0..95db6b1 100644 --- a/pkg/hwinfo/smbios_memory_error.go +++ b/pkg/hwinfo/smbios_memory_error.go @@ -10,26 +10,26 @@ import "C" type SmbiosMemoryError struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` - ErrorType *Id `json:"error_type"` // error type memory - Granularity *Id `json:"granularity"` // memory array or memory partition - Operation *Id `json:"operation"` // mem operation causing the rror + ErrorType *ID `json:"error_type"` // error type memory + Granularity *ID `json:"granularity"` // memory array or memory partition + Operation *ID `json:"operation"` // mem operation causing the rror Syndrome uint `json:"syndrome"` // vendor-specific ECC syndrome; 0: unknown ArrayAddress uint `json:"array_address"` // fault address relative to mem array; 0x80000000: unknown DeviceAddress uint `json:"device_address"` // fault address relative to mem array; 0x80000000: unknown - Range uint `json:"range"` // range, within which the error can be determined; 0x80000000: unknown + Range uint `json:"range"` // range within which the error can be determined; 0x80000000: unknown } func (s SmbiosMemoryError) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosMemError(info C.smbios_memerror_t) (Smbios, error) { - return SmbiosMemoryError{ +func NewSmbiosMemError(info C.smbios_memerror_t) (*SmbiosMemoryError, error) { + return &SmbiosMemoryError{ Type: SmbiosTypeMemoryError, Handle: int(info.handle), - ErrorType: NewId(info.err_type), - Granularity: NewId(info.granularity), - Operation: NewId(info.operation), + ErrorType: NewID(info.err_type), + Granularity: NewID(info.granularity), + Operation: NewID(info.operation), Syndrome: uint(info.syndrome), ArrayAddress: uint(info.array_addr), DeviceAddress: uint(info.device_addr), diff --git a/pkg/hwinfo/smbios_oem_strings.go b/pkg/hwinfo/smbios_oem_strings.go index 7ca0e59..52458ca 100644 --- a/pkg/hwinfo/smbios_oem_strings.go +++ b/pkg/hwinfo/smbios_oem_strings.go @@ -17,8 +17,8 @@ func (s SmbiosOEMStrings) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosOEM(info C.smbios_oem_t) (Smbios, error) { - return SmbiosOEMStrings{ +func NewSmbiosOEM(info C.smbios_oem_t) (*SmbiosOEMStrings, error) { + return &SmbiosOEMStrings{ Type: SmbiosTypeOEMStrings, Handle: int(info.handle), Strings: ReadStringList(info.oem_strings), diff --git a/pkg/hwinfo/smbios_onboard.go b/pkg/hwinfo/smbios_onboard.go index 989ff45..0af55be 100644 --- a/pkg/hwinfo/smbios_onboard.go +++ b/pkg/hwinfo/smbios_onboard.go @@ -12,7 +12,7 @@ import "C" type OnboardDevice struct { Name string `json:"name"` - Type *Id `json:"type"` + Type *ID `json:"type"` Enabled bool `json:"enabled"` } @@ -27,17 +27,17 @@ func (s SmbiosOnboard) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosOnboard(info C.smbios_onboard_t) (Smbios, error) { +func NewSmbiosOnboard(info C.smbios_onboard_t) (*SmbiosOnboard, error) { var devices []OnboardDevice for i := 0; i < int(info.dev_len); i++ { devices = append(devices, OnboardDevice{ Name: C.GoString(C.smbios_onboard_get_name(info, C.int(i))), - Type: NewId(C.smbios_onboard_get_type(info, C.int(i))), + Type: NewID(C.smbios_onboard_get_type(info, C.int(i))), Enabled: uint(C.smbios_onboard_get_status(info, C.int(i))) == 1, }) } - return SmbiosOnboard{ + return &SmbiosOnboard{ Type: SmbiosTypeOnboard, Handle: int(info.handle), Devices: devices, diff --git a/pkg/hwinfo/smbios_pointing_device.go b/pkg/hwinfo/smbios_pointing_device.go index f5b7e40..a0f4091 100644 --- a/pkg/hwinfo/smbios_pointing_device.go +++ b/pkg/hwinfo/smbios_pointing_device.go @@ -10,8 +10,8 @@ import "C" type SmbiosPointingDevice struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` - MouseType *Id `json:"mouse_type"` - Interface *Id `json:"interface"` + MouseType *ID `json:"mouse_type"` + Interface *ID `json:"interface"` Buttons uint `json:"buttons"` } @@ -19,12 +19,12 @@ func (s SmbiosPointingDevice) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosMouse(info C.smbios_mouse_t) (Smbios, error) { - return SmbiosPointingDevice{ +func NewSmbiosMouse(info C.smbios_mouse_t) (*SmbiosPointingDevice, error) { + return &SmbiosPointingDevice{ Type: SmbiosTypePointingDevice, Handle: int(info.handle), - MouseType: NewId(info.mtype), - Interface: NewId(info._interface), + MouseType: NewID(info.mtype), + Interface: NewID(info._interface), Buttons: uint(info.buttons), }, nil } diff --git a/pkg/hwinfo/smbios_port_connector.go b/pkg/hwinfo/smbios_port_connector.go index a018fd2..c587da6 100644 --- a/pkg/hwinfo/smbios_port_connector.go +++ b/pkg/hwinfo/smbios_port_connector.go @@ -10,10 +10,10 @@ import "C" type SmbiosPortConnector struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` - PortType *Id `json:"port_type"` - InternalConnectorType *Id `json:"internal_connector_type,omitempty"` + PortType *ID `json:"port_type"` + InternalConnectorType *ID `json:"internal_connector_type,omitempty"` InternalReferenceDesignator string `json:"internal_reference_designator,omitempty"` - ExternalConnectorType *Id `json:"external_connector_type,omitempty"` + ExternalConnectorType *ID `json:"external_connector_type,omitempty"` ExternalReferenceDesignator string `json:"external_reference_designator,omitempty"` } @@ -21,14 +21,14 @@ func (s SmbiosPortConnector) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosConnect(info C.smbios_connect_t) (Smbios, error) { - return SmbiosPortConnector{ +func NewSmbiosConnect(info C.smbios_connect_t) (*SmbiosPortConnector, error) { + return &SmbiosPortConnector{ Type: SmbiosTypePortConnector, Handle: int(info.handle), - PortType: NewId(info.port_type), - InternalConnectorType: NewId(info.i_type), + PortType: NewID(info.port_type), + InternalConnectorType: NewID(info.i_type), InternalReferenceDesignator: C.GoString(info.i_des), - ExternalConnectorType: NewId(info.x_type), + ExternalConnectorType: NewID(info.x_type), ExternalReferenceDesignator: C.GoString(info.x_des), }, nil } diff --git a/pkg/hwinfo/smbios_power_controls.go b/pkg/hwinfo/smbios_power_controls.go index 7c2f174..2fcb4a2 100644 --- a/pkg/hwinfo/smbios_power_controls.go +++ b/pkg/hwinfo/smbios_power_controls.go @@ -21,8 +21,8 @@ func (s SmbiosPowerControls) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosPower(info C.smbios_power_t) (Smbios, error) { - return SmbiosPowerControls{ +func NewSmbiosPower(info C.smbios_power_t) (*SmbiosPowerControls, error) { + return &SmbiosPowerControls{ Type: SmbiosTypePowerControls, Handle: int(info.handle), Month: uint(info.month), diff --git a/pkg/hwinfo/smbios_processor.go b/pkg/hwinfo/smbios_processor.go index 6204599..897cc49 100644 --- a/pkg/hwinfo/smbios_processor.go +++ b/pkg/hwinfo/smbios_processor.go @@ -11,17 +11,17 @@ type SmbiosProcessor struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` Socket string `json:"socket"` - SocketType *Id `json:"socket_type"` + SocketType *ID `json:"socket_type"` SocketPopulated bool `json:"socket_populated"` // true: populated, false: empty Manufacturer string `json:"manufacturer"` Version string `json:"version"` Serial string `json:"-"` // omit from json output AssetTag string `json:"-"` // asset tag Part string `json:"part"` // part number - ProcessorType *Id `json:"processor_type"` - ProcessorFamily *Id `json:"processor_family"` - ProcessorId uint64 `json:"-"` // omit from json - ProcessorStatus *Id `json:"processor_status"` + ProcessorType *ID `json:"processor_type"` + ProcessorFamily *ID `json:"processor_family"` + ProcessorID uint64 `json:"-"` // omit from json + ProcessorStatus *ID `json:"processor_status"` Voltage uint `json:"-"` ClockExt uint `json:"clock_ext"` // MHz ClockMax uint `json:"clock_max"` // MHz @@ -35,22 +35,22 @@ func (s SmbiosProcessor) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosProcessor(info C.smbios_processor_t) (Smbios, error) { - return SmbiosProcessor{ +func NewSmbiosProcessor(info C.smbios_processor_t) (*SmbiosProcessor, error) { + return &SmbiosProcessor{ Type: SmbiosTypeProcessor, Handle: int(info.handle), Socket: C.GoString(info.socket), - SocketType: NewId(info.upgrade), + SocketType: NewID(info.upgrade), SocketPopulated: uint(info.sock_status) == 1, Manufacturer: C.GoString(info.manuf), Version: C.GoString(info.version), Serial: C.GoString(info.serial), AssetTag: C.GoString(info.asset), Part: C.GoString(info.part), - ProcessorType: NewId(info.pr_type), - ProcessorFamily: NewId(info.family), - ProcessorId: uint64(info.cpu_id), - ProcessorStatus: NewId(info.cpu_status), + ProcessorType: NewID(info.pr_type), + ProcessorFamily: NewID(info.family), + ProcessorID: uint64(info.cpu_id), + ProcessorStatus: NewID(info.cpu_status), Voltage: uint(info.voltage), ClockExt: uint(info.ext_clock), ClockMax: uint(info.max_speed), diff --git a/pkg/hwinfo/smbios_slot.go b/pkg/hwinfo/smbios_slot.go index 5860e67..4ba6e2a 100644 --- a/pkg/hwinfo/smbios_slot.go +++ b/pkg/hwinfo/smbios_slot.go @@ -11,11 +11,11 @@ type SmbiosSlot struct { Type SmbiosType `json:"-"` Handle int `json:"handle"` Designation string `json:"designation,omitempty"` - SlotType *Id `json:"slot_type"` - BusWidth *Id `json:"bus_width"` - Usage *Id `json:"usage"` - Length *Id `json:"length"` - Id uint `json:"id"` + SlotType *ID `json:"slot_type"` + BusWidth *ID `json:"bus_width"` + Usage *ID `json:"usage"` + Length *ID `json:"length"` + ID uint `json:"id"` Features []string `json:"features,omitempty"` } @@ -23,16 +23,16 @@ func (s SmbiosSlot) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosSlot(info C.smbios_slot_t) (Smbios, error) { - return SmbiosSlot{ +func NewSmbiosSlot(info C.smbios_slot_t) (*SmbiosSlot, error) { + return &SmbiosSlot{ Type: SmbiosTypeSlot, Handle: int(info.handle), Designation: C.GoString(info.desig), - SlotType: NewId(info.slot_type), - BusWidth: NewId(info.bus_width), - Usage: NewId(info.usage), - Length: NewId(info.length), - Id: uint(info.id), + SlotType: NewID(info.slot_type), + BusWidth: NewID(info.bus_width), + Usage: NewID(info.usage), + Length: NewID(info.length), + ID: uint(info.id), Features: ReadStringList(info.feature.str), }, nil } diff --git a/pkg/hwinfo/smbios_sys_info.go b/pkg/hwinfo/smbios_sys_info.go index cb0c2a4..09adcb1 100644 --- a/pkg/hwinfo/smbios_sys_info.go +++ b/pkg/hwinfo/smbios_sys_info.go @@ -14,16 +14,16 @@ type SmbiosSystem struct { Product string `json:"product"` Version string `json:"version"` Serial string `json:"-"` // omit from json output - UUID string `json:"-"` // universal unique id; all 0x00: undef, all 0xff: undef but settable, omit from json - WakeUp *Id `json:"wake_up"` // wake-up type + UUID string `json:"-"` // universal unique id; all 0x00: undef, all 0xff: undef but settable + WakeUp *ID `json:"wake_up"` // wake-up type } func (s SmbiosSystem) SmbiosType() SmbiosType { return s.Type } -func NewSmbiosSysInfo(info C.smbios_sysinfo_t) (Smbios, error) { - return SmbiosSystem{ +func NewSmbiosSysInfo(info C.smbios_sysinfo_t) (*SmbiosSystem, error) { + return &SmbiosSystem{ Type: SmbiosTypeSystem, Handle: int(info.handle), Manufacturer: C.GoString(info.manuf), @@ -31,6 +31,6 @@ func NewSmbiosSysInfo(info C.smbios_sysinfo_t) (Smbios, error) { Version: C.GoString(info.version), Serial: C.GoString(info.serial), // todo uuid - WakeUp: NewId(info.wake_up), + WakeUp: NewID(info.wake_up), }, nil } diff --git a/pkg/hwinfo/virtual_network_devices.go b/pkg/hwinfo/virtual_network_devices.go index a5705c8..bdb5292 100644 --- a/pkg/hwinfo/virtual_network_devices.go +++ b/pkg/hwinfo/virtual_network_devices.go @@ -1,6 +1,6 @@ package hwinfo -// original list taken from systemd.network +// original list taken from systemd.network. var virtualNetworkDevices = map[string]bool{ "bonding": true, "bridge": true, diff --git a/pkg/virt/virt.go b/pkg/virt/virt.go index 810f1cd..b39d7b4 100644 --- a/pkg/virt/virt.go +++ b/pkg/virt/virt.go @@ -16,6 +16,7 @@ import ( //go:generate enumer -type=Type -json -text -transform=snake -trimprefix Type -output=./virt_enum_type.go type Type int +//nolint:revive,stylecheck const ( TypeNone Type = iota TypeKvm @@ -59,6 +60,7 @@ func Detect() (Type, error) { // note: systemd-detect-virt exits with status 1 when "none" is detected if !(outStr == "none" || err == nil) { slog.Error("failed to detect virtualisation type", "output", out) + return TypeNone, fmt.Errorf("failed to detect virtualisation type: %w", err) }