Skip to content

Commit 5ca11b9

Browse files
Merge pull request #1 from marcelocarlos/strict-mode
Adding strict mode flag
2 parents b6d0c5c + 8285cfb commit 5ca11b9

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Options:
2424
-e, --env-data Input data source comes from environment variables.
2525
-i, --input INPUT Input template file in go template format.
2626
-o, --output OUTPUT Write the output to the file at OUTPUT.
27-
--help PATH Display this help and exit.
27+
-s, --strict Strict mode (causes an error if a key is missing)
28+
--help Display this help and exit.
2829
--version Output version information and exit.
2930
3031
INPUT defaults to standard input and OUTPUT defaults to standard output.
@@ -92,7 +93,7 @@ func main() {
9293

9394
var (
9495
inputFile, outputFile, jsonDataFile, yamlDataFile string
95-
envFlag, helpFlag, versionFlag bool
96+
envFlag, strictFlag, helpFlag, versionFlag bool
9697
)
9798

9899
flag.StringVar(&inputFile, "input", "", "input template file in go template format")
@@ -105,6 +106,8 @@ func main() {
105106
flag.StringVar(&outputFile, "o", "", "write the output to the file at OUTPUT")
106107
flag.StringVar(&yamlDataFile, "yaml-data", "", "input data source in YAML format")
107108
flag.StringVar(&yamlDataFile, "y", "", "input data source in YAML format")
109+
flag.BoolVar(&strictFlag, "strict", false, "strict mode (causes an error if a key is missing)")
110+
flag.BoolVar(&strictFlag, "s", false, "strict mode (causes an error if a key is missing)")
108111
flag.BoolVar(&versionFlag, "version", false, "output version information and exit")
109112
flag.BoolVar(&helpFlag, "help", false, "display this help and exit")
110113
flag.Parse()
@@ -157,7 +160,11 @@ func main() {
157160
log.Fatalf("Error opening data file: %v\n", err)
158161
}
159162
// Parse Input
160-
tpl, err := template.New("template").Parse(string(tplStr))
163+
missingkey := "default"
164+
if strictFlag {
165+
missingkey = "error"
166+
}
167+
tpl, err := template.New("template").Option(fmt.Sprintf("missingkey=%s", missingkey)).Parse(string(tplStr))
161168
if err != nil {
162169
log.Fatalf("Error parsing template: %v\n", err)
163170
}

0 commit comments

Comments
 (0)