-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (64 loc) · 1.55 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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"text/template"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func replaceEnvs(match []byte) []byte {
var varContent = string(match)
// remove wrapping chars and split
var varParts = strings.Split(varContent[2:len(varContent)-1], ":-")
// do we have the env?
var envValue = os.Getenv(varParts[0])
if len(varParts) == 2 && envValue == "" {
envValue = varParts[1]
}
return []byte(envValue)
}
func main() {
programArgs := os.Args[1:]
var templateString = ""
if len(programArgs) == 0 {
scanner := bufio.NewScanner(os.Stdin)
buffer := bytes.Buffer{}
for scanner.Scan() {
buffer.WriteString(scanner.Text() + "\n")
}
templateString = buffer.String()
} else if len(programArgs) == 1 {
fileContent, err := ioutil.ReadFile(programArgs[0])
check(err)
templateString = string(fileContent)
} else {
panic(errors.New("you need to either pass a filename as first arg or pass content through stdin"))
}
// first, execute go templateString
var envVars = make(map[string]string)
for _, e := range os.Environ() {
envVarPair := strings.Split(e, "=")
envVars[envVarPair[0]] = envVarPair[1]
}
t, err := template.New("tmpl").Parse(templateString)
if err != nil {
panic(err)
}
var templateBytes bytes.Buffer
terr := t.Execute(&templateBytes, envVars)
if terr != nil {
panic(terr)
}
envRegex, _ := regexp.Compile("\\$\\{.*\\}")
envReplaced := envRegex.ReplaceAllFunc(templateBytes.Bytes(), replaceEnvs)
fmt.Println(string(envReplaced))
}