Skip to content

Commit dcb8f66

Browse files
committed
Add missingok flag
1 parent 5bbdb2d commit dcb8f66

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ SUMMARY ************************************************************************
212212

213213
The value defined in command-line argument takes precedence over the one from environment variable.
214214

215+
All go-template functions can be used. Example:
216+
217+
```
218+
name: my-product.{{ if .Plant }}{{ .Plant }}{{ else }}default{{ end }}.{{ .Env }}.my-topic
219+
```
220+
221+
But Kafka-Ops fails if some unresolved template key is encountered. In order to override this behaviour use flag *--missingok*.
222+
215223

216224
## Full Usage
217225

@@ -239,6 +247,7 @@ Usage: ./kafka-ops <action> [<options>] [<broker connection options>]
239247
Env variables and from --var arguments (--var arguments are
240248
taking precedence)
241249
--var Variable in format "key=value". Can be presented multiple times
250+
--missingok Do not fail if template key is not defined
242251
--verbose Verbose output
243252
--stop-on-error Exit on first occurred error
244253
----------------

kafka-ops.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var (
3232
actionHelp bool
3333
errorStop bool
3434
isTemplate bool
35+
missingOk bool
3536
varFlags arrFlags
3637
)
3738

@@ -100,6 +101,7 @@ func init() {
100101
flag.BoolVar(&isJSON, "json", false, "Spec-file is in JSON format (will try to detect format if none of --yaml or --json is set)")
101102
flag.BoolVar(&errorStop, "stop-on-error", false, "Exit on first occurred error")
102103
flag.BoolVar(&isTemplate, "template", false, "Spec-file is a template")
104+
flag.BoolVar(&missingOk, "missingok", false, "Ignore missing template keys")
103105
flag.BoolVar(&verbose, "verbose", false, "Verbose output")
104106
flag.Var(&varFlags, "var", "Variable for templating")
105107
flag.Usage = func() {
@@ -604,7 +606,12 @@ func parseSpecFile() (Spec, error) {
604606
}
605607

606608
if isTemplate {
607-
t, err := template.New("").Option("missingkey=error").Parse(string(specFile))
609+
t := template.New("")
610+
if missingOk {
611+
t, err = t.Parse(string(specFile))
612+
} else {
613+
t, err = t.Option("missingkey=error").Parse(string(specFile))
614+
}
608615
if err != nil {
609616
return spec, err
610617
}
@@ -914,6 +921,7 @@ Usage: %s <action> [<options>] [<broker connection options>]
914921
Env variables and from --var arguments (--var arguments are
915922
taking precedence)
916923
--var Variable in format "key=value". Can be presented multiple times
924+
--missingok Do not fail if template key is not defined
917925
--verbose Verbose output
918926
--stop-on-error Exit on first occurred error
919927
----------------

0 commit comments

Comments
 (0)