-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathstore.go
164 lines (138 loc) · 3.46 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
package goss
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"gopkg.in/yaml.v2"
"github.com/aelsabbahy/goss/resource"
)
const (
JSON = iota
YAML
UNSET
)
var StoreFormat = UNSET
func setStoreFormatFromFileName(f string) {
ext := filepath.Ext(f)
switch ext {
case ".json":
StoreFormat = JSON
case ".yaml", ".yml":
StoreFormat = YAML
default:
log.Fatalf("Unknown file extension: %v", ext)
}
}
func setStoreFormatFromData(data []byte) {
var v interface{}
if err := unmarshalJSON(data, &v); err == nil {
StoreFormat = JSON
return
}
if err := unmarshalYAML(data, &v); err == nil {
StoreFormat = YAML
return
}
log.Fatalf("Unable to determine format from content")
}
// Reads json file returning GossConfig
func ReadJSON(filePath string) GossConfig {
// FIXME: Any problems with this?
setStoreFormatFromFileName(filePath)
file, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("File error: %v\n", err)
os.Exit(1)
}
return ReadJSONData(file)
}
// Reads json byte array returning GossConfig
func ReadJSONData(data []byte) GossConfig {
setStoreFormatFromData(data)
gossConfig := NewGossConfig()
// Horrible, but will do for now
if err := unmarshal(data, gossConfig); 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(filePath string) string {
path := filepath.Dir(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)
}
for _, g := range gossConfig.Gossfiles {
fpath := filepath.Join(path, g.ID())
fdir := filepath.Dir(fpath)
j := mergeJSONData(ReadJSON(fpath), depth, fdir)
gossConfig = mergeGoss(gossConfig, j)
}
return gossConfig
}
func WriteJSON(filePath string, gossConfig GossConfig) error {
jsonData, err := marshal(gossConfig)
if err != nil {
log.Fatalf("Error writing: %v\n", err)
}
if err := ioutil.WriteFile(filePath, jsonData, 0644); err != nil {
log.Fatalf("Error writing: %v\n", err)
}
return nil
}
func resourcePrint(fileName string, res resource.IDer) {
resMap := map[string]resource.IDer{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 StoreFormat {
case JSON:
return marshalJSON(gossConfig)
case YAML:
return marshalYAML(gossConfig)
default:
return nil, fmt.Errorf("StoreFormat unset")
}
}
func unmarshal(data []byte, v interface{}) 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)
}