|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "encoding/json" |
6 | 5 | "flag" |
7 | 6 | "fmt" |
8 | 7 | "github.com/BurntSushi/toml" |
9 | 8 | "github.com/fsouza/go-dockerclient" |
10 | 9 | "io" |
11 | | - "io/ioutil" |
12 | 10 | "net" |
13 | 11 | "net/http" |
14 | 12 | "net/http/httputil" |
15 | 13 | "os" |
16 | 14 | "os/exec" |
17 | 15 | "os/signal" |
18 | | - "path/filepath" |
19 | 16 |
|
20 | 17 | "strings" |
21 | 18 | "syscall" |
22 | | - "text/template" |
23 | 19 | ) |
24 | 20 |
|
25 | 21 | var ( |
@@ -76,91 +72,10 @@ func (r *RuntimeContainer) Equals(o RuntimeContainer) bool { |
76 | 72 | return r.ID == o.ID && r.Image == o.Image |
77 | 73 | } |
78 | 74 |
|
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 | | - |
97 | 75 | func usage() { |
98 | 76 | println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] <template> [<dest>]") |
99 | 77 | } |
100 | 78 |
|
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 | | - |
164 | 79 | func newConn() (*httputil.ClientConn, error) { |
165 | 80 | conn, err := net.Dial("unix", "/var/run/docker.sock") |
166 | 81 | if err != nil { |
|
0 commit comments