-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.go
108 lines (101 loc) · 3.34 KB
/
file.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
99
100
101
102
103
104
105
106
107
108
package resource
import (
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
)
type File struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Path string `json:"-" yaml:"-"`
Exists matcher `json:"exists" yaml:"exists"`
Mode matcher `json:"mode,omitempty" yaml:"mode,omitempty"`
Size matcher `json:"size,omitempty" yaml:"size,omitempty"`
Owner matcher `json:"owner,omitempty" yaml:"owner,omitempty"`
Group matcher `json:"group,omitempty" yaml:"group,omitempty"`
LinkedTo matcher `json:"linked-to,omitempty" yaml:"linked-to,omitempty"`
Filetype matcher `json:"filetype,omitempty" yaml:"filetype,omitempty"`
Contains []string `json:"contains" yaml:"contains"`
Md5 matcher `json:"md5,omitempty" yaml:"md5,omitempty"`
Sha256 matcher `json:"sha256,omitempty" yaml:"sha256,omitempty"`
}
func (f *File) ID() string { return f.Path }
func (f *File) SetID(id string) { f.Path = id }
func (f *File) GetTitle() string { return f.Title }
func (f *File) GetMeta() meta { return f.Meta }
func (f *File) Validate(sys *system.System) []TestResult {
skip := false
sysFile := sys.NewFile(f.Path, sys, util.Config{})
var results []TestResult
results = append(results, ValidateValue(f, "exists", f.Exists, sysFile.Exists, skip))
if shouldSkip(results) {
skip = true
}
if f.Mode != nil {
results = append(results, ValidateValue(f, "mode", f.Mode, sysFile.Mode, skip))
}
if f.Owner != nil {
results = append(results, ValidateValue(f, "owner", f.Owner, sysFile.Owner, skip))
}
if f.Group != nil {
results = append(results, ValidateValue(f, "group", f.Group, sysFile.Group, skip))
}
if f.LinkedTo != nil {
results = append(results, ValidateValue(f, "linkedto", f.LinkedTo, sysFile.LinkedTo, skip))
}
if f.Filetype != nil {
results = append(results, ValidateValue(f, "filetype", f.Filetype, sysFile.Filetype, skip))
}
if len(f.Contains) > 0 {
results = append(results, ValidateContains(f, "contains", f.Contains, sysFile.Contains, skip))
}
if f.Size != nil {
results = append(results, ValidateValue(f, "size", f.Size, sysFile.Size, skip))
}
if f.Md5 != nil {
results = append(results, ValidateValue(f, "md5", f.Md5, sysFile.Md5, skip))
}
if f.Sha256 != nil {
results = append(results, ValidateValue(f, "sha256", f.Sha256, sysFile.Sha256, skip))
}
return results
}
func NewFile(sysFile system.File, config util.Config) (*File, error) {
path := sysFile.Path()
exists, _ := sysFile.Exists()
f := &File{
Path: path,
Exists: exists,
Contains: []string{},
}
if !contains(config.IgnoreList, "mode") {
if mode, err := sysFile.Mode(); err == nil {
f.Mode = mode
}
}
if !contains(config.IgnoreList, "owner") {
if owner, err := sysFile.Owner(); err == nil {
f.Owner = owner
}
}
if !contains(config.IgnoreList, "group") {
if group, err := sysFile.Group(); err == nil {
f.Group = group
}
}
if !contains(config.IgnoreList, "linked-to") {
if linkedTo, err := sysFile.LinkedTo(); err == nil {
f.LinkedTo = linkedTo
}
}
if !contains(config.IgnoreList, "filetype") {
if filetype, err := sysFile.Filetype(); err == nil {
f.Filetype = filetype
}
}
if !contains(config.IgnoreList, "size") {
if size, err := sysFile.Size(); err == nil {
f.Size = size
}
}
return f, nil
}