forked from nginx-proxy/docker-gen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
129 lines (116 loc) · 2.99 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func groupByMulti(entries []*RuntimeContainer, key, sep string) map[string][]*RuntimeContainer {
groups := make(map[string][]*RuntimeContainer)
for _, v := range entries {
value := deepGet(*v, key)
if value != nil {
items := strings.Split(value.(string), sep)
for _, item := range items {
groups[item] = append(groups[item], v)
}
}
}
return groups
}
func groupBy(entries []*RuntimeContainer, key string) map[string][]*RuntimeContainer {
groups := make(map[string][]*RuntimeContainer)
for _, v := range entries {
value := deepGet(*v, key)
if value != nil {
groups[value.(string)] = append(groups[value.(string)], v)
}
}
return groups
}
func contains(item map[string]string, key string) bool {
if _, ok := item[key]; ok {
return true
}
return false
}
func generateFile(config Config, containers Context) bool {
templatePath := config.Template
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
"contains": contains,
"exists": exists,
"groupBy": groupBy,
"groupByMulti": groupByMulti,
"split": strings.Split,
"replace": strings.Replace,
}).ParseFiles(templatePath)
if err != nil {
log.Fatalf("unable to parse template: %s", err)
}
filteredContainers := Context{}
if config.OnlyPublished {
for _, container := range containers {
if len(container.PublishedAddresses()) > 0 {
filteredContainers = append(filteredContainers, container)
}
}
} else if config.OnlyExposed {
for _, container := range containers {
if len(container.Addresses) > 0 {
filteredContainers = append(filteredContainers, container)
}
}
} else {
filteredContainers = containers
}
dest := os.Stdout
if config.Dest != "" {
dest, err = ioutil.TempFile(filepath.Dir(config.Dest), "docker-gen")
defer func() {
dest.Close()
os.Remove(dest.Name())
}()
if err != nil {
log.Fatalf("unable to create temp file: %s\n", err)
}
}
var buf bytes.Buffer
multiwriter := io.MultiWriter(dest, &buf)
err = tmpl.ExecuteTemplate(multiwriter, filepath.Base(templatePath), &filteredContainers)
if err != nil {
log.Fatalf("template error: %s\n", err)
}
if config.Dest != "" {
contents := []byte{}
if _, err := os.Stat(config.Dest); err == nil {
contents, err = ioutil.ReadFile(config.Dest)
if err != nil {
log.Fatalf("unable to compare current file contents: %s: %s\n", config.Dest, err)
}
}
if bytes.Compare(contents, buf.Bytes()) != 0 {
err = os.Rename(dest.Name(), config.Dest)
if err != nil {
log.Fatalf("unable to create dest file %s: %s\n", config.Dest, err)
}
log.Printf("Generated '%s' from %d containers", config.Dest, len(filteredContainers))
return true
}
return false
}
return true
}