/
models.go
162 lines (142 loc) · 3.49 KB
/
models.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
package internal
import (
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/creasty/defaults"
"github.com/qri-io/jsonschema"
log "github.com/sirupsen/logrus"
)
var definitionSchemaData = []byte(`{
"title": "Definition",
"type": "object",
"properties": {
"vars": {
"type": "object",
"properties": {
"include": {
"type": "array",
"default" : []
},
"global": {
"type": "object",
"default" : {}
}
}
},
"templates": {
"type": "array",
"items": {
"type": "object",
"required": ["src", "dest"],
"properties": {
"src": {
"type": "string"
},
"dest": {
"type": "string"
},
"local_vars": {
"type": "object"
},
"include_vars": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
},
"required": ["templates"]
}`)
var includeVarsSchemaData = []byte(`{
"title": "IncludeVars",
"type": "object",
"properties": {
"vars": {
"type": "object"
}
},
"required": ["vars"]
}`)
type definition struct {
Vars vars `toml:"vars" json:"vars"`
Templates []template `toml:"templates" json:"templates"`
}
func newDefinition(path string) *definition {
d := &definition{}
if _, err := toml.DecodeFile(path, d); err != nil {
log.Fatalf("Problem decoding TOML file: %s", err)
}
if err := defaults.Set(d); err != nil {
log.Fatalf("Problem setting up default values: %s", err)
}
rs := &jsonschema.RootSchema{}
if err := json.Unmarshal(definitionSchemaData, rs); err != nil {
log.Fatalf("Error un-marshaling schema: %s", err)
}
log.Infof("Validating parsed definition file: %s", d.String())
j := d.MarshalJSON()
if errs, _ := rs.ValidateBytes(j); len(errs) > 0 {
log.Fatal(errs)
//return errs
}
return d
}
func (d *definition) String() string {
j, err := json.MarshalIndent(d, "", " ")
if err != nil {
log.Fatalf("Problem marshalling to JSON: %s", err)
}
return fmt.Sprintf("%s", j)
}
func (d *definition) MarshalJSON() []byte {
j, err := json.Marshal(d)
if err != nil {
log.Fatalf("Problem marshalling to JSON: %s", err)
}
return j
}
type vars struct {
Include *[]string `toml:"include" json:"include" default:"[]"`
Global *map[string]interface{} `toml:"global" json:"global" default:"{}"`
}
type includeVars struct {
Vars map[string]interface{} `toml:"vars" json:"vars"`
}
func (v *includeVars) MarshalJSON() []byte {
j, err := json.Marshal(v)
if err != nil {
log.Fatalf("Problem marshalling to JSON: %s", err)
}
return j
}
func (v *includeVars) String() string {
j, err := json.MarshalIndent(v, "", " ")
if err != nil {
log.Fatalf("Problem marshalling to JSON: %s", err)
}
return fmt.Sprintf("%s", j)
}
type template struct {
Src string `toml:"src" json:"src"`
Dest string `toml:"dest" json:"dest"`
LocalVars *map[string]interface{} `toml:"local_vars" json:"local_vars" default:"{}"`
IncludeVars *[]string `toml:"include_vars" json:"include_vars" default:"[]"`
}
func (t *template) MarshalJSON() []byte {
j, err := json.Marshal(t)
if err != nil {
log.Fatalf("Problem marshalling to JSON: %s", err)
}
return j
}
func (t *template) String() string {
j, err := json.MarshalIndent(t, "", " ")
if err != nil {
log.Fatalf("Problem marshalling to JSON: %s", err)
}
return fmt.Sprintf("%s", j)
}