forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
199 lines (170 loc) · 5.13 KB
/
add.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package goss
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/system"
"github.com/aelsabbahy/goss/util"
)
// AddResources is a simple wrapper to add multiple resources
func AddResources(fileName, resourceName string, keys []string, c *util.Config) error {
var err error
outStoreFormat, err = getStoreFormatFromFileName(fileName)
if err != nil {
return err
}
var gossConfig GossConfig
if _, err := os.Stat(fileName); err == nil {
gossConfig, err = ReadJSON(fileName)
if err != nil {
return err
}
} else {
gossConfig = *NewGossConfig()
}
sys := system.New(c.PackageManager)
for _, key := range keys {
if err := AddResource(fileName, gossConfig, resourceName, key, *c, sys); err != nil {
return err
}
}
return WriteJSON(fileName, gossConfig)
}
// AddResource adds a single resource to fileName
func AddResource(fileName string, gossConfig GossConfig, resourceName, key string, config util.Config, sys *system.System) error {
var res resource.ResourceRead
var err error
// Need to figure out a good way to refactor this
switch resourceName {
case "Addr":
res, err = gossConfig.Addrs.AppendSysResource(key, sys, config)
case "Command":
res, err = gossConfig.Commands.AppendSysResource(key, sys, config)
case "DNS":
res, err = gossConfig.DNS.AppendSysResource(key, sys, config)
case "File":
res, err = gossConfig.Files.AppendSysResource(key, sys, config)
case "Group":
res, err = gossConfig.Groups.AppendSysResource(key, sys, config)
case "Package":
res, err = gossConfig.Packages.AppendSysResource(key, sys, config)
case "Port":
res, err = gossConfig.Ports.AppendSysResource(key, sys, config)
case "Process":
res, err = gossConfig.Processes.AppendSysResource(key, sys, config)
case "Service":
res, err = gossConfig.Services.AppendSysResource(key, sys, config)
case "User":
res, err = gossConfig.Users.AppendSysResource(key, sys, config)
case "Gossfile":
res, err = gossConfig.Gossfiles.AppendSysResource(key, sys, config)
case "KernelParam":
res, err = gossConfig.KernelParams.AppendSysResource(key, sys, config)
case "Mount":
res, err = gossConfig.Mounts.AppendSysResource(key, sys, config)
case "Interface":
res, err = gossConfig.Interfaces.AppendSysResource(key, sys, config)
case "HTTP":
res, err = gossConfig.HTTPs.AppendSysResource(key, sys, config)
default:
err = fmt.Errorf("undefined resource name: %s", resourceName)
}
if err != nil {
return err
}
resourcePrint(fileName, res, config.AnnounceToCLI)
return nil
}
// AutoAddResources is a simple wrapper to add multiple resources
func AutoAddResources(fileName string, keys []string, c *util.Config) error {
var err error
outStoreFormat, err = getStoreFormatFromFileName(fileName)
if err != nil {
return err
}
var gossConfig GossConfig
if _, err = os.Stat(fileName); err == nil {
gossConfig, err = ReadJSON(fileName)
if err != nil {
return err
}
} else {
gossConfig = *NewGossConfig()
}
sys := system.New(c.PackageManager)
for _, key := range keys {
if err := AutoAddResource(fileName, gossConfig, key, c, sys); err != nil {
return err
}
}
return WriteJSON(fileName, gossConfig)
}
// AutoAddResource adds a single resource to fileName with automatic detection of the type of resource
func AutoAddResource(fileName string, gossConfig GossConfig, key string, c *util.Config, sys *system.System) error {
// file
if strings.Contains(key, "/") {
res, _, ok, err := gossConfig.Files.AppendSysResourceIfExists(key, sys)
if err != nil {
return err
}
if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
}
}
// group
if res, _, ok, err := gossConfig.Groups.AppendSysResourceIfExists(key, sys); err != nil {
return err
} else if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
}
// package
if res, _, ok, err := gossConfig.Packages.AppendSysResourceIfExists(key, sys); err != nil {
return err
} else if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
}
// port
if res, _, ok, err := gossConfig.Ports.AppendSysResourceIfExists(key, sys); err != nil {
return err
} else if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
}
// process
if res, sysres, ok, err := gossConfig.Processes.AppendSysResourceIfExists(key, sys); err != nil {
return err
} else if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
ports := system.GetPorts(true)
pids, _ := sysres.Pids()
for _, pid := range pids {
pidS := strconv.Itoa(pid)
for port, entries := range ports {
for _, entry := range entries {
if entry.Pid == pidS {
// port
if res, _, ok, err := gossConfig.Ports.AppendSysResourceIfExists(port, sys); err != nil {
return err
} else if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
}
}
}
}
}
}
// Service
if res, _, ok, err := gossConfig.Services.AppendSysResourceIfExists(key, sys); err != nil {
return err
} else if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
}
// user
if res, _, ok, err := gossConfig.Users.AppendSysResourceIfExists(key, sys); err != nil {
return err
} else if ok {
resourcePrint(fileName, res, c.AnnounceToCLI)
}
return nil
}