-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
121 lines (103 loc) · 2.89 KB
/
main.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
package main
import (
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"github.com/spf13/cobra"
)
// options
var output string
var missingKey = "default"
func die(err error) {
if err != nil {
log.Fatal(err)
}
}
var RootCmd = &cobra.Command{
Use: "envtpl",
Short: "Render go templates from environment variables",
Long: `Render go templates from environment variables.`,
Run: func(cmd *cobra.Command, args []string) {
var t *template.Template
var err error
// load template; if an argument is not specified, default to stdin
if len(args) > 0 {
t, err = parseFiles(args...)
die(err)
} else {
bytes, err := ioutil.ReadAll(os.Stdin)
die(err)
t, err = parse(string(bytes))
die(err)
}
// get environment variables to supply to the template
env := readEnv()
// get writer for rendered output; if an output file is not
// specified, default to stdout
var w io.Writer
if output == "" {
w = os.Stdout
} else {
f, err := os.Create(output)
die(err)
defer f.Close()
w = io.Writer(f)
}
// set error handling strategy for missing keys
if missingKey != "default" {
t = t.Option("missingkey=" + missingKey)
}
// render the template
err = t.Execute(w, env)
die(err)
},
}
// returns a new template with custom function maps
// (sprig and environment key prefix matcher) applied
func parse(s string) (*template.Template, error) {
return template.New("").Funcs(sprig.TxtFuncMap()).Funcs(customFuncMap()).Parse(s)
}
// returns a new template with custom function maps
// (sprig and environment key prefix matcher) applied
func parseFiles(files ...string) (*template.Template, error) {
return template.New(filepath.Base(files[0])).Funcs(sprig.TxtFuncMap()).Funcs(customFuncMap()).ParseFiles(files...)
}
// returns map of environment variables
func readEnv() (env map[string]string) {
env = make(map[string]string)
for _, setting := range os.Environ() {
pair := strings.SplitN(setting, "=", 2)
env[pair[0]] = pair[1]
}
return
}
// custom function that returns key, value for all environment variable keys matching prefix
// (see original envtpl: https://pypi.org/project/envtpl/)
func environment(prefix string) map[string]string {
env := make(map[string]string)
for _, setting := range os.Environ() {
pair := strings.SplitN(setting, "=", 2)
if strings.HasPrefix(pair[0], prefix) {
env[pair[0]] = pair[1]
}
}
return env
}
// returns custom template functions map
func customFuncMap() template.FuncMap {
var functionMap = map[string]interface{}{
"environment": environment,
}
return template.FuncMap(functionMap)
}
func main() {
RootCmd.Flags().StringVarP(&output, "output", "o", output, "The rendered output file")
RootCmd.Flags().StringVarP(&missingKey, "missingkey", "m", missingKey, "Strategy for dealing with missing keys: [default|zero|error]")
err := RootCmd.Execute()
die(err)
}