-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
103 lines (80 loc) · 2.08 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
package main
import (
"fmt"
"io/ioutil"
"os"
)
// AppName is the name of the built binary.
const AppName = "templatefile"
// LicenseInfo is the license information for the app.
const LicenseInfo = "License MPL-2.0 <https://www.mozilla.org/en-US/MPL/2.0/>"
// Version is the application version. This is filled in by the compiler.
var Version = "0.0.0"
// GitCommit is the commit that was build. This is filled in by the compiler.
var GitCommit = "HEAD"
// UsageText is the help text for the root command.
var UsageText = fmt.Sprintf("Usage: %s [-v] path vars", AppName)
func main() {
argv := os.Args[1:]
showHelp(argv)
showVersion(argv)
showUsage(argv)
if argv[1] == "-" {
argv[1] = os.Stdin.Name()
}
vars, err := ioutil.ReadFile(argv[1])
pp(err)
out, err := templatefile(".", argv[0], string(vars))
pp(err)
_, err = fmt.Fprintf(os.Stdout, "%s", out)
pp(err)
os.Exit(0)
}
func sliceContains(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
// showHelp prints the help text to stdout and exits.
func showHelp(argv []string) {
if !sliceContains(argv, "--help") {
return
}
_, err := fmt.Fprintf(os.Stdout, "%s\n\n%s\n", "Render a Terraform template file", UsageText)
pp(err)
os.Exit(0)
}
// showVersion prints the version information to stdout and exits.
func showVersion(argv []string) {
if !sliceContains(argv, "--version") && !sliceContains(argv, "-v") {
return
}
v := fmt.Sprintf("%s %s-%s", AppName, Version, GitCommit)
_, err := fmt.Fprintf(os.Stdout, "%s\n%s\n", v, LicenseInfo)
pp(err)
os.Exit(0)
}
// showUsage prints the usage text to stderr and exits with an error exit status.
func showUsage(argv []string) {
if len(argv) == 2 {
return
}
_, err := fmt.Fprintf(os.Stderr, "%s\n", UsageText)
pp(err)
os.Exit(1)
}
// pp prints the given error to stderr and exits with an error exit status.
// If printing to stderr fails, panic.
func pp(err error) {
if err == nil {
return
}
_, err = fmt.Fprintf(os.Stderr, "%s\n", err.Error())
if err != nil {
panic(err)
}
os.Exit(1)
}