Skip to content

Commit bcfdb4c

Browse files
committed
skip-blank-lines option
fixes #16
1 parent f622fc7 commit bcfdb4c

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ $ docker run -d --name nginx-gen --volumes-from nginx \
7777
### Usage
7878
```
7979
$ docker-gen
80-
Usage: docker-gen [-config file] [-watch=false] [-notify="restart xyz"] [-notify-sighup="nginx-proxy"] [-interval=0] [-endpoint tcp|unix://..] [-tlsverify] [-tlscert file] [-tlskey file] [-tlscacert file] <template> [<dest>]
80+
Usage: docker-gen [-config file] [-watch=false] [-notify="restart xyz"] [-notify-sighup="nginx-proxy"] [-interval=0] [-endpoint tcp|unix://..] [-tlsverify] [-tlscert file] [-tlskey file] [-tlscacert file] [-skip-blank-lines] <template> [<dest>]
8181
```
8282

8383
*Options:*
@@ -92,6 +92,7 @@ Usage: docker-gen [-config file] [-watch=false] [-notify="restart xyz"] [-notify
9292
-notify-sighup="": send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`
9393
-only-exposed=false: only include containers with exposed ports
9494
-only-published=false: only include containers with published ports (implies -only-exposed)
95+
-skip-blank-lines=false: skip blank lines in output file
9596
-tlscacert="": path to TLS CA certificate file
9697
-tlscert="": path to TLS client certificate file
9798
-tlskey="": path to TLS client key file

docker-gen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
configFiles stringslice
3030
configs ConfigFile
3131
interval int
32+
skipBlankLines bool
3233
endpoint string
3334
tlsCert string
3435
tlsKey string
@@ -114,6 +115,7 @@ type Config struct {
114115
OnlyExposed bool
115116
OnlyPublished bool
116117
Interval int
118+
SkipBlankLines bool
117119
}
118120

119121
type ConfigFile struct {
@@ -391,6 +393,7 @@ func initFlags() {
391393
"send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`")
392394
flag.Var(&configFiles, "config", "config files with template directives. Config files will be merged if this option is specified multiple times.")
393395
flag.IntVar(&interval, "interval", 0, "notify command interval (secs)")
396+
flag.BoolVar(&skipBlankLines, "skip-blank-lines", false, "skip blank lines in output file")
394397
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint (tcp|unix://..). Default unix:///var/run/docker.sock")
395398
flag.StringVar(&tlsCert, "tlscert", filepath.Join(certPath, "cert.pem"), "path to TLS client certificate file")
396399
flag.StringVar(&tlsKey, "tlskey", filepath.Join(certPath, "key.pem"), "path to TLS client key file")
@@ -431,6 +434,7 @@ func main() {
431434
OnlyExposed: onlyExposed,
432435
OnlyPublished: onlyPublished,
433436
Interval: interval,
437+
SkipBlankLines: skipBlankLines,
434438
}
435439
if notifySigHUPContainerID != "" {
436440
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP

template.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"bytes"
56
"crypto/sha1"
67
"encoding/json"
@@ -398,11 +399,24 @@ func generateFile(config Config, containers Context) bool {
398399
}
399400

400401
var buf bytes.Buffer
401-
multiwriter := io.MultiWriter(dest, &buf)
402-
err = tmpl.ExecuteTemplate(multiwriter, filepath.Base(templatePath), &filteredContainers)
402+
bw := bufio.NewWriter(&buf)
403+
err = tmpl.ExecuteTemplate(bw, filepath.Base(templatePath), &filteredContainers)
403404
if err != nil {
404405
log.Fatalf("template error: %s\n", err)
405406
}
407+
bw.Flush()
408+
409+
if config.SkipBlankLines {
410+
scanner := bufio.NewScanner(bufio.NewReader(&buf))
411+
for scanner.Scan() {
412+
line := scanner.Text()
413+
if len(line) > 0 {
414+
fmt.Fprintln(dest, line)
415+
}
416+
}
417+
} else {
418+
buf.WriteTo(dest)
419+
}
406420

407421
if config.Dest != "" {
408422

0 commit comments

Comments
 (0)