This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
121 lines (109 loc) · 2.82 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
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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/BurntSushi/toml"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)
var valuesFile = flag.String("values", "", "File with values")
var outputFile = flag.String("output", "", "Output file path. Defaults to stdout if unspecified")
var templateFiles stringList
func init() {
flag.Var(&templateFiles, "template", "Template file. Can be specified multiple times")
}
func main() {
flag.Parse()
if *valuesFile == "" {
log.Error("values file is required")
flag.Usage()
}
if err := run(*valuesFile, *outputFile, templateFiles...); err != nil {
log.Fatalln(err)
}
}
func run(valuesFile, outputFile string, templateFiles ...string) error {
valsf, err := os.Open(valuesFile)
if err != nil {
return trace.ConvertSystemError(err)
}
defer valsf.Close()
v, err := decodeValues(valsf)
if err != nil {
return trace.Wrap(err)
}
tpl, err := template.ParseFiles(templateFiles...)
if err != nil {
return trace.Wrap(err)
}
if outputFile == "" {
return tpl.Execute(os.Stdout, v)
}
var buf bytes.Buffer
err = tpl.Execute(&buf, v)
if err != nil {
return trace.Wrap(err)
}
return copyReaderWithPerms(outputFile, &buf, sharedReadWriteMask)
}
func decodeValues(valuesFile io.Reader) (v interface{}, err error) {
_, err = toml.DecodeReader(valuesFile, &v)
if err != nil {
return nil, trace.Wrap(err)
}
return v, nil
}
// String formats this list for output
func (r stringList) String() string {
return fmt.Sprint(([]string)(r))
}
func (r *stringList) Set(v string) error {
if strings.TrimSpace(v) == "" {
return trace.BadParameter("value cannot be empty")
}
*r = append(*r, v)
return nil
}
// copyReaderWithPerms copies the contents from src to dst atomically.
// If dst does not exist, CopyReaderWithPerms creates it with permissions perm.
// If the copy fails, CopyReaderWithPerms aborts and dst is preserved.
// Adopted with modifications from https://go-review.googlesource.com/#/c/1591/9/src/io/ioutil/ioutil.go
func copyReaderWithPerms(dst string, src io.Reader, perm os.FileMode) error {
tmp, err := ioutil.TempFile(filepath.Dir(dst), "")
if err != nil {
return trace.ConvertSystemError(err)
}
defer func() {
if err == nil {
return
}
if err := os.Remove(tmp.Name()); err != nil {
log.Errorf("Failed to remove %v: %v.", tmp.Name(), err)
}
}()
_, err = io.Copy(tmp, src)
if err != nil {
tmp.Close()
return trace.ConvertSystemError(err)
}
if err = tmp.Close(); err != nil {
return trace.ConvertSystemError(err)
}
if err = os.Chmod(tmp.Name(), perm); err != nil {
return trace.ConvertSystemError(err)
}
err = os.Rename(tmp.Name(), dst)
if err != nil {
return trace.ConvertSystemError(err)
}
return nil
}
type stringList []string
const sharedReadWriteMask = 0666