-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
218 lines (192 loc) · 6.34 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// PACKAGE
package main
/*
* IMPORTS
*/
import (
"fmt"
"os"
"path"
"strings"
"github.com/alecthomas/kong"
"github.com/runeimp/templar"
)
/*
* APP CONSTANTS
*/
const (
AppDesc = "Command line templating system based on Mustache template engine and data supplied by environment variables, ENV, INI, JSON, YAML, and TOML files. And soon CSV, XML, and SQLite files as well."
AppName = "Templar"
AppVersion = "2.1.0"
CLIName = "templar"
)
/*
* CONSTANTS
*/
const (
ErrorENVParsing = iota + 50
ErrorINIParsing
ErrorJSONParsing
ErrorTemplateMissing
ErrorTemplateRendering
ErrorTOMLParsing
ErrorYAMLParsing
)
/*
* GENERATED VARIABLES
*/
var (
commit = "none"
date = "unknown"
)
/*
* DERIVED CONSTANTS
*/
var (
AppLabel = fmt.Sprintf("%s v%s\n%s Library v%s", AppName, AppVersion, templar.Name, templar.Version)
)
/*
* TYPES
*/
// cli defines the command line interface for this tool
var cli struct {
DataENV []string `short:"e" help:"Use the specified ENV_FILE (regardless of it's file extension) to populate the template environment." placeholder:"ENV_FILE" type:"existingfile"`
DataFile []string `short:"f" help:"Use the specified DATA_FILE to populate the template environment. File type determined by the extension on the file name." placeholder:"DATA_FILE" type:"existingfile"`
DataINI []string `short:"i" help:"Use the specified INI_FILE (regardless of it's file extension) to populate the template environment." placeholder:"INI_FILE" type:"existingfile"`
DataJSON []string `short:"j" help:"Use the specified JSON_FILE (regardless of it's file extension) to populate the template environment." placeholder:"JSON_FILE" type:"existingfile"`
DataTOML []string `short:"t" help:"Use the specified TOML_FILE (regardless of it's file extension) to populate the template environment." placeholder:"TOML_FILE" type:"existingfile"`
DataYAML []string `short:"y" help:"Use the specified YAML_FILE (regardless of it's file extension) to populate the template environment." placeholder:"YAML_FILE" type:"existingfile"`
Debug int `help:"Show debug info on stderr." default:"0" hidden`
NoDotenv bool `short:"n" help:"Do not load a local .env file if present."`
OutputFile string `short:"o" help:"Output to the specified file." placeholder:"FILE" sep:' ' type:"file"`
Template string `arg optional help:"Specify the template file to render." type:"existingfile"`
Version bool `short:"v" help:"Show version info."`
// ____ string `arg:"-_" help:"____"`
// ____ string `arg:"-_" help:"____"`
}
/*
* VARIABLES
*/
var (
ctx *kong.Context
envFiles []string
iniFiles []string
jsonFiles []string
tomlFiles []string
yamlFiles []string
)
/*
* FUNCTIONS
*/
func init() {
ctx = kong.Parse(&cli, kong.Name(CLIName), kong.Description(AppDesc))
if cli.Version {
fmt.Println(AppLabel)
os.Exit(0)
}
}
/*
* MAIN ENTRYPOINT
*/
func main() {
if cli.Debug > templar.DebugOff {
fmt.Println(AppLabel)
}
if len(cli.Template) == 0 {
if len(cli.DataENV) == 0 && len(cli.DataFile) == 0 && len(cli.DataINI) == 0 && len(cli.DataJSON) == 0 && len(cli.OutputFile) == 0 && cli.NoDotenv == false {
ctx.PrintUsage(false)
os.Exit(0)
}
fmt.Fprintf(os.Stderr, "Usage Error: %s\n", "Template Missing")
os.Exit(ErrorTemplateMissing)
}
templar.Debug = cli.Debug
// fmt.Printf("templar.main() | cli.DataFile = %q\n", cli.DataFile)
for _, file := range cli.DataFile {
ext := path.Ext(file)
switch strings.ToUpper(ext) {
case ".ENV":
envFiles = append(envFiles, file)
case ".INI":
iniFiles = append(iniFiles, file)
case ".JSON":
jsonFiles = append(jsonFiles, file)
case ".TOML":
tomlFiles = append(tomlFiles, file)
case ".YAML":
yamlFiles = append(yamlFiles, file)
default:
fmt.Errorf("Unknown data file type: %q", ext)
}
}
// fmt.Printf("templar.main() | templar.Data = %#v\n", templar.Data)
for _, file := range cli.DataENV {
envFiles = append(envFiles, file)
}
for _, file := range cli.DataINI {
iniFiles = append(iniFiles, file)
}
for _, file := range cli.DataJSON {
jsonFiles = append(jsonFiles, file)
}
for _, file := range cli.DataTOML {
tomlFiles = append(tomlFiles, file)
}
for _, file := range cli.DataYAML {
yamlFiles = append(yamlFiles, file)
}
// templar.Data.Template = cli.Template
checkDotEnv := !cli.NoDotenv
// fmt.Printf("templar.main() | cli.Template = %q\n", cli.Template)
// fmt.Printf("templar.main() | templar.Data = %#v\n", templar.Data)
if cli.Debug > templar.DebugOff {
fmt.Fprintf(os.Stderr, "templar.main() | envFiles = %#v\n", envFiles)
fmt.Fprintf(os.Stderr, "templar.main() | iniFiles = %#v\n", iniFiles)
fmt.Fprintf(os.Stderr, "templar.main() | jsonFiles = %#v\n", jsonFiles)
fmt.Fprintf(os.Stderr, "templar.main() | tomlFiles = %#v\n", tomlFiles)
fmt.Fprintf(os.Stderr, "templar.main() | yamlFiles = %#v\n", yamlFiles)
fmt.Fprintf(os.Stderr, "templar.main() | template = %q\n", cli.Template)
fmt.Fprintf(os.Stderr, "templar.main() | checkDotEnv = %t\n", checkDotEnv)
}
err := templar.InitData(checkDotEnv, envFiles...)
if err != nil {
fmt.Fprintf(os.Stderr, "ENV File Parsing Error: %s\n", err.Error())
os.Exit(ErrorENVParsing)
}
err = templar.InitData(checkDotEnv, iniFiles...)
if err != nil {
fmt.Fprintf(os.Stderr, "INI File Parsing Error: %s\n", err.Error())
os.Exit(ErrorINIParsing)
}
err = templar.InitData(checkDotEnv, jsonFiles...)
if err != nil {
fmt.Fprintf(os.Stderr, "JSON File Parsing Error: %s\n", err.Error())
os.Exit(ErrorJSONParsing)
}
err = templar.InitData(checkDotEnv, tomlFiles...)
if err != nil {
fmt.Fprintf(os.Stderr, "TOML File Parsing Error: %s\n", err.Error())
os.Exit(ErrorTOMLParsing)
}
err = templar.InitData(checkDotEnv, yamlFiles...)
if err != nil {
fmt.Fprintf(os.Stderr, "YAML File Parsing Error: %s\n", err.Error())
os.Exit(ErrorYAMLParsing)
}
if len(cli.OutputFile) > 0 {
_, err := templar.RenderToFile(cli.OutputFile, cli.Template)
if err != nil {
fmt.Fprintf(os.Stderr, "Template Rendering Error: %s\n", err.Error())
os.Exit(ErrorTemplateRendering)
}
} else {
output, err := templar.Render(cli.Template)
if err != nil {
fmt.Fprintf(os.Stderr, "Template Rendering Error: %s\n", err.Error())
os.Exit(ErrorTemplateRendering)
}
fmt.Printf("%s", output)
}
kong.UsageOnError()
// kong.Help(kong.DefaultHelpPrinter(kong.HelpOptions, ctx))
}