-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
gucci.go
155 lines (126 loc) · 3 KB
/
gucci.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
package main
import (
"fmt"
"github.com/imdario/mergo"
"io"
"log"
"os"
"text/template"
"github.com/urfave/cli"
)
var logger = log.New(os.Stderr, "", 0)
const (
flagSetVar = "s"
flagSetVarLong = flagSetVar + ",set-var"
flagVarsFile = "f"
flagVarsFileLong = flagVarsFile + ",vars-file"
flagSetOpt = "o"
flagSetOptLong = flagSetOpt + ",tpl-opt"
)
var (
AppVersion = "0.0.0-dev.0" // Injected
)
func main() {
app := cli.NewApp()
app.Name = "gucci"
app.Usage = "simple CLI Go lang templating"
app.UsageText = app.Name + " [options] [template]"
app.Version = AppVersion
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: flagSetVarLong,
Usage: "A `KEY=VALUE` pair variable",
},
cli.StringFlag{
Name: flagVarsFileLong,
Usage: "A json or yaml `FILE` from which to read variables",
},
cli.StringSliceFlag{
Name: flagSetOptLong,
Usage: "A template option (`KEY=VALUE`) to be applied",
Value: &cli.StringSlice{"missingkey=error"},
},
}
app.Action = func(c *cli.Context) error {
tplPath := c.Args().First()
vars, err := loadVariables(c)
if err != nil {
return cli.NewExitError(err, 1)
}
err = run(tplPath, vars, c.StringSlice(flagSetOpt))
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
app.Run(os.Args)
}
func loadInputVarsFile(c *cli.Context) (map[string]interface{}, error) {
var vars map[string]interface{}
varsFilePath := c.String(flagVarsFile)
if varsFilePath != "" {
v, err := loadVarsFile(varsFilePath)
if err != nil {
return nil, err
}
vars = v
} else {
vars = make(map[string]interface{})
}
return vars, nil
}
func loadInputVarsOptions(c *cli.Context) (map[string]interface{}, error) {
vars := make(map[string]interface{})
for _, varStr := range c.StringSlice(flagSetVar) {
key, val := getKeyVal(varStr)
varMap := keyValToMap(key, val)
err := mergo.Merge(&vars, varMap, mergo.WithOverride)
if err != nil {
return nil, err
}
}
return vars, nil
}
func loadVariables(c *cli.Context) (map[string]interface{}, error) {
vars, err := loadInputVarsFile(c)
if err != nil {
return nil, err
}
envVars := env()
err = mergo.Merge(&vars, envVars, mergo.WithOverride)
if err != nil {
return nil, err
}
optVars, err := loadInputVarsOptions(c)
if err != nil {
return nil, err
}
err = mergo.Merge(&vars, optVars, mergo.WithOverride)
if err != nil {
return nil, err
}
return vars, nil
}
func executeTemplate(valuesIn map[string]interface{}, out io.Writer, tpl *template.Template, opt []string) error {
tpl.Option(opt...)
err := tpl.Execute(out, valuesIn)
if err != nil {
return fmt.Errorf("Failed to parse standard input: %v", err)
}
return nil
}
func run(tplPath string, vars map[string]interface{}, tplOpt []string) error {
tpl, err := loadTemplateFileOrStdin(tplPath)
if err != nil {
return err
}
err = executeTemplate(vars, os.Stdout, tpl, tplOpt)
if err != nil {
return err
}
return nil
}
func logError(msg string, err error) {
logger.Println(msg)
logger.Println(err.Error())
}