-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
141 lines (129 loc) · 3.78 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"strings"
)
// Build information
var (
BuildDate string
BuildVersion string
)
func usage() {
fmt.Fprintf(os.Stderr, "%s v%s built %s\n\n", os.Args[0], BuildVersion, BuildDate)
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " %s [options...] <templates...>\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "where <templates...> may be one or more template files or directories.")
fmt.Fprintf(os.Stderr, "Directories are processed only single depth.")
fmt.Fprintf(os.Stderr, "")
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
}
func main() {
dataFile := flag.String("values", "", "Comma-separated paths to YAML files containing values (only top-level keys are merged)")
execMapFile := flag.String("exec-map-file", "", "File from which exec rules can be read")
onError := flag.String("on-error", "die", "What to do on render error: die, ignore")
outFile := flag.String("out", "-", "Output file (or '-' for STDOUT)")
plugDir := flag.String("plugins-dir", os.Getenv("TPL_PLUGINS"), "Directory from which plugins implementing custom text/template funcs can be loaded dynamically")
preloadFiles := make(stringSliceFlag, 0)
flag.Var(&preloadFiles, "preload", "Additional files to preload")
valueMap := make(valueMapFlag)
flag.Var(&valueMap, "value", "Additional values to inject in the form of key=value")
// Parse command line flags
flag.Usage = usage
flag.Parse()
dataFiles := []string{}
if *dataFile != "" {
dataFiles = strings.Split(*dataFile, ",")
}
allValues := make(Values)
for _, fname := range dataFiles {
if err := allValues.LoadFile(fname); err != nil {
log.Fatal(err)
}
}
if len(valueMap) > 0 {
log.Printf("Loading values from command line\n")
for km, vm := range valueMap {
allValues[km] = vm
}
}
if flag.NArg() < 1 {
usage()
log.Fatalln("At least one <template> path is required.")
}
fm := funcMap()
fm["exec"] = func(name string, args ...string) string {
log.Fatalf("the 'exec' template function is disabled; you must specify -exec-map-file=FILE to enable it")
return ""
}
if *execMapFile != "" {
exmap, err := loadExecMap(*execMapFile)
if err != nil {
log.Fatal(err)
}
// log.Printf("%+v\n", exmap)
fm["exec"] = func(name string, args ...string) string {
exset, err := exmap.Get(name)
if err != nil {
log.Printf("could not exec %q %v: %v", name, args, err)
return ""
}
var stdin io.Reader
if exset.Stdin {
stdin = strings.NewReader(args[len(args)-1])
args = args[:len(args)-1]
}
stdout, stderr, err := exset.Run(args, stdin)
if stderr != "" {
log.Printf("exec %q %v, STDERR output was: %s", name, args, stderr)
}
if err != nil {
log.Printf("exec %q %v failed with error: %v", name, args, err)
return ""
}
if exset.Stdout {
return stdout
}
if exset.Stderr {
return stderr
}
return ""
}
}
// Respect `onError` when loading plugins
var logf func(string, ...interface{})
if *onError != "ignore" {
logf = log.Fatalf
} else {
logf = log.Printf
}
if plugDir != nil && *plugDir != "" {
if _, err := os.Stat(*plugDir); os.IsNotExist(err) {
logf("could not search for plugins in directory %q: %v", *plugDir, err)
} else if soFiles, err := filepath.Glob(path.Join(*plugDir, "*.so")); err != nil {
logf("could not search for plugins in directory %q: %v", *plugDir, err)
} else {
for _, so := range soFiles {
if err := loadPlugin(&so, &fm); err != nil {
logf("could not load plugin %q: %v", so, err)
}
}
}
}
r := &Renderer{
FuncMap: fm,
Inputs: flag.Args(),
PreloadFiles: preloadFiles,
StopOnError: (*onError != "ignore"),
}
if err := r.Execute(*outFile, allValues); err != nil {
log.Fatal(err)
}
}