Skip to content

Commit

Permalink
Merge pull request #220 from Gman98ish/master
Browse files Browse the repository at this point in the history
Can now pass --exclude as a flag
  • Loading branch information
hairyhenderson authored Nov 28, 2017
2 parents 99b5cec + 13d46e7 commit e5d193a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
15 changes: 15 additions & 0 deletions docs/content/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ Example:
gomplate --input-dir=templates --output-dir=config --datasource config=config.yaml
```

### `--exclude`

To prevent certain files from being processed, you can use `--exclude`. It takes a glob, and any files matching that glob will not be included.

Example:

```
gomplate --exclude example/** \
--exclude *.png
```

This will stop all files in the example folder from being processed, as well as all .png files in the root folder.

You can also chain the exclude flag to build up a series of globs to be excluded

## `--datasource`/`-d`

Add a data source in `name=URL` form. Specify multiple times to add multiple sources. The data can then be used by the [`datasource`](../functions/#datasource) and [`include`](../functions/#include) functions.
Expand Down
26 changes: 24 additions & 2 deletions gomplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"io"
"path/filepath"
"text/template"

"github.com/hairyhenderson/gomplate/data"
Expand Down Expand Up @@ -45,11 +46,16 @@ func runTemplate(o *GomplateOpts) error {

g := NewGomplate(d, o.lDelim, o.rDelim)

excludeList, err := executeCombinedGlob(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 ...
Expand All @@ -63,3 +69,19 @@ func renderTemplate(g *Gomplate, inString string, outPath string) error {
err = g.RunTemplate(inString, outFile)
return err
}

// takes an array of glob strings and executes it as a whole,
// returning a merged list of globbed files
func executeCombinedGlob(globArray []string) ([]string, error) {
var combinedExcludes []string
for _, glob := range globArray {
excludeList, err := filepath.Glob(glob)
if err != nil {
return nil, err
}

combinedExcludes = append(combinedExcludes, excludeList...)
}

return combinedExcludes, nil
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type GomplateOpts struct {
inputDir string
outputFiles []string
outputDir string
excludeGlob []string
}

var opts GomplateOpts
Expand Down Expand Up @@ -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().StringArrayVar(&opts.excludeGlob, "exclude", []string{}, "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")

Expand Down
20 changes: 17 additions & 3 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions test/files/input-dir/in/exclude.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Should not be included

0 comments on commit e5d193a

Please sign in to comment.