diff --git a/README.md b/README.md index 3d96de6..a43ec43 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,14 @@ $ dockerize -template template1.tmpl ``` +Template may also be a directory. In this case all files within this directory are processed as template and stored with the same name in the destination directory. +If the destination directory is omitted, the output is sent to `STDOUT`. The files in the source directory are processed in sorted order (as returned by `ioutil.ReadDir`). + +``` +$ dockerize -template src_dir:dest_dir + +``` + You can tail multiple files to `STDOUT` and `STDERR` by passing the options multiple times. diff --git a/dockerize.go b/dockerize.go index 55aa0b4..625f69a 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 + templateDirsFlag 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,7 +167,7 @@ func main() { flag.BoolVar(&version, "version", false, "show version") flag.BoolVar(&poll, "poll", false, "enable polling") - flag.Var(&templatesFlag, "template", "Template (/template:/dest). Can be passed multiple times") + flag.Var(&templatesFlag, "template", "Template (/template:/dest). Can be passed multiple times. Does also support directories") 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") flag.StringVar(&delimsFlag, "delims", "", `template tag delimiters. default "{{":"}}" `) @@ -230,7 +231,16 @@ func main() { } template, dest = parts[0], parts[1] } - generateFile(template, dest) + + fi, err := os.Stat(template) + if err != nil { + log.Fatalf("unable to stat %s, error: %s", template, err) + } + if fi.IsDir() { + generateDir(template, dest) + } else { + generateFile(template, dest) + } } waitForDependencies() diff --git a/template.go b/template.go index 6640ded..01f3a92 100644 --- a/template.go +++ b/template.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io/ioutil" "log" "net/url" "os" @@ -124,3 +125,30 @@ func generateFile(templatePath, destPath string) bool { return true } + +func generateDir(templateDir, destDir string) bool { + if destDir != "" { + fiDest, err := os.Stat(destDir) + if err != nil { + log.Fatalf("unable to stat %s, error: %s", destDir, err) + } + if !fiDest.IsDir() { + log.Fatalf("if template is a directory, dest must also be a directory (or stdout)") + } + } + + files, err := ioutil.ReadDir(templateDir) + if err != nil { + log.Fatalf("bad directory: %s, error: %s", templateDir, err) + } + + for _, file := range files { + if destDir == "" { + generateFile(filepath.Join(templateDir, file.Name()), "") + } else { + generateFile(filepath.Join(templateDir, file.Name()), filepath.Join(destDir, file.Name())) + } + } + + return true +}