-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterrastache.go
53 lines (45 loc) · 1.1 KB
/
terrastache.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/cbroglie/mustache"
"github.com/hashicorp/terraform/helper/variables"
)
func parseCmdArgs() (string, map[string]interface{}, error) {
vars := map[string]interface{}{}
var file string
flag.Var((*variables.Flag)(&vars), "var", "variables")
flag.Var((*variables.FlagFile)(&vars), "var-file", "variable file")
flag.StringVar(&file, "template", "", "template file")
flag.Parse()
if file == "" {
return "", nil, fmt.Errorf("must specify a template file using the template parameter")
}
b, err := ioutil.ReadFile(file)
if err != nil {
return "", nil, err
}
return string(b), vars, nil
}
func renderTemplate(template string, vars map[string]interface{}) (string, error) {
data, err := mustache.Render(template, vars)
if err != nil {
return "", err
}
return data, nil
}
func main() {
template, vars, err := parseCmdArgs()
if err != nil {
fmt.Println("Error: " + err.Error())
os.Exit(1)
}
rendered, err := renderTemplate(template, vars)
if err != nil {
fmt.Println("Error: " + err.Error())
os.Exit(1)
}
fmt.Println(rendered)
}