forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
246 lines (214 loc) · 5.44 KB
/
store.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package goss
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"gopkg.in/yaml.v2"
"github.com/aelsabbahy/goss/resource"
"github.com/urfave/cli"
)
const (
UNSET = iota
JSON
YAML
)
var OutStoreFormat = UNSET
var TemplateFilter func(data []byte) []byte
var debug = false
func getStoreFormatFromFileName(f string) int {
ext := filepath.Ext(f)
switch ext {
case ".json":
return JSON
case ".yaml", ".yml":
return YAML
default:
log.Fatalf("Unknown file extension: %v", ext)
}
return 0
}
func getStoreFormatFromData(data []byte) int {
var v interface{}
if err := unmarshalJSON(data, &v); err == nil {
return JSON
}
if err := unmarshalYAML(data, &v); err == nil {
return YAML
}
log.Fatalf("Unable to determine format from content")
return 0
}
// Reads json file returning GossConfig
func ReadJSON(filePath string) GossConfig {
file, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("File error: %v\n", err)
os.Exit(1)
}
return ReadJSONData(file, false)
}
type TmplVars struct {
Vars map[string]interface{}
}
func (t *TmplVars) Env() map[string]string {
env := make(map[string]string)
for _, i := range os.Environ() {
sep := strings.Index(i, "=")
env[i[0:sep]] = i[sep+1:]
}
return env
}
func varsFromFile(varsFile string) (map[string]interface{}, error) {
var vars map[string]interface{}
if varsFile == "" {
return vars, nil
}
data, err := ioutil.ReadFile(varsFile)
if err != nil {
return vars, err
}
format := getStoreFormatFromData(data)
if err := unmarshal(data, &vars, format); err != nil {
return vars, err
}
return vars, nil
}
// Reads json byte array returning GossConfig
func ReadJSONData(data []byte, detectFormat bool) GossConfig {
if TemplateFilter != nil {
data = TemplateFilter(data)
if debug {
fmt.Println("DEBUG: file after text/template render")
fmt.Println(string(data))
}
}
format := OutStoreFormat
if detectFormat == true {
format = getStoreFormatFromData(data)
}
gossConfig := NewGossConfig()
// Horrible, but will do for now
if err := unmarshal(data, gossConfig, format); err != nil {
// FIXME: really dude.. this is so ugly
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
return *gossConfig
}
// Reads json file recursively returning string
func RenderJSON(c *cli.Context) string {
filePath := c.GlobalString("gossfile")
varsFile := c.GlobalString("vars")
debug = c.Bool("debug")
TemplateFilter = NewTemplateFilter(varsFile)
path := filepath.Dir(filePath)
OutStoreFormat = getStoreFormatFromFileName(filePath)
gossConfig := mergeJSONData(ReadJSON(filePath), 0, path)
b, err := marshal(gossConfig)
if err != nil {
log.Fatalf("Error rendering: %v\n", err)
}
return string(b)
}
func mergeJSONData(gossConfig GossConfig, depth int, path string) GossConfig {
depth++
if depth >= 50 {
fmt.Println("Error: Max depth of 50 reached, possibly due to dependency loop in goss file")
os.Exit(1)
}
// Our return gossConfig
ret := *NewGossConfig()
ret = mergeGoss(ret, gossConfig)
// Sort the gossfiles to ensure consistent ordering
var keys []string
for k, _ := range gossConfig.Gossfiles {
keys = append(keys, k)
}
sort.Strings(keys)
// Merge gossfiles in sorted order
for _, k := range keys {
g := gossConfig.Gossfiles[k]
var fpath string
if strings.HasPrefix(g.ID(), "/") {
fpath = g.ID()
} else {
fpath = filepath.Join(path, g.ID())
}
matches, err := filepath.Glob(fpath)
if err != nil {
fmt.Printf("Error in expanding glob pattern: \"%s\"\n", err.Error())
os.Exit(1)
}
for _, match := range matches {
fdir := filepath.Dir(match)
j := mergeJSONData(ReadJSON(match), depth, fdir)
ret = mergeGoss(ret, j)
}
}
return ret
}
func WriteJSON(filePath string, gossConfig GossConfig) error {
jsonData, err := marshal(gossConfig)
if err != nil {
log.Fatalf("Error writing: %v\n", err)
}
// check if the auto added json data is empty before writing to file.
emptyConfig := *NewGossConfig()
emptyData, err := marshal(emptyConfig)
if err != nil {
log.Fatalf("Error writing: %v\n", err)
}
if string(emptyData) == string(jsonData) {
log.Printf("Can't write empty configuration file. Please check resource name(s).")
return nil
}
if err := ioutil.WriteFile(filePath, jsonData, 0644); err != nil {
log.Fatalf("Error writing: %v\n", err)
}
return nil
}
func resourcePrint(fileName string, res resource.ResourceRead) {
resMap := map[string]resource.ResourceRead{res.ID(): res}
oj, _ := marshal(resMap)
typ := reflect.TypeOf(res)
typs := strings.Split(typ.String(), ".")[1]
fmt.Printf("Adding %s to '%s':\n\n%s\n\n", typs, fileName, string(oj))
}
func marshal(gossConfig interface{}) ([]byte, error) {
switch OutStoreFormat {
case JSON:
return marshalJSON(gossConfig)
case YAML:
return marshalYAML(gossConfig)
default:
return nil, fmt.Errorf("StoreFormat unset")
}
}
func unmarshal(data []byte, v interface{}, storeFormat int) error {
switch storeFormat {
case JSON:
return unmarshalJSON(data, v)
case YAML:
return unmarshalYAML(data, v)
default:
return fmt.Errorf("StoreFormat unset")
}
}
func marshalJSON(gossConfig interface{}) ([]byte, error) {
return json.MarshalIndent(gossConfig, "", " ")
}
func unmarshalJSON(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func marshalYAML(gossConfig interface{}) ([]byte, error) {
return yaml.Marshal(gossConfig)
}
func unmarshalYAML(data []byte, v interface{}) error {
return yaml.Unmarshal(data, v)
}