-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (75 loc) · 1.77 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
package main
import (
"bytes"
"io"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
intemplate = kingpin.Flag("template", "the source template").Default("file.tpl").String()
outfile = kingpin.Flag("outfile", "output file").Default("outfile.txt").String()
verbose = kingpin.Flag("verbose", "Produce verbose output").Short('v').Default("false").Bool()
)
func writeFile(data *bytes.Buffer, outfile string) {
pr, pw := io.Pipe()
go func() {
defer pw.Close()
_, err := io.Copy(pw, data)
if err != nil {
log.Fatalf("%s\n", err)
}
}()
out, err := os.Create(outfile)
if err != nil {
log.Fatalf("%s\n", err)
}
defer func() {
cerr := out.Close()
if cerr != nil {
log.Fatalf("%s\n", cerr)
}
}()
if _, err = io.Copy(out, pr); err != nil {
return
}
err = out.Sync()
if err != nil {
log.Fatalf("%s\n", err)
}
return
}
func parsetemplate(infile string) (b *bytes.Buffer, err error) {
if _, err := os.Stat(infile); !os.IsNotExist(err) {
if err != nil {
log.Fatalf("%s\n", err)
}
}
tpl, err := template.New(infile).Funcs(sprig.TxtFuncMap()).ParseFiles(infile)
if err != nil {
log.Fatalf("%s\n", err)
}
b = new(bytes.Buffer)
err = tpl.ExecuteTemplate(b, filepath.Base(infile), nil)
log.Debugln(b.String())
if err != nil {
log.Fatalf("%s\n", err)
}
return b, err
}
func main() {
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version("0.0.1").Author("Benjamin Rizkowsky")
kingpin.CommandLine.Help = "A simple environment variable config template writer."
kingpin.Parse()
if *verbose == true {
log.SetLevel(log.DebugLevel)
}
data, err := parsetemplate(*intemplate)
if err != nil {
log.Fatalf("%s\n", err)
}
writeFile(data, *outfile)
}