diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2019-08-18 14:25:27 -0400 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2019-08-18 14:25:27 -0400 |
| commit | eb8c563fcf62e2cc6b072550d3fe7cfb390f69a6 (patch) | |
| tree | 17b77547355d962af9e7761ee500e78300bdb39f /cmd | |
| parent | b4b82904d31d3166a8e40b4b88ebb02eea8641aa (diff) | |
Adding --include flag
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/gomplate/main.go | 22 | ||||
| -rw-r--r-- | cmd/gomplate/main_test.go | 16 |
2 files changed, 38 insertions, 0 deletions
diff --git a/cmd/gomplate/main.go b/cmd/gomplate/main.go index 017b9d89..15ee02c5 100644 --- a/cmd/gomplate/main.go +++ b/cmd/gomplate/main.go @@ -21,6 +21,7 @@ var ( printVer bool verbose bool opts gomplate.Config + includes []string ) // nolint: gocyclo @@ -98,6 +99,23 @@ func optionalExecArgs(cmd *cobra.Command, args []string) error { return cobra.NoArgs(cmd, args) } +// process --include flags - these are analogous to specifying --exclude '*', +// then the inverse of the --include options. +func processIncludes(includes, excludes []string) []string { + out := []string{} + // if any --includes are set, we start by excluding everything + if len(includes) > 0 { + out = make([]string, 1+len(includes)) + out[0] = "*" + } + for i, include := range includes { + // includes are just the opposite of an exclude + out[i+1] = "!" + include + } + out = append(out, excludes...) + return out +} + func newGomplateCmd() *cobra.Command { rootCmd := &cobra.Command{ Use: "gomplate", @@ -115,6 +133,9 @@ func newGomplateCmd() *cobra.Command { &opts) } + // support --include + opts.ExcludeGlob = processIncludes(includes, opts.ExcludeGlob) + err := gomplate.RunTemplates(&opts) cmd.SilenceErrors = true cmd.SilenceUsage = true @@ -144,6 +165,7 @@ func initFlags(command *cobra.Command) { command.Flags().StringVar(&opts.InputDir, "input-dir", "", "`directory` which is examined recursively for templates (alternative to --file and --in)") command.Flags().StringArrayVar(&opts.ExcludeGlob, "exclude", []string{}, "glob of files to not parse") + command.Flags().StringArrayVar(&includes, "include", []string{}, "glob of files to parse") command.Flags().StringArrayVarP(&opts.OutputFiles, "out", "o", []string{"-"}, "output `file` name. Omit to use standard output.") command.Flags().StringArrayVarP(&opts.Templates, "template", "t", []string{}, "Additional template file(s)") diff --git a/cmd/gomplate/main_test.go b/cmd/gomplate/main_test.go index c8670853..f29e4978 100644 --- a/cmd/gomplate/main_test.go +++ b/cmd/gomplate/main_test.go @@ -68,3 +68,19 @@ func parseFlags(flags ...string) *cobra.Command { } return cmd } + +func TestProcessIncludes(t *testing.T) { + data := []struct { + inc, exc, expected []string + }{ + {nil, nil, []string{}}, + {[]string{}, []string{}, []string{}}, + {nil, []string{"*.foo"}, []string{"*.foo"}}, + {[]string{"*.bar"}, []string{"a*.bar"}, []string{"*", "!*.bar", "a*.bar"}}, + {[]string{"*.bar"}, nil, []string{"*", "!*.bar"}}, + } + + for _, d := range data { + assert.EqualValues(t, d.expected, processIncludes(d.inc, d.exc)) + } +} |
