-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (116 loc) · 2.75 KB
/
main.go
File metadata and controls
129 lines (116 loc) · 2.75 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"time"
"github.com/hairyhenderson/gomplate"
)
// main - check is a no-op for the gomplate resource - the input is always
// considered out-of-date
func main() {
switch basename := path.Base(os.Args[0]); basename {
case "check":
fmt.Printf("[{\"date\": \"%s\"}]\n", time.Now().Format(time.RFC3339))
case "in":
in, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("couldn't read stdin: %#v", err)
}
var p payload
err = json.Unmarshal(in, &p)
if err != nil {
log.Fatalf("couldn't unmarshal payload: %#v (payload was '%s')", err, string(in))
}
var destination string
if len(os.Args) > 1 {
destination = os.Args[1]
}
p.Params.OutputDir = path.Join(destination, p.Params.OutputDir)
if len(p.Params.OutputFiles) > 0 {
for i, f := range p.Params.OutputFiles {
p.Params.OutputFiles[i] = path.Join(destination, f)
}
}
buf := &bytes.Buffer{}
gomplate.Stdout = &nopWCloser{buf}
oStdout := os.Stdout
oStderr := os.Stderr
defer func() {
os.Stdout = oStdout
os.Stderr = oStderr
}()
ro, wo, _ := os.Pipe()
re, we, _ := os.Pipe()
os.Stdout = wo
os.Stdout = we
stdout := make(chan string)
stderr := make(chan string)
go func() {
b := bytes.Buffer{}
e := bytes.Buffer{}
io.Copy(&b, ro)
io.Copy(&e, re)
stdout <- b.String()
stderr <- e.String()
}()
wd, _ := os.Getwd()
err = gomplate.RunTemplates(p.Params)
wo.Close()
we.Close()
os.Stdout = oStdout
os.Stderr = oStderr
r := result{
Version: p.Version,
Metadata: []metadata{
{"workDir", wd},
{"destination", destination},
{"templateOut", buf.String()},
{"stdout", <-stdout},
{"stderr", <-stderr},
{"success", strconv.FormatBool(err == nil)},
{"errors", strconv.Itoa(gomplate.Metrics.Errors)},
{"gatherDuration", gomplate.Metrics.GatherDuration.String()},
{"totalRenderDuration", gomplate.Metrics.TotalRenderDuration.String()},
},
}
if err != nil {
r.Metadata = append(r.Metadata, metadata{"error", err.Error()})
}
json.NewEncoder(os.Stdout).Encode(r)
if err != nil {
log.Fatalf("couldn't run gomplate: %s", err)
}
default:
log.Fatalf("%s is an invalid binary name", basename)
}
}
// input
type payload struct {
Version version `json:"version"`
Source struct{}
Params *gomplate.Config
}
// output
type result struct {
Version version `json:"version"`
Metadata []metadata `json:"metadata,omitempty"`
}
type version map[string]string
type metadata struct {
Name string `json:"name"`
Value string `json:"value"`
}
// like ioutil.NopCloser(), except for io.WriteClosers...
type nopWCloser struct {
io.Writer
}
func (n *nopWCloser) Close() error {
return nil
}