-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtpl.go
More file actions
67 lines (57 loc) · 1.31 KB
/
tpl.go
File metadata and controls
67 lines (57 loc) · 1.31 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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"flag"
"text/template"
"path/filepath"
"github.com/Masterminds/sprig"
"gopkg.in/yaml.v2"
)
var f = flag.String("template", "/input/template", "The go template to use.")
var d = flag.String("data", "/input/data", "The YAML file to read configuration from.")
var o = flag.String("output", "", "This is the file that the output will be writen to. If not set, will print to stdout")
// ExecuteTemplate asdf
func ExecuteTemplate(b []byte, o io.Writer, t string) error {
tpl, err := template.New(filepath.Base(t)).Funcs(sprig.TxtFuncMap()).ParseFiles(t)
if err != nil {
return fmt.Errorf("Error parsing template(s): %v", err)
}
var values map[string]interface{}
err = yaml.Unmarshal(b, &values)
if err != nil {
return fmt.Errorf("Failed to parse standard input: %v", err)
}
err = tpl.Execute(o, values)
if err != nil {
return fmt.Errorf("Failed to parse standard input: %v", err)
}
return nil
}
func main() {
flag.Parse()
var out *os.File
var err error
if *o == "" {
out = os.Stdout
} else {
out, err = os.Create(*o)
if err != nil {
log.Println(err)
os.Exit(1)
}
}
d, err := ioutil.ReadFile(*d)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = ExecuteTemplate(d, out, *f)
if err != nil {
log.Println(err)
os.Exit(1)
}
}