Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow template to be a whole directory #57

Merged
merged 3 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
58 changes: 45 additions & 13 deletions dockerize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -41,17 +42,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
Expand Down Expand Up @@ -166,7 +168,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 "{{":"}}" `)
Expand Down Expand Up @@ -230,7 +232,37 @@ 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() {
if dest != "" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this whole block L241-L262 to a separate func like generateDir?

fiDest, err := os.Stat(dest)
if err != nil {
log.Fatalf("unable to stat %s, error: %s", dest, err)
}
if !fiDest.IsDir() {
log.Fatalf("if template is a directory, dest must also be a directory (or stdout)")
}
}

files, err := ioutil.ReadDir(template)
if err != nil {
log.Fatalf("bad directory: %s, error: %s", template, err)
}

for _, file := range files {
if dest == "" {
generateFile(template+string(os.PathSeparator)+file.Name(), "")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use filepath.Join(template, file.Name) here

} else {
generateFile(template+string(os.PathSeparator)+file.Name(), dest+string(os.PathSeparator)+file.Name())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto: file path.Join(...) for these.

}
}
} else {
generateFile(template, dest)
}
}

waitForDependencies()
Expand Down