-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --input-dir and --output-dir as options
All filese from --input-dir will be processed as templates and stored with the same directory hierachy in --ouput-dir - Use both options when a whole directory hierarchy needs to be processed. - Extracted file processing logic in an extra process.go - --output-dir is optional and default to "." - --output-dir is created automatically if not existing Fixes #117 Signed-off-by: Roland Huss <[email protected]>
- Loading branch information
Showing
8 changed files
with
256 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"io" | ||
) | ||
|
||
// == Direct input processing ======================================== | ||
|
||
func processInputFiles(stringTemplate string, input []string, output []string, g *Gomplate) error { | ||
input, err := readInputs(stringTemplate, input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(output) == 0 { | ||
output = []string{"-"} | ||
} | ||
|
||
for n, input := range input { | ||
if err := renderTemplate(g, input, output[n]); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// == Recursive input dir processing ====================================== | ||
|
||
func processInputDir(input string, output string, g *Gomplate) error { | ||
input = filepath.Clean(input) | ||
output = filepath.Clean(output) | ||
|
||
// assert tha input path exists | ||
si, err := os.Stat(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// read directory | ||
entries, err := ioutil.ReadDir(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// ensure output directory | ||
if err = os.MkdirAll(output, si.Mode()); err != nil { | ||
return err | ||
} | ||
|
||
// process or dive in again | ||
for _, entry := range entries { | ||
nextInPath := filepath.Join(input, entry.Name()) | ||
nextOutPath := filepath.Join(output, entry.Name()) | ||
|
||
if entry.IsDir() { | ||
err := processInputDir(nextInPath, nextOutPath, g) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
inString, err := readInput(nextInPath) | ||
if err != nil { | ||
return err | ||
} | ||
if err := renderTemplate(g, inString, nextOutPath); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// == File handling ================================================ | ||
|
||
func readInputs(input string, files []string) ([]string, error) { | ||
if input != "" { | ||
return []string{input}, nil | ||
} | ||
if len(files) == 0 { | ||
files = []string{"-"} | ||
} | ||
ins := make([]string, len(files)) | ||
|
||
for n, filename := range files { | ||
inString, err := readInput(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ins[n] = inString | ||
} | ||
return ins, nil | ||
} | ||
|
||
func readInput(filename string) (string, error) { | ||
var err error | ||
var inFile *os.File | ||
if filename == "-" { | ||
inFile = os.Stdin | ||
} else { | ||
inFile, err = os.Open(filename) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to open %s\n%v", filename, err) | ||
} | ||
defer checkClose(inFile, &err) | ||
} | ||
bytes, err := ioutil.ReadAll(inFile) | ||
if err != nil { | ||
err = fmt.Errorf("read failed for %s\n%v", filename, err) | ||
return "", err | ||
} | ||
return string(bytes), nil | ||
} | ||
|
||
func openOutFile(filename string) (out *os.File, err error) { | ||
if filename == "-" { | ||
return os.Stdout, nil | ||
} | ||
return os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) | ||
} | ||
|
||
func checkClose(c io.Closer, err *error) { | ||
cerr := c.Close() | ||
if *err == nil { | ||
*err = cerr | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"path/filepath" | ||
|
||
"log" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadInput(t *testing.T) { | ||
actual, err := readInputs("foo", nil) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "foo", actual[0]) | ||
|
||
// stdin is "" because during tests it's given /dev/null | ||
actual, err = readInputs("", []string{"-"}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "", actual[0]) | ||
|
||
actual, err = readInputs("", []string{"main_test.go"}) | ||
assert.Nil(t, err) | ||
thisFile, _ := os.Open("main_test.go") | ||
expected, _ := ioutil.ReadAll(thisFile) | ||
assert.Equal(t, string(expected), actual[0]) | ||
} | ||
|
||
func TestInputDir(t *testing.T) { | ||
outDir, err := ioutil.TempDir("test/files/input-dir", "out-temp-") | ||
assert.Nil(t, err) | ||
defer (func() { | ||
if cerr := os.RemoveAll(outDir); cerr != nil { | ||
log.Fatalf("Error while removing temporary directory %s : %v", outDir, cerr) | ||
} | ||
})() | ||
|
||
src, err := ParseSource("config=test/files/input-dir/config.yml") | ||
assert.Nil(t, err) | ||
|
||
data := &Data{ | ||
Sources: map[string]*Source{"config": src}, | ||
} | ||
gomplate := NewGomplate(data, "{{", "}}") | ||
err = processInputDir("test/files/input-dir/in", outDir, gomplate) | ||
assert.Nil(t, err) | ||
|
||
top, err := ioutil.ReadFile(filepath.Join(outDir, "top.txt")) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "eins", string(top)) | ||
|
||
inner, err := ioutil.ReadFile(filepath.Join(outDir, "inner/nested.txt")) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "zwei", string(inner)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
one: eins | ||
two: zwei |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ (datasource "config").two }} |
Oops, something went wrong.