-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomplate.go
40 lines (33 loc) · 855 Bytes
/
tomplate.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
package main
import (
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
"os"
"text/template"
)
func main() {
// Usage info
if len(os.Args) < 3 {
log.Print("Usage: tomplate values.yaml template.txt")
return
}
// Read in data
valuesfile := os.Args[1]
valuesdata, err := ioutil.ReadFile(valuesfile)
if err != nil { log.Fatal(err) }
// Injest data
var values map[string]interface{}
err = yaml.Unmarshal(valuesdata, &values)
if err != nil { log.Fatal(err) }
// Read in template
templatefile := os.Args[2]
templatedata, err := ioutil.ReadFile(templatefile)
if err != nil { log.Fatal(err) }
// Validate template
tmpl, err := template.New("output").Parse(string(templatedata))
if err != nil { log.Fatal(err) }
// Apply data to template
err = tmpl.Execute(os.Stdout, values)
if err != nil { log.Fatal(err) }
}