This repository was archived by the owner on Dec 10, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrender_cmd.go
More file actions
108 lines (81 loc) · 2.66 KB
/
Copy pathrender_cmd.go
File metadata and controls
108 lines (81 loc) · 2.66 KB
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
package main
import (
"encoding/json"
"github.com/BurntSushi/toml"
"github.com/creasty/defaults"
"github.com/imdario/mergo"
"github.com/qri-io/jsonschema"
"gopkg.in/urfave/cli.v1"
)
func renderCmd(c *cli.Context) error {
if len(c.Args()) < 1 {
l.Panicln("This command requires at least one definition file path")
}
definitionPath := c.Args().Get(0)
l.Println("Definition file path:", definitionPath)
definition := unmarshalDefinitionFile(definitionPath)
if errs := validateDefinition(definition); errs != nil {
errJSON, _ := json.MarshalIndent(errs, "", " ")
l.Fatalf("Error detected in definition file: %s", errJSON)
}
resolveIncludeVars(definition.Vars.Include, definition.Vars.Global)
for _, template := range definition.Templates {
l.Printf("Processing template: %s", template.String())
resolveIncludeVars(template.IncludeVars, template.LocalVars)
if err := mergo.Merge(template.LocalVars, definition.Vars.Global, mergo.WithOverride); err != nil {
l.Fatalf("Problem merging variables: %s", err)
}
renderFile(template)
}
l.Printf("Finished rendering templates!")
return nil
}
func unmarshalDefinitionFile(path string) *definition {
d := &definition{}
if _, err := toml.DecodeFile(path, d); err != nil {
l.Fatalf("Problem decoding TOML file: %s", err)
}
if err := defaults.Set(d); err != nil {
l.Fatalf("Problem setting up default values: %s", err)
}
return d
}
func validateDefinition(d *definition) []jsonschema.ValError {
rs := &jsonschema.RootSchema{}
if err := json.Unmarshal(definitionSchemaData, rs); err != nil {
l.Fatalf("Error unmarshaling schema: %s", err)
}
l.Printf("Validating parsed definition file: %s", d.String())
json := d.MarshalJSON()
if errs, _ := rs.ValidateBytes(json); len(errs) > 0 {
return errs
}
return nil
}
func validateIncludeVars(v *includeVars) []jsonschema.ValError {
rs := &jsonschema.RootSchema{}
if err := json.Unmarshal(includeVarsSchemaData, rs); err != nil {
l.Fatalf("Error unmarshaling schema: %s", err)
}
l.Printf("Validating include vars file: %s", v.String())
json := v.MarshalJSON()
if errs, _ := rs.ValidateBytes(json); len(errs) > 0 {
return errs
}
return nil
}
func resolveIncludeVars(include *[]string, global *map[string]interface{}) {
if include != nil && len(*include) > 0 {
for _, path := range *include {
v := &includeVars{}
if _, err := toml.DecodeFile(path, v); err != nil {
l.Fatalf("Problem decoding TOML vars file: %s", err)
}
l.Printf("Loaded global vars file: %s", v.String())
validateIncludeVars(v)
if err := mergo.Merge(global, v.Vars, mergo.WithOverride); err != nil {
l.Fatalf("Problem merging variables: %s", err)
}
}
}
}