-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv-replace.go
More file actions
61 lines (52 loc) · 1.46 KB
/
env-replace.go
File metadata and controls
61 lines (52 loc) · 1.46 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
// env-replace.go is like envsubst but (a) slightly more portable, (b) 100%
// written by AI, and (c) pukes out "<no value>" when a key isn't found so we
// can tell if there's a missing env var more easily.
//
// Input can be from a file or stdin. The format of env vars must be
// "{{.VAR}}". This isn't bash, people.
package main
import (
"flag"
"io"
"os"
"strings"
"text/template"
)
func main() {
// 1. Setup flags
filePath := flag.String("f", "", "Path to the template file (default: read from stdin)")
flag.Parse()
// 2. Load Environment Variables into a map
envMap := make(map[string]string)
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
if len(pair) == 2 {
envMap[pair[0]] = pair[1]
}
}
// 3. Determine the input source
var input []byte
var err error
if *filePath != "" {
input, err = os.ReadFile(*filePath)
} else {
input, err = io.ReadAll(os.Stdin)
}
if err != nil {
os.Stderr.WriteString("Error reading input: " + err.Error() + "\n")
os.Exit(1)
}
// 4. Parse and Execute
// We use "." in the template to access the map keys,
// but we can map the root to the envMap directly.
tmpl, err := template.New("env").Parse(string(input))
if err != nil {
os.Stderr.WriteString("Error parsing template: " + err.Error() + "\n")
os.Exit(1)
}
// Execute against the envMap
if err := tmpl.Execute(os.Stdout, envMap); err != nil {
os.Stderr.WriteString("Error executing template: " + err.Error() + "\n")
os.Exit(1)
}
}