From f64a1585dd0962fd4eef5b14a70686ee68650eb7 Mon Sep 17 00:00:00 2001 From: Josh Mahowald Date: Sat, 29 Oct 2016 19:08:11 -0500 Subject: [PATCH 1/5] Adding in basic unit test --- examples/json/json-example | 5 +--- templatize_test.go | 50 ++++++++++++++++++++++++++++++++++++++ test/rendered-json-example | 15 ++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 templatize_test.go create mode 100644 test/rendered-json-example diff --git a/examples/json/json-example b/examples/json/json-example index 3ad2628..12902bd 100644 --- a/examples/json/json-example +++ b/examples/json/json-example @@ -9,17 +9,14 @@ } ] }` }} - NAME0={{ jsonQuery $jsonDoc "services.[0].name" }} PORT0={{ jsonQuery $jsonDoc "services.[0].port" }} NAME1={{ jsonQuery $jsonDoc "services.[1].name" }} PORT1={{ jsonQuery $jsonDoc "services.[1].port" }} - {{ range $index, $value := jsonQuery $jsonDoc "services" }} Index: {{ printf "%v" $index }} Name: {{ printf "%v" $value.name }} Port: {{ printf "%v" $value.port }} --- {{end}} - -{{end}} \ No newline at end of file +{{end}} diff --git a/templatize_test.go b/templatize_test.go new file mode 100644 index 0000000..73c5de8 --- /dev/null +++ b/templatize_test.go @@ -0,0 +1,50 @@ +package main + +import ( + "io/ioutil" + "path/filepath" + "strings" + "testing" + + // I use this instead of base testing Suite + // to bring back warm fuzzies of junit + . "gopkg.in/check.v1" +) + +// Hook up gocheck into the "go test" runner. +func Test(t *testing.T) { TestingT(t) } + +type MySuite struct { + dir string +} + +var _ = Suite(&MySuite{}) + +func (s *MySuite) SetUpSuite(c *C) { + s.dir = c.MkDir() +} + +func (s *MySuite) TestJsonTemplate(c *C) { + + templatePath := filepath.Join("examples", "json", "json-example") + answerFile := filepath.Join("test", "rendered-json-example") + destPath := filepath.Join(s.dir, "output-json-example") + + generateFile(templatePath, destPath) + + fileCompare(answerFile, destPath, c) +} + +func fileCompare(expectedFile, actualFile string, c *C) { + expectedResult, err := ioutil.ReadFile(expectedFile) + if err != nil { + c.Errorf("No file %s", expectedFile) + } + actualResult, err := ioutil.ReadFile(actualFile) + if err != nil { + c.Errorf("No file %s", actualFile) + } + + c.Assert(strings.TrimSpace(string(actualResult)), + Equals, strings.TrimSpace(string(expectedResult))) +} diff --git a/test/rendered-json-example b/test/rendered-json-example new file mode 100644 index 0000000..6f557c8 --- /dev/null +++ b/test/rendered-json-example @@ -0,0 +1,15 @@ + + NAME0=service1 + PORT0=8000 + NAME1=service2 + PORT1=9000 + + Index: 0 + Name: service1 + Port: 8000 + --- + + Index: 1 + Name: service2 + Port: 9000 + --- From 2f628f04541965b81d445cc986303de0691b79b9 Mon Sep 17 00:00:00 2001 From: Josh Mahowald Date: Sun, 30 Oct 2016 12:09:53 -0500 Subject: [PATCH 2/5] adding in ability to templatize full direcotries --- template.go | 40 +++++++++++++++++++++++ templatize_test.go => template_test.go | 25 +++++++++++--- test/expected/one.conf | 1 + test/{ => expected}/rendered-json-example | 0 test/expected/two.txt | 1 + test/fixtures/etc/conf/two.txt.tpl | 1 + test/fixtures/etc/sample/one.conf.tpl | 1 + 7 files changed, 64 insertions(+), 5 deletions(-) rename templatize_test.go => template_test.go (61%) create mode 100644 test/expected/one.conf rename test/{ => expected}/rendered-json-example (100%) create mode 100644 test/expected/two.txt create mode 100644 test/fixtures/etc/conf/two.txt.tpl create mode 100644 test/fixtures/etc/sample/one.conf.tpl diff --git a/template.go b/template.go index ce7f4ae..76b59f5 100644 --- a/template.go +++ b/template.go @@ -139,3 +139,43 @@ func generateFile(templatePath, destPath string) bool { return true } + +// TODO - make this be able to be set from CLI? +var templateSuffix = ".tpl" + +func processTemplates(sourceDirPath, destDirPath string) { + + walker := func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Fatalf("Could not process %s: %s", sourceDirPath, err) + return nil + } + if strings.HasSuffix(info.Name(), templateSuffix) { + destFileName := strings.TrimSuffix(info.Name(), templateSuffix) + + relPath, err := filepath.Rel(sourceDirPath, path) + if err != nil { + log.Fatalf("Could not split %q into relative path with base %q", + path, sourceDirPath) + } + dir, _ := filepath.Split(relPath) + // if err != nil { + // log.Fatalf("Could not get directory file split from %q", relPath) + // } + + destDir := filepath.Join(destDirPath, dir) + os.MkdirAll(destDir, 0755) + destFileName = filepath.Join(destDir, destFileName) + + //Drop the first parth, which shoudl be the sourceDirPath + //and the last part which is + log.Println("writing to ", destFileName) + + generateFile(path, destFileName) + + } + return nil + } + filepath.Walk(sourceDirPath, walker) + +} diff --git a/templatize_test.go b/template_test.go similarity index 61% rename from templatize_test.go rename to template_test.go index 73c5de8..742dcd4 100644 --- a/templatize_test.go +++ b/template_test.go @@ -2,6 +2,7 @@ package main import ( "io/ioutil" + "os" "path/filepath" "strings" "testing" @@ -21,20 +22,35 @@ type MySuite struct { var _ = Suite(&MySuite{}) func (s *MySuite) SetUpSuite(c *C) { - s.dir = c.MkDir() + // s.dir = c.MkDir() + os.Mkdir("target", 0777) + s.dir = "target" } func (s *MySuite) TestJsonTemplate(c *C) { templatePath := filepath.Join("examples", "json", "json-example") - answerFile := filepath.Join("test", "rendered-json-example") + answerFile := filepath.Join("test", "expected", "rendered-json-example") destPath := filepath.Join(s.dir, "output-json-example") - generateFile(templatePath, destPath) - fileCompare(answerFile, destPath, c) } +// Test walking through a tree of templates +func (s *MySuite) TestDirTemplates(c *C) { + dirpath := filepath.Join("test", "fixtures") + processTemplates(dirpath, s.dir) + os.Setenv("VAL_1", "one") + os.Setenv("VAL_2", "two") + fileCompare(filepath.Join("test", "expected", "one.conf"), + filepath.Join(s.dir, "expected", + "etc", "sample", "one.conf"), c) + fileCompare(filepath.Join("test", "expected", "two.txt"), + filepath.Join(s.dir, "expected", + "etc", "conf", "two.txt"), c) + +} + func fileCompare(expectedFile, actualFile string, c *C) { expectedResult, err := ioutil.ReadFile(expectedFile) if err != nil { @@ -44,7 +60,6 @@ func fileCompare(expectedFile, actualFile string, c *C) { if err != nil { c.Errorf("No file %s", actualFile) } - c.Assert(strings.TrimSpace(string(actualResult)), Equals, strings.TrimSpace(string(expectedResult))) } diff --git a/test/expected/one.conf b/test/expected/one.conf new file mode 100644 index 0000000..525c02c --- /dev/null +++ b/test/expected/one.conf @@ -0,0 +1 @@ +key1=one; diff --git a/test/rendered-json-example b/test/expected/rendered-json-example similarity index 100% rename from test/rendered-json-example rename to test/expected/rendered-json-example diff --git a/test/expected/two.txt b/test/expected/two.txt new file mode 100644 index 0000000..92d0ce9 --- /dev/null +++ b/test/expected/two.txt @@ -0,0 +1 @@ +key2=two diff --git a/test/fixtures/etc/conf/two.txt.tpl b/test/fixtures/etc/conf/two.txt.tpl new file mode 100644 index 0000000..56ae246 --- /dev/null +++ b/test/fixtures/etc/conf/two.txt.tpl @@ -0,0 +1 @@ +key2={{ .Env.VAL_2 }} diff --git a/test/fixtures/etc/sample/one.conf.tpl b/test/fixtures/etc/sample/one.conf.tpl new file mode 100644 index 0000000..266ddff --- /dev/null +++ b/test/fixtures/etc/sample/one.conf.tpl @@ -0,0 +1 @@ +key1={{ .Env.VAL_1 }}; From a36f7d6c35d1cbacdec441a3fc923ab651e252dc Mon Sep 17 00:00:00 2001 From: Josh Mahowald Date: Sun, 30 Oct 2016 12:14:13 -0500 Subject: [PATCH 3/5] Set the env variables before anything else in tests --- template_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/template_test.go b/template_test.go index 742dcd4..3f84f11 100644 --- a/template_test.go +++ b/template_test.go @@ -22,9 +22,9 @@ type MySuite struct { var _ = Suite(&MySuite{}) func (s *MySuite) SetUpSuite(c *C) { - // s.dir = c.MkDir() - os.Mkdir("target", 0777) - s.dir = "target" + os.Setenv("VAL_1", "one") + os.Setenv("VAL_2", "two") + s.dir = c.MkDir() } func (s *MySuite) TestJsonTemplate(c *C) { @@ -40,13 +40,12 @@ func (s *MySuite) TestJsonTemplate(c *C) { func (s *MySuite) TestDirTemplates(c *C) { dirpath := filepath.Join("test", "fixtures") processTemplates(dirpath, s.dir) - os.Setenv("VAL_1", "one") - os.Setenv("VAL_2", "two") + fileCompare(filepath.Join("test", "expected", "one.conf"), - filepath.Join(s.dir, "expected", + filepath.Join(s.dir, "etc", "sample", "one.conf"), c) fileCompare(filepath.Join("test", "expected", "two.txt"), - filepath.Join(s.dir, "expected", + filepath.Join(s.dir, "etc", "conf", "two.txt"), c) } From 8e13eed0b34c8d279d3ffd2140b15c05002a73ae Mon Sep 17 00:00:00 2001 From: Josh Mahowald Date: Sun, 30 Oct 2016 12:25:02 -0500 Subject: [PATCH 4/5] Added in CLI flags for new template directories --- dockerize.go | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/dockerize.go b/dockerize.go index 55aa0b4..c246644 100644 --- a/dockerize.go +++ b/dockerize.go @@ -41,17 +41,18 @@ var ( poll bool wg sync.WaitGroup - templatesFlag sliceVar - stdoutTailFlag sliceVar - stderrTailFlag sliceVar - headersFlag sliceVar - delimsFlag string - delims []string - headers []HttpHeader - urls []url.URL - waitFlag hostFlagsVar - waitTimeoutFlag time.Duration - dependencyChan chan struct{} + templatesFlag sliceVar + templatesDirFlag sliceVar + stdoutTailFlag sliceVar + stderrTailFlag sliceVar + headersFlag sliceVar + delimsFlag string + delims []string + headers []HttpHeader + urls []url.URL + waitFlag hostFlagsVar + waitTimeoutFlag time.Duration + dependencyChan chan struct{} ctx context.Context cancel context.CancelFunc @@ -166,6 +167,7 @@ func main() { flag.BoolVar(&version, "version", false, "show version") flag.BoolVar(&poll, "poll", false, "enable polling") + flag.Var(&templatesDirFlag, "template-dir", "Template Directory (templateDir:destDir).") flag.Var(&templatesFlag, "template", "Template (/template:/dest). Can be passed multiple times") flag.Var(&stdoutTailFlag, "stdout", "Tails a file to stdout. Can be passed multiple times") flag.Var(&stderrTailFlag, "stderr", "Tails a file to stderr. Can be passed multiple times") @@ -233,6 +235,19 @@ func main() { generateFile(template, dest) } + for _, t := range templatesDirFlag { + if strings.Contains(t, ":") { + parts := strings.Split(t, ":") + if len(parts) != 2 { + log.Fatalf("bad template argument: %s. expected \"templateDir:destDir\"", t) + } + templateDir, destDir := parts[0], parts[1] + processTemplates(templateDir, destDir) + } else { + log.Fatalf("bad template directory arugment: %s. expected \"templateDir:destDir\"", t) + } + } + waitForDependencies() // Setup context From 774fe7fda451a878881f5be25cae76506dbbc56b Mon Sep 17 00:00:00 2001 From: Josh Mahowald Date: Sat, 12 Nov 2016 18:20:43 -0600 Subject: [PATCH 5/5] don't need *.tpl to be used --- dockerize.go | 2 +- template.go | 17 +++++++++++++---- template_test.go | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/dockerize.go b/dockerize.go index c246644..528688c 100644 --- a/dockerize.go +++ b/dockerize.go @@ -242,7 +242,7 @@ func main() { log.Fatalf("bad template argument: %s. expected \"templateDir:destDir\"", t) } templateDir, destDir := parts[0], parts[1] - processTemplates(templateDir, destDir) + ProcessTemplates(templateDir, destDir) } else { log.Fatalf("bad template directory arugment: %s. expected \"templateDir:destDir\"", t) } diff --git a/template.go b/template.go index 76b59f5..9345be5 100644 --- a/template.go +++ b/template.go @@ -143,16 +143,25 @@ func generateFile(templatePath, destPath string) bool { // TODO - make this be able to be set from CLI? var templateSuffix = ".tpl" -func processTemplates(sourceDirPath, destDirPath string) { +// ProcessTemplates goes through entire directory +// and will apply templates +func ProcessTemplates(sourceDirPath, destDirPath string) { walker := func(path string, info os.FileInfo, err error) error { if err != nil { log.Fatalf("Could not process %s: %s", sourceDirPath, err) return nil } - if strings.HasSuffix(info.Name(), templateSuffix) { - destFileName := strings.TrimSuffix(info.Name(), templateSuffix) + var destFileName string + + if !info.IsDir() { + // If the file ends in .tpl strip that, otherwise just use the filename + if strings.HasSuffix(info.Name(), templateSuffix) { + destFileName = strings.TrimSuffix(info.Name(), templateSuffix) + } else { + destFileName = info.Name() + } relPath, err := filepath.Rel(sourceDirPath, path) if err != nil { log.Fatalf("Could not split %q into relative path with base %q", @@ -172,9 +181,9 @@ func processTemplates(sourceDirPath, destDirPath string) { log.Println("writing to ", destFileName) generateFile(path, destFileName) - } return nil + } filepath.Walk(sourceDirPath, walker) diff --git a/template_test.go b/template_test.go index 3f84f11..5bdc0cf 100644 --- a/template_test.go +++ b/template_test.go @@ -39,7 +39,7 @@ func (s *MySuite) TestJsonTemplate(c *C) { // Test walking through a tree of templates func (s *MySuite) TestDirTemplates(c *C) { dirpath := filepath.Join("test", "fixtures") - processTemplates(dirpath, s.dir) + ProcessTemplates(dirpath, s.dir) fileCompare(filepath.Join("test", "expected", "one.conf"), filepath.Join(s.dir,