summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Kelly <gdog.kelly@googlemail.com>2017-10-30 22:14:45 +0000
committerGeorge Kelly <gdog.kelly@googlemail.com>2017-11-27 20:04:32 +0000
commit0e33e6fc712d31b742f470cc69d4166c2804ee5e (patch)
treeedcdaad482b05d887ed51af61aca46985f3eee9a
parent99b5cec8b37e25418bbfe2fc251f2adfd57603a2 (diff)
Can now pass --exclude as a flag
exclude takes a glob and ensures that any files found in that glob aren't parsed
-rw-r--r--gomplate.go10
-rw-r--r--main.go2
-rw-r--r--process.go20
-rw-r--r--process_test.go6
-rw-r--r--test/files/input-dir/in/exclude.txt1
5 files changed, 33 insertions, 6 deletions
diff --git a/gomplate.go b/gomplate.go
index fcc5792f..45cb2548 100644
--- a/gomplate.go
+++ b/gomplate.go
@@ -2,6 +2,7 @@ package main
import (
"io"
+ "path/filepath"
"text/template"
"github.com/hairyhenderson/gomplate/data"
@@ -45,11 +46,16 @@ func runTemplate(o *GomplateOpts) error {
g := NewGomplate(d, o.lDelim, o.rDelim)
+ excludeList, err := filepath.Glob(o.excludeGlob)
+ if err != nil {
+ return err
+ }
+
if o.inputDir != "" {
- return processInputDir(o.inputDir, o.outputDir, g)
+ return processInputDir(o.inputDir, o.outputDir, excludeList, g)
}
- return processInputFiles(o.input, o.inputFiles, o.outputFiles, g)
+ return processInputFiles(o.input, o.inputFiles, o.outputFiles, excludeList, g)
}
// Called from process.go ...
diff --git a/main.go b/main.go
index 12690f30..f761a264 100644
--- a/main.go
+++ b/main.go
@@ -23,6 +23,7 @@ type GomplateOpts struct {
inputDir string
outputFiles []string
outputDir string
+ excludeGlob string
}
var opts GomplateOpts
@@ -79,6 +80,7 @@ func initFlags(command *cobra.Command) {
command.Flags().StringArrayVarP(&opts.inputFiles, "file", "f", []string{"-"}, "Template `file` to process. Omit to use standard input, or use --in or --input-dir")
command.Flags().StringVarP(&opts.input, "in", "i", "", "Template `string` to process (alternative to --file and --input-dir)")
command.Flags().StringVar(&opts.inputDir, "input-dir", "", "`directory` which is examined recursively for templates (alternative to --file and --in)")
+ command.Flags().StringVar(&opts.excludeGlob, "exclude", "", "glob of files to not parse")
command.Flags().StringArrayVarP(&opts.outputFiles, "out", "o", []string{"-"}, "output `file` name. Omit to use standard output.")
command.Flags().StringVar(&opts.outputDir, "output-dir", ".", "`directory` to store the processed templates. Only used for --input-dir")
diff --git a/process.go b/process.go
index 85235a52..7df93819 100644
--- a/process.go
+++ b/process.go
@@ -9,7 +9,7 @@ import (
// == Direct input processing ========================================
-func processInputFiles(stringTemplate string, input []string, output []string, g *Gomplate) error {
+func processInputFiles(stringTemplate string, input []string, output []string, excludeList []string, g *Gomplate) error {
input, err := readInputs(stringTemplate, input)
if err != nil {
return err
@@ -29,7 +29,7 @@ func processInputFiles(stringTemplate string, input []string, output []string, g
// == Recursive input dir processing ======================================
-func processInputDir(input string, output string, g *Gomplate) error {
+func processInputDir(input string, output string, excludeList []string, g *Gomplate) error {
input = filepath.Clean(input)
output = filepath.Clean(output)
@@ -55,8 +55,12 @@ func processInputDir(input string, output string, g *Gomplate) error {
nextInPath := filepath.Join(input, entry.Name())
nextOutPath := filepath.Join(output, entry.Name())
+ if inList(excludeList, nextInPath) {
+ continue
+ }
+
if entry.IsDir() {
- err := processInputDir(nextInPath, nextOutPath, g)
+ err := processInputDir(nextInPath, nextOutPath, excludeList, g)
if err != nil {
return err
}
@@ -73,6 +77,16 @@ func processInputDir(input string, output string, g *Gomplate) error {
return nil
}
+func inList(list []string, entry string) bool {
+ for _, file := range list {
+ if file == entry {
+ return true
+ }
+ }
+
+ return false
+}
+
// == File handling ================================================
func readInputs(input string, files []string) ([]string, error) {
diff --git a/process_test.go b/process_test.go
index b63ad6eb..161518a4 100644
--- a/process_test.go
+++ b/process_test.go
@@ -48,7 +48,7 @@ func TestInputDir(t *testing.T) {
Sources: map[string]*data.Source{"config": src},
}
gomplate := NewGomplate(d, "{{", "}}")
- err = processInputDir(filepath.Join("test", "files", "input-dir", "in"), outDir, gomplate)
+ err = processInputDir(filepath.Join("test", "files", "input-dir", "in"), outDir, []string{"**/*.exclude.txt"}, gomplate)
assert.Nil(t, err)
top, err := ioutil.ReadFile(filepath.Join(outDir, "top.txt"))
@@ -58,4 +58,8 @@ func TestInputDir(t *testing.T) {
inner, err := ioutil.ReadFile(filepath.Join(outDir, "inner/nested.txt"))
assert.Nil(t, err)
assert.Equal(t, "zwei", string(inner))
+
+ // excluded file should not exist in out dir
+ _, err = ioutil.ReadFile(filepath.Join(outDir, "inner/exclude.txt"))
+ assert.NotEmpty(t, err)
}
diff --git a/test/files/input-dir/in/exclude.txt b/test/files/input-dir/in/exclude.txt
new file mode 100644
index 00000000..d9cfae16
--- /dev/null
+++ b/test/files/input-dir/in/exclude.txt
@@ -0,0 +1 @@
+Should not be included