Skip to content

Commit 748a621

Browse files
committed
Move template functions to separate file
1 parent a7ed5d4 commit 748a621

File tree

2 files changed

+92
-85
lines changed

2 files changed

+92
-85
lines changed

docker-gen.go

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package main
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"flag"
76
"fmt"
87
"github.com/BurntSushi/toml"
98
"github.com/fsouza/go-dockerclient"
109
"io"
11-
"io/ioutil"
1210
"net"
1311
"net/http"
1412
"net/http/httputil"
1513
"os"
1614
"os/exec"
1715
"os/signal"
18-
"path/filepath"
1916

2017
"strings"
2118
"syscall"
22-
"text/template"
2319
)
2420

2521
var (
@@ -76,91 +72,10 @@ func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {
7672
return r.ID == o.ID && r.Image == o.Image
7773
}
7874

79-
func groupBy(entries []*RuntimeContainer, key string) map[string][]*RuntimeContainer {
80-
groups := make(map[string][]*RuntimeContainer)
81-
for _, v := range entries {
82-
value := deepGet(*v, key)
83-
if value != nil {
84-
groups[value.(string)] = append(groups[value.(string)], v)
85-
}
86-
}
87-
return groups
88-
}
89-
90-
func contains(a map[string]string, b string) bool {
91-
if _, ok := a[b]; ok {
92-
return true
93-
}
94-
return false
95-
}
96-
9775
func usage() {
9876
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] <template> [<dest>]")
9977
}
10078

101-
func generateFile(config Config, containers []*RuntimeContainer) bool {
102-
templatePath := config.Template
103-
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
104-
"contains": contains,
105-
"groupBy": groupBy,
106-
}).ParseFiles(templatePath)
107-
if err != nil {
108-
panic(err)
109-
}
110-
111-
filteredContainers := []*RuntimeContainer{}
112-
if config.OnlyExposed {
113-
for _, container := range containers {
114-
if len(container.Addresses) > 0 {
115-
filteredContainers = append(filteredContainers, container)
116-
}
117-
}
118-
} else {
119-
filteredContainers = containers
120-
}
121-
122-
tmpl = tmpl
123-
dest := os.Stdout
124-
if config.Dest != "" {
125-
dest, err = ioutil.TempFile("", "docker-gen")
126-
defer dest.Close()
127-
if err != nil {
128-
fmt.Printf("unable to create temp file: %s\n", err)
129-
os.Exit(1)
130-
}
131-
}
132-
133-
var buf bytes.Buffer
134-
multiwriter := io.MultiWriter(dest, &buf)
135-
err = tmpl.ExecuteTemplate(multiwriter, filepath.Base(templatePath), containers)
136-
if err != nil {
137-
fmt.Printf("template error: %s\n", err)
138-
}
139-
140-
if config.Dest != "" {
141-
142-
contents := []byte{}
143-
if _, err := os.Stat(config.Dest); err == nil {
144-
contents, err = ioutil.ReadFile(config.Dest)
145-
if err != nil {
146-
fmt.Printf("unable to compare current file contents: %s: %s\n", config.Dest, err)
147-
os.Exit(1)
148-
}
149-
}
150-
151-
if bytes.Compare(contents, buf.Bytes()) != 0 {
152-
err = os.Rename(dest.Name(), config.Dest)
153-
if err != nil {
154-
fmt.Printf("unable to create dest file %s: %s\n", config.Dest, err)
155-
os.Exit(1)
156-
}
157-
return true
158-
}
159-
return false
160-
}
161-
return true
162-
}
163-
16479
func newConn() (*httputil.ClientConn, error) {
16580
conn, err := net.Dial("unix", "/var/run/docker.sock")
16681
if err != nil {

template.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"path/filepath"
10+
"text/template"
11+
)
12+
13+
func groupBy(entries []*RuntimeContainer, key string) map[string][]*RuntimeContainer {
14+
groups := make(map[string][]*RuntimeContainer)
15+
for _, v := range entries {
16+
value := deepGet(*v, key)
17+
if value != nil {
18+
groups[value.(string)] = append(groups[value.(string)], v)
19+
}
20+
}
21+
return groups
22+
}
23+
24+
func contains(item map[string]string, key string) bool {
25+
if _, ok := item[key]; ok {
26+
return true
27+
}
28+
return false
29+
}
30+
31+
func generateFile(config Config, containers []*RuntimeContainer) bool {
32+
templatePath := config.Template
33+
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
34+
"contains": contains,
35+
"groupBy": groupBy,
36+
}).ParseFiles(templatePath)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
filteredContainers := []*RuntimeContainer{}
42+
if config.OnlyExposed {
43+
for _, container := range containers {
44+
if len(container.Addresses) > 0 {
45+
filteredContainers = append(filteredContainers, container)
46+
}
47+
}
48+
} else {
49+
filteredContainers = containers
50+
}
51+
52+
tmpl = tmpl
53+
dest := os.Stdout
54+
if config.Dest != "" {
55+
dest, err = ioutil.TempFile("", "docker-gen")
56+
defer dest.Close()
57+
if err != nil {
58+
fmt.Printf("unable to create temp file: %s\n", err)
59+
os.Exit(1)
60+
}
61+
}
62+
63+
var buf bytes.Buffer
64+
multiwriter := io.MultiWriter(dest, &buf)
65+
err = tmpl.ExecuteTemplate(multiwriter, filepath.Base(templatePath), containers)
66+
if err != nil {
67+
fmt.Printf("template error: %s\n", err)
68+
}
69+
70+
if config.Dest != "" {
71+
72+
contents := []byte{}
73+
if _, err := os.Stat(config.Dest); err == nil {
74+
contents, err = ioutil.ReadFile(config.Dest)
75+
if err != nil {
76+
fmt.Printf("unable to compare current file contents: %s: %s\n", config.Dest, err)
77+
os.Exit(1)
78+
}
79+
}
80+
81+
if bytes.Compare(contents, buf.Bytes()) != 0 {
82+
err = os.Rename(dest.Name(), config.Dest)
83+
if err != nil {
84+
fmt.Printf("unable to create dest file %s: %s\n", config.Dest, err)
85+
os.Exit(1)
86+
}
87+
return true
88+
}
89+
return false
90+
}
91+
return true
92+
}

0 commit comments

Comments
 (0)