-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema_writer.go
256 lines (214 loc) · 5.16 KB
/
schema_writer.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
247
248
249
250
251
252
253
254
255
256
package jsonschema
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
const (
vsCodeConfigDirectory = "./.vscode"
vsCodeConfigFile = "settings.json"
// default permissions
dirPerm os.FileMode = 0700
filePerm os.FileMode = 0600
)
// M is a convenience wrapper for JSON object.
type M map[string]interface{}
func writeToFile(w schemaWriter) error {
if err := w.Prepare(); err != nil {
return err
}
return w.Write()
}
var _ schemaWriter = (*vscodeWriter)(nil)
var _ schemaWriter = (*basicWriter)(nil)
type schemaWriter interface {
Prepare() error
Write() error
}
type basicWriter struct{}
func (b basicWriter) Prepare() error { return nil }
func (b basicWriter) Write() error {
return jsonToFile(rootSchema, config.File, filePerm)
}
type file struct {
filename string
perm os.FileMode
exists bool
}
type vscodeWriter struct {
dir, config, schema file
configJSON map[string]interface{}
ignoreConfig bool
}
func (v *vscodeWriter) prepareDirectory() error {
// check if directory exists, retain permission
if stat, err := os.Stat(vsCodeConfigDirectory); err == nil {
if !stat.IsDir() {
return errors.New("a file named '.vscode' exists")
}
// retain directory permission
v.dir.perm = stat.Mode()
v.dir.exists = true
} else {
err := os.MkdirAll(vsCodeConfigDirectory, dirPerm)
if err != nil {
return err
}
v.dir.perm = dirPerm
}
return nil
}
func (v *vscodeWriter) prepareFiles() error {
// check if schema file exists, retain permission
v.schema.filename = filepath.Join(vsCodeConfigDirectory, "caddy_schema.json")
perm, ok, err := permOrDefault(v.schema.filename)
if err != nil {
return err
}
v.schema.exists = ok
v.schema.perm = perm
// check if vscode config file exists, retain permission
v.config.filename = filepath.Join(vsCodeConfigDirectory, vsCodeConfigFile)
perm, ok, err = permOrDefault(v.config.filename)
if err != nil {
return err
}
v.config.exists = ok
v.config.perm = perm
return nil
}
func (v *vscodeWriter) Prepare() error {
if err := v.prepareDirectory(); err != nil {
return err
}
if err := v.prepareFiles(); err != nil {
return err
}
if v.config.exists {
if err := v.loadVsConfig(); err != nil {
return err
}
} else {
v.configJSON = map[string]interface{}{}
}
if err := v.setVsConfig(); err != nil {
return err
}
return nil
}
func (v *vscodeWriter) loadVsConfig() error {
b, err := ioutil.ReadFile(v.config.filename)
if err != nil {
return err
}
return json.Unmarshal(b, &v.configJSON)
}
func (v *vscodeWriter) setJSONConfig() (bool, error) {
const key = "json.schemas"
var schemas []interface{}
if s := v.configJSON[key]; s != nil {
if _, ok := s.([]interface{}); !ok {
return false, errors.New("invalid vscode config, 'json.schemas' not a list")
}
schemas = s.([]interface{})
}
for _, schema := range schemas {
if s, ok := schema.(map[string]interface{}); ok {
if url, ok := s["url"]; ok && url == v.schema.filename {
return false, nil
}
}
}
schemas = append(schemas, M{
"fileMatch": []interface{}{"*caddy*.json"},
"url": v.schema.filename,
})
v.configJSON[key] = schemas
return true, nil
}
func (v *vscodeWriter) setYAMLConfig() (bool, error) {
const key = "yaml.schemas"
var schemas map[string]interface{}
if s := v.configJSON[key]; s != nil {
if _, ok := s.(map[string]interface{}); !ok {
return false, errors.New("invalid vscode config, 'yaml.schemas' not an object")
}
schemas = s.(map[string]interface{})
}
for key := range schemas {
if key == v.schema.filename {
return false, nil
}
}
if schemas == nil {
schemas = make(map[string]interface{})
}
schemas[v.schema.filename] = []interface{}{
"*caddy*.yaml",
"*caddy*.yml",
}
v.configJSON[key] = schemas
return true, nil
}
func (v *vscodeWriter) setVsConfig() error {
var err error
edited := struct{ json, yaml bool }{}
// json
edited.json, err = v.setJSONConfig()
if err != nil {
return err
}
// yaml
edited.yaml, err = v.setYAMLConfig()
if err != nil {
return err
}
if !edited.json && !edited.yaml {
v.ignoreConfig = true
log.Println("vscode config found, ignoring...")
}
return nil
}
func (v *vscodeWriter) Write() error {
err := jsonToFile(rootSchema, v.schema.filename, v.schema.perm)
if err != nil {
return err
}
if !v.ignoreConfig {
return jsonToFile(v.configJSON, v.config.filename, v.config.perm)
}
return nil
}
// jsonToFile writes JSON obj to file.
func jsonToFile(obj interface{}, filename string, perm os.FileMode) error {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perm)
if err != nil {
return err
}
defer f.Close()
encoder := json.NewEncoder(f)
indentSpace := ""
for i := 0; i < config.Indent; i++ {
indentSpace += " "
}
encoder.SetIndent("", indentSpace)
// var err error
err = encoder.Encode(obj)
if err != nil {
return err
}
log.Println(filename, "written.")
return nil
}
func permOrDefault(filename string) (os.FileMode, bool, error) {
if stat, err := os.Stat(filename); err == nil {
if stat.IsDir() {
return 0, false, fmt.Errorf("a directory named '%s' exists", filename)
}
return stat.Mode(), true, nil
}
return filePerm, false, nil
}