forked from jaypipes/pcidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
69 lines (61 loc) · 1.61 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package pcidb
import (
"fmt"
"os"
"path/filepath"
"runtime"
homedir "github.com/mitchellh/go-homedir"
)
// Concrete merged set of configuration switches that get passed to pcidb
// internal functions
type context struct {
chroot string
cacheOnly bool
cachePath string
searchPaths []string
}
func contextFromOptions(merged *WithOption) *context {
ctx := &context{
chroot: *merged.Chroot,
cacheOnly: *merged.CacheOnly,
cachePath: getCachePath(),
searchPaths: make([]string, 0),
}
ctx.setSearchPaths()
return ctx
}
func getCachePath() string {
hdir, err := homedir.Dir()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed getting homedir.Dir(): %v", err)
return ""
}
fp, err := homedir.Expand(filepath.Join(hdir, ".cache", "pci.ids"))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed expanding local cache path: %v", err)
return ""
}
return fp
}
// Depending on the operating system, sets the context's searchPaths to a set
// of local filepaths to search for a pci.ids database file
func (ctx *context) setSearchPaths() {
// A set of filepaths we will first try to search for the pci-ids DB file
// on the local machine. If we fail to find one, we'll try pulling the
// latest pci-ids file from the network
ctx.searchPaths = append(ctx.searchPaths, ctx.cachePath)
if ctx.cacheOnly {
return
}
rootPath := ctx.chroot
if runtime.GOOS != "windows" {
ctx.searchPaths = append(
ctx.searchPaths,
filepath.Join(rootPath, "usr", "share", "hwdata", "pci.ids"),
)
ctx.searchPaths = append(
ctx.searchPaths,
filepath.Join(rootPath, "usr", "share", "misc", "pci.ids"),
)
}
}