clustvirt/util/pcidb.go

47 lines
933 B
Go
Raw Permalink Normal View History

package util
import (
log "git.staur.ca/stobbsm/simplelog"
"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
}