-
Notifications
You must be signed in to change notification settings - Fork 2
/
interface.go
60 lines (53 loc) · 1.69 KB
/
interface.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
package resource
import (
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
)
type Interface struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Name string `json:"-" yaml:"-"`
Exists matcher `json:"exists" yaml:"exists"`
Addrs matcher `json:"addrs,omitempty" yaml:"addrs,omitempty"`
MTU matcher `json:"mtu,omitempty" yaml:"mtu,omitempty"`
}
func (i *Interface) ID() string { return i.Name }
func (i *Interface) SetID(id string) { i.Name = id }
// FIXME: Can this be refactored?
func (i *Interface) GetTitle() string { return i.Title }
func (i *Interface) GetMeta() meta { return i.Meta }
func (i *Interface) Validate(sys *system.System) []TestResult {
skip := false
sysInterface := sys.NewInterface(i.Name, sys, util.Config{})
var results []TestResult
results = append(results, ValidateValue(i, "exists", i.Exists, sysInterface.Exists, skip))
if shouldSkip(results) {
skip = true
}
if i.Addrs != nil {
results = append(results, ValidateValue(i, "addrs", i.Addrs, sysInterface.Addrs, skip))
}
if i.MTU != nil {
results = append(results, ValidateValue(i, "mtu", i.MTU, sysInterface.MTU, skip))
}
return results
}
func NewInterface(sysInterface system.Interface, config util.Config) (*Interface, error) {
name := sysInterface.Name()
exists, _ := sysInterface.Exists()
i := &Interface{
Name: name,
Exists: exists,
}
if !contains(config.IgnoreList, "addrs") {
if addrs, err := sysInterface.Addrs(); err == nil {
i.Addrs = addrs
}
}
if !contains(config.IgnoreList, "mtu") {
if mtu, err := sysInterface.MTU(); err == nil {
i.MTU = mtu
}
}
return i, nil
}