forked from dreadatour/go-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-cli-template.go
90 lines (81 loc) · 2.04 KB
/
go-cli-template.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
package main
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"os"
"strings"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s <--key1=value1> <--key2=value2> <--flag1> ... template.file\n", os.Args[0])
os.Exit(1)
}
func main() {
// get all environment variables
data := make(map[string]string)
for _, i := range os.Environ() {
s := strings.Split(i, "=")
data[s[0]] = s[1]
}
// parse command line arguments
var templateFile string
for _, i := range os.Args[1:] {
if len(i) >= 2 && i[0:2] == "--" {
s := strings.Split(i[2:], "=")
if len(s) > 1 {
data[s[0]] = s[1]
} else {
data[i[2:]] = "true"
}
} else if templateFile == "" {
templateFile = i
} else {
fmt.Fprintln(os.Stderr, "Error: more than one template file defined")
usage()
}
}
var templateContent string
if templateFile == "" {
fmt.Fprintln(os.Stderr, "Error: no template file defined")
usage()
} else if templateFile == "-" {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: can't read from STDIN")
os.Exit(1)
}
templateContent = string(data)
} else {
// check template file
if _, err := os.Stat(templateFile); err != nil {
fmt.Fprintf(os.Stderr, "Error: can't find template file '%s'\n", templateFile)
os.Exit(1)
}
// read from template
data, err := ioutil.ReadFile(templateFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: can't read from template file '%s'\n", templateFile)
os.Exit(1)
}
templateContent = string(data)
}
// parse template file
t, err := template.New("template").Parse(templateContent)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: can't parse template file")
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
t.Option("missingkey=error")
// render template file with environment variables
var output bytes.Buffer
err = t.Execute(&output, data)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: can't render template file")
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// write result to STDOUT
fmt.Print(output.String())
}