-
Notifications
You must be signed in to change notification settings - Fork 0
/
mustache-then-exec.go
164 lines (135 loc) · 3.88 KB
/
mustache-then-exec.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
arg "github.com/alexflint/go-arg"
"github.com/cbroglie/mustache"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
)
type template struct {
source string
regex *regexp.Regexp
replacement string
}
type args struct {
AllowMissing bool `arg:"-a,--allow-missing" help:"whether to allow missing variables (default: false)"`
Templates []string `arg:"-t,--template,separate" placeholder:"TEMPLATE" help:"path to a template to be rendered"`
GlobTemplates []string `arg:"-g,--glob-template,separate" placeholder:"GLOB" help:"glob for templates to be rendered"`
Binary string `arg:"positional,required" placeholder:"BINARY" help:"the binary to run after rendering the templates"`
Args []string `arg:"positional" placeholder:"ARG" help:"arguments to the binary to run after rendering the templates"`
}
func fail(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "Error: "+format+"\n", args...)
os.Exit(1)
}
func failErr(err error) {
fail("%s", err)
}
func environmentAsMap() map[string]string {
envMap := make(map[string]string)
for _, v := range os.Environ() {
kv := strings.SplitN(v, "=", 2)
envMap[kv[0]] = kv[1]
}
return envMap
}
func renderTemplate(template template, environment map[string]string) error {
outputFileName := template.source
if template.regex != nil {
outputFileName = template.regex.ReplaceAllString(template.source, template.replacement)
}
fmt.Printf("Rendering template: %s; output: %s\n", template.source, outputFileName)
renderedTemplate, err := mustache.RenderFile(template.source, environment)
if err != nil {
return err
}
fileInfo, err := os.Stat(template.source)
if err != nil {
return err
}
err = ioutil.WriteFile(outputFileName, []byte(renderedTemplate), fileInfo.Mode())
if err != nil {
return err
}
return nil
}
func parseArgs() args {
var args args
arg.MustParse(&args)
mustache.AllowMissingVariables = args.AllowMissing
return args
}
func parseTemplate(templateFileName string) template {
currentString := []rune{}
splits := []string{}
isEscaped := false
for _, c := range templateFileName {
if c == '\\' {
isEscaped = true
currentString = append(currentString[:], c)
} else if c == ':' {
if isEscaped {
currentString = append(currentString[0:len(currentString)-1], c)
} else {
splits = append(splits, string(currentString))
currentString = []rune{}
}
isEscaped = false
} else {
currentString = append(currentString, c)
isEscaped = false
}
}
splits = append(splits, string(currentString))
if len(splits) == 1 {
return template{source: splits[0]}
} else if len(splits) != 3 {
fail("Template '%s' is invalid (have you escaped colons properly?)", templateFileName)
}
regex, err := regexp.Compile(splits[1])
if err != nil {
failErr(err)
}
return template{
source: splits[0],
regex: regex,
replacement: splits[2],
}
}
func main() {
args := parseArgs()
environment := environmentAsMap()
templates := []template{}
for _, templateGlob := range args.GlobTemplates {
parsedTemplateGlob := parseTemplate(templateGlob)
matchingTemplateFileNames, err := filepath.Glob(parsedTemplateGlob.source)
if err != nil {
failErr(err)
}
for _, matchingTemplateFileName := range matchingTemplateFileNames {
templates = append(templates, template{
source: matchingTemplateFileName,
regex: parsedTemplateGlob.regex,
replacement: parsedTemplateGlob.replacement,
})
}
}
for _, template := range args.Templates {
templates = append(templates, parseTemplate(template))
}
for _, template := range templates {
err := renderTemplate(template, environment)
if err != nil {
failErr(err)
}
}
argv := append([]string{args.Binary}, args.Args...)
err := syscall.Exec(args.Binary, argv, os.Environ())
if err != nil {
fail("Error running %s: %s", argv, err)
}
}