Skip to content

Commit

Permalink
Merge pull request #240 from fromanirh/pci-lookup-device
Browse files Browse the repository at this point in the history
pci: add and use LookupDevice
  • Loading branch information
jaypipes authored Apr 12, 2021
2 parents 14f2fd2 + c126558 commit f38192d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pkg/pci/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ func New(opts ...*option.Option) (*Info, error) {
return info, nil
}

// lookupDevice gets a device from cached data
func (info *Info) lookupDevice(address string) *Device {
for _, dev := range info.Devices {
if dev.Address == address {
return dev
}
}
return nil
}

// simple private struct used to encapsulate PCI information in a top-level
// "pci" YAML/JSON map/object key
type pciPrinter struct {
Expand Down
9 changes: 7 additions & 2 deletions pkg/pci/pci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,14 @@ func findPCIProgrammingInterface(
}

// GetDevice returns a pointer to a Device struct that describes the PCI
// device at the requested address. If no such device could be found, returns
// nil
// device at the requested address. If no such device could be found, returns nil.
func (info *Info) GetDevice(address string) *Device {
// check cached data first
if dev := info.lookupDevice(address); dev != nil {
return dev
}

// no cached data, let's get the information from system.
fp := getDeviceModaliasPath(info.ctx, address)
if fp == "" {
info.ctx.Warn("error finding modalias info for device %q", address)
Expand Down

0 comments on commit f38192d

Please sign in to comment.