Skip to content

Commit

Permalink
feat: accept PCIIDS_LOCAL_PATH env variable to read a local file for …
Browse files Browse the repository at this point in the history
…PCI IDs database (#3)

Signed-off-by: Xiaodong Ye <xiaodong.ye@mthreads.com>
  • Loading branch information
yeahdongcn committed Sep 30, 2022
1 parent d4c8f9a commit ec08432
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,56 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const remoteURL = "https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids"
const (
remoteURL = "https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids"
localPathEnvVar = "PCIIDS_LOCAL_PATH"
)

// All returns all PCI IDs in a slice.
func All() ([]PCIID, error) {
rawIDs, err := Latest()
if err != nil {
return nil, errors.Wrap(err, "cannot read latest IDs")
var rawIDs string
var err error

path, exist := os.LookupEnv(localPathEnvVar)
if exist {
rawIDs, err = Local(path)
if err != nil {
return nil, errors.Wrap(err, "cannot read local IDs")
}
} else {
rawIDs, err = Latest()
if err != nil {
return nil, errors.Wrap(err, "cannot read latest IDs")
}
}

parsedIDs := parse(rawIDs)

return parsedIDs, nil
}

func Local(path string) (string, error) {
log.Debug("reading: ", path)

if _, err := os.Stat(path); err != nil {
return "", errors.Wrap(err, "could not stat local file")
}

bytes, err := os.ReadFile(path)
if err != nil {
return "", errors.Wrap(err, "could not read local file")
}

return string(bytes), nil
}

// Latest downloads the latest PCI ID file from the GitHub mirror.
func Latest() (string, error) {
return LatestWithContext(context.Background())
Expand Down

0 comments on commit ec08432

Please sign in to comment.