47 lines
937 B
Go
47 lines
937 B
Go
package util
|
|
|
|
import (
|
|
"git.staur.ca/stobbsm/clustvirt/lib/log"
|
|
"github.com/jaypipes/pcidb"
|
|
)
|
|
|
|
var (
|
|
pcidbInitDone = false
|
|
db *pcidb.PCIDB
|
|
)
|
|
|
|
const (
|
|
pcidbNOTFOUND string = `NOTFOUND`
|
|
pcidbNODB string = `NODBFOUND`
|
|
)
|
|
|
|
func initPCIDB() {
|
|
var err error
|
|
|
|
// Attempt to use local sources first, fallback to network if
|
|
// local sources aren't found
|
|
db, err = pcidb.New()
|
|
if err != nil {
|
|
log.Warn("util.initPCIDB").Err(err).Msg("fallback to downloading pcidb")
|
|
db, err = pcidb.New(pcidb.WithEnableNetworkFetch())
|
|
if err != nil {
|
|
log.Error("util.initPCIDB").Err(err).Msg("no more fallbacks available")
|
|
}
|
|
}
|
|
pcidbInitDone = true
|
|
}
|
|
|
|
func GetPCIClass(id string) string {
|
|
if !pcidbInitDone {
|
|
initPCIDB()
|
|
}
|
|
if pcidbInitDone && db == nil {
|
|
log.Warn("util.GetPCIClass").Msg("no pcidb to do lookup")
|
|
return pcidbNODB
|
|
}
|
|
if class, ok := db.Classes[id]; ok {
|
|
return class.Name
|
|
}
|
|
return pcidbNOTFOUND
|
|
}
|