-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdrvinfo.go
98 lines (86 loc) · 1.81 KB
/
drvinfo.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package drvinfo
import (
"log"
"os"
"github.com/hashicorp/go-version"
"github.com/safchain/ethtool"
"gopkg.in/yaml.v3"
)
var (
newEthtool = newEthtoolHandler
)
type ethtoolInterface interface {
Close()
DriverInfo(name string) (ethtool.DrvInfo, error)
}
func newEthtoolHandler() (ethtoolInterface, error) {
return ethtool.NewEthtool()
}
type DriverInfo struct {
Name string
Version string
}
type DriversList struct {
Drivers []DriverInfo
}
type SupportedDrivers struct {
Drivers DriversList
DbFilePath string
}
var NewSupportedDrivers = func(fp string) SupportedDrivers {
retv := SupportedDrivers{}
supportedDrivers, err := readSupportedDrivers(fp)
if err != nil {
log.Printf("fetching supported drivers list from %s failed with error %v", fp, err)
return retv
}
retv.Drivers = *supportedDrivers
retv.DbFilePath = fp
return retv
}
var GetDriverInfo = func(name string) (*DriverInfo, error) {
ethHandle, err := newEthtool()
if err != nil {
return nil, err
}
defer ethHandle.Close()
drvInfo, err := ethHandle.DriverInfo(name)
if err != nil {
return nil, err
}
return &DriverInfo{
Name: drvInfo.Driver,
Version: drvInfo.Version,
}, nil
}
func (dl *SupportedDrivers) IsDriverSupported(drv *DriverInfo) bool {
for _, d := range dl.Drivers.Drivers {
if d.Name != drv.Name {
continue
}
supported, err := version.NewVersion(d.Version)
if err != nil {
continue
}
v, err := version.NewVersion(drv.Version)
if err != nil {
continue
}
if v.GreaterThanOrEqual(supported) {
return true
}
}
return false
}
func readSupportedDrivers(filePath string) (*DriversList, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
drivers := &DriversList{}
err = yaml.Unmarshal(data, drivers)
if err != nil {
return drivers, err
}
return drivers, nil
}