Matthew Stobbs
de93204e3d
- instead of getting all the data the hard way, use libvirtxml to parse the XML from libvirt - this makes it more accurate, and more future proof when schema changes occur - add pcidb to query devices better
49 lines
958 B
Go
49 lines
958 B
Go
package util
|
|
|
|
import (
|
|
"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.Printf("warning: couldn't use local pcidb cache: %s", err)
|
|
log.Println("falling back to downloading database")
|
|
db, err = pcidb.New(pcidb.WithEnableNetworkFetch())
|
|
if err != nil {
|
|
log.Println("error: couldn't get pcidb. no more fallbacks available, will not be able to query the pcidb")
|
|
}
|
|
}
|
|
pcidbInitDone = true
|
|
}
|
|
|
|
func GetPCIClass(id string) string {
|
|
if !pcidbInitDone {
|
|
initPCIDB()
|
|
}
|
|
if pcidbInitDone && db == nil {
|
|
log.Println("unable to access pcidb")
|
|
return pcidbNODB
|
|
}
|
|
if class, ok := db.Classes[id]; ok {
|
|
return class.Name
|
|
}
|
|
return pcidbNOTFOUND
|
|
}
|