-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (49 loc) · 1.37 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
package main
import "os"
import "fmt"
import "bufio"
import "io/ioutil"
import "text/template"
import "encoding/json"
func main() {
paths := os.Args[1:]
if len(paths) != 2 {
fmt.Println("Usage: consul-watch-renderer <template> <destination>")
os.Exit(1)
}
sourcePath := paths[0]
raw_template, error := ioutil.ReadFile(sourcePath)
if error != nil {
fmt.Println("Failed to read from template file:", sourcePath)
os.Exit(2)
}
contents, error := template.New("template").Parse(string(raw_template))
if error != nil {
fmt.Println("Error parsing template:", error)
}
var data []interface{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
error := json.Unmarshal(scanner.Bytes(), &data)
if error != nil {
fmt.Println("Failed to parse stdin as json:", error)
os.Exit(3)
}
}
tempFile, error := ioutil.TempFile("/tmp", "cwr")
if error != nil {
fmt.Println("Failed to create temp file:", tempFile.Name(), error)
os.Exit(4)
}
if error = contents.Execute(tempFile, data); error != nil {
fmt.Println("Failed to render template:", error)
os.Exit(5)
}
targetPath := paths[1]
if error = os.Rename(tempFile.Name(), targetPath); error != nil {
fmt.Println("Failed to replace target with rendered template:", tempFile.Name(), targetPath)
os.Exit(6)
} else {
fmt.Printf("Successfully rendered template %s to %s\n", sourcePath, targetPath)
}
}