Skip to content

Commit 2d078b1

Browse files
authored
Merge pull request #362 from nginx-proxy/restructure
Re-organize the project structure, change the name and clean the dependencies.
2 parents 24bf161 + aa2a909 commit 2d078b1

22 files changed

+210
-246
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
.git
2+
.github
23
docker-gen
34
dist
5+
examples
6+
LICENSE
7+
Makefile
8+
README.md
9+
templates
410
*.gz

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ jobs:
3232
run: make check-gofmt
3333

3434
- name: Run tests
35-
run: go test -v
35+
run: go test -v ./internal/dockergen

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
docker-gen
2+
!cmd/docker-gen
23
dist
34
*.gz

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ get-deps:
4444
go mod download
4545

4646
check-gofmt:
47-
if [ -n "$(shell gofmt -l .)" ]; then \
47+
if [ -n "$(shell gofmt -l ./cmd/docker-gen)" ]; then \
4848
echo 1>&2 'The following files need to be formatted:'; \
49-
gofmt -l .; \
49+
gofmt -l ./cmd/docker-gen; \
50+
exit 1; \
51+
fi
52+
if [ -n "$(shell gofmt -l ./internal/dockergen)" ]; then \
53+
echo 1>&2 'The following files need to be formatted:'; \
54+
gofmt -l ./internal/dockergen; \
5055
exit 1; \
5156
fi
5257

5358
test:
54-
go test
59+
go test ./internal/dockergen

cmd/docker-gen/main.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"log"
77
"os"
88
"path/filepath"
9-
"sync"
109

1110
"github.com/BurntSushi/toml"
1211
docker "github.com/fsouza/go-dockerclient"
13-
"github.com/jwilder/docker-gen"
12+
"github.com/nginx-proxy/docker-gen/internal/dockergen"
1413
)
1514

1615
type stringslice []string
@@ -36,8 +35,6 @@ var (
3635
tlsKey string
3736
tlsCaCert string
3837
tlsVerify bool
39-
tlsCertPath string
40-
wg sync.WaitGroup
4138
)
4239

4340
func (strings *stringslice) String() string {
@@ -69,7 +66,7 @@ Environment Variables:
6966
DOCKER_CERT_PATH - directory path containing key.pem, cert.pem and ca.pem
7067
DOCKER_TLS_VERIFY - enable client TLS verification
7168
`)
72-
println(`For more information, see https://github.com/jwilder/docker-gen`)
69+
println(`For more information, see https://github.com/nginx-proxy/docker-gen`)
7370
}
7471

7572
func loadConfig(file string) error {
File renamed without changes.

go.mod

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
module github.com/jwilder/docker-gen
1+
module github.com/nginx-proxy/docker-gen
22

3-
go 1.11
3+
go 1.16
44

55
require (
6-
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
76
github.com/BurntSushi/toml v0.3.1
8-
github.com/Microsoft/go-winio v0.4.16 // indirect
9-
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
10-
github.com/containerd/continuity v0.0.0-20210315143101-93e15499afd5 // indirect
11-
github.com/docker/docker v1.4.2-0.20171014114940-f2afa2623594 // indirect
12-
github.com/docker/go-connections v0.4.0 // indirect
13-
github.com/docker/go-units v0.3.2 // indirect
14-
github.com/fsouza/go-dockerclient v0.0.0-20171009031830-d2a6d0596004
15-
github.com/gogo/protobuf v1.3.2 // indirect
16-
github.com/google/go-cmp v0.5.5 // indirect
17-
github.com/gorilla/context v1.1.1 // indirect
18-
github.com/gorilla/mux v0.0.0-20160718151158-d391bea3118c // indirect
19-
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
20-
github.com/opencontainers/image-spec v1.0.1 // indirect
21-
github.com/opencontainers/runc v0.1.1 // indirect
22-
github.com/opencontainers/selinux v1.8.0 // indirect
23-
github.com/sirupsen/logrus v1.8.1 // indirect
7+
github.com/fsouza/go-dockerclient v1.7.2
248
github.com/stretchr/testify v1.7.0
25-
golang.org/x/sys v0.0.0-20210331175145-43e1dd70ce54 // indirect
26-
gotest.tools v2.2.0+incompatible // indirect
279
)

go.sum

Lines changed: 96 additions & 111 deletions
Large diffs are not rendered by default.

config.go renamed to internal/dockergen/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func ParseWait(s string) (*Wait, error) {
7373
return nil, err
7474
}
7575
if max < min {
76-
return nil, errors.New("Invalid wait interval: max must be larger than min")
76+
return nil, errors.New("invalid wait interval: max must be larger than min")
7777
}
7878
} else {
7979
max = 4 * min
File renamed without changes.
File renamed without changes.
File renamed without changes.

docker_client.go renamed to internal/dockergen/docker_client.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dockergen
33
import (
44
"errors"
55
"fmt"
6-
"os"
76
"strconv"
87
"strings"
98

@@ -48,7 +47,7 @@ func parseHost(addr string) (string, string, error) {
4847
addr = strings.TrimSpace(addr)
4948
switch {
5049
case addr == "tcp://":
51-
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
50+
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
5251
case strings.HasPrefix(addr, "unix://"):
5352
proto = "unix"
5453
addr = strings.TrimPrefix(addr, "unix://")
@@ -65,15 +64,15 @@ func parseHost(addr string) (string, string, error) {
6564
addr = "/var/run/docker.sock"
6665
default:
6766
if strings.Contains(addr, "://") {
68-
return "", "", fmt.Errorf("Invalid bind address protocol: %s", addr)
67+
return "", "", fmt.Errorf("invalid bind address protocol: %s", addr)
6968
}
7069
proto = "tcp"
7170
}
7271

7372
if proto != "unix" && strings.Contains(addr, ":") {
7473
hostParts := strings.Split(addr, ":")
7574
if len(hostParts) != 2 {
76-
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
75+
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
7776
}
7877
if hostParts[0] != "" {
7978
host = hostParts[0]
@@ -84,11 +83,11 @@ func parseHost(addr string) (string, string, error) {
8483
if p, err := strconv.Atoi(hostParts[1]); err == nil && p != 0 {
8584
port = p
8685
} else {
87-
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
86+
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
8887
}
8988

9089
} else if proto == "tcp" && !strings.Contains(addr, ":") {
91-
return "", "", fmt.Errorf("Invalid bind address format: %s", addr)
90+
return "", "", fmt.Errorf("invalid bind address format: %s", addr)
9291
} else {
9392
host = addr
9493
}
@@ -118,15 +117,3 @@ func splitDockerImage(img string) (string, string, string) {
118117

119118
return registry, repository, tag
120119
}
121-
122-
// pathExists returns whether the given file or directory exists or not
123-
func pathExists(path string) (bool, error) {
124-
_, err := os.Stat(path)
125-
if err == nil {
126-
return true, nil
127-
}
128-
if os.IsNotExist(err) {
129-
return false, nil
130-
}
131-
return false, err
132-
}
File renamed without changes.

generator.go renamed to internal/dockergen/generator.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"syscall"
1212
"time"
1313

14-
"github.com/fsouza/go-dockerclient"
14+
docker "github.com/fsouza/go-dockerclient"
1515
)
1616

1717
type generator struct {
@@ -41,12 +41,12 @@ type GeneratorConfig struct {
4141
func NewGenerator(gc GeneratorConfig) (*generator, error) {
4242
endpoint, err := GetEndpoint(gc.Endpoint)
4343
if err != nil {
44-
return nil, fmt.Errorf("Bad endpoint: %s", err)
44+
return nil, fmt.Errorf("bad endpoint: %s", err)
4545
}
4646

4747
client, err := NewDockerClient(endpoint, gc.TLSVerify, gc.TLSCert, gc.TLSCACert, gc.TLSKey)
4848
if err != nil {
49-
return nil, fmt.Errorf("Unable to create docker client: %s", err)
49+
return nil, fmt.Errorf("unable to create docker client: %s", err)
5050
}
5151

5252
apiVersion, err := client.Version()
@@ -191,7 +191,7 @@ func (g *generator) generateFromEvents() {
191191
watchers = append(watchers, watcher)
192192

193193
debouncedChan := newDebounceChannel(watcher, config.Wait)
194-
for _ = range debouncedChan {
194+
for range debouncedChan {
195195
containers, err := g.getContainers()
196196
if err != nil {
197197
log.Printf("Error listing containers: %s\n", err)
@@ -367,7 +367,8 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
367367

368368
containers := []*RuntimeContainer{}
369369
for _, apiContainer := range apiContainers {
370-
container, err := g.Client.InspectContainer(apiContainer.ID)
370+
opts := docker.InspectContainerOptions{ID: apiContainer.ID}
371+
container, err := g.Client.InspectContainerWithOptions(opts)
371372
if err != nil {
372373
log.Printf("Error inspecting container: %s: %s\n", apiContainer.ID, err)
373374
continue
@@ -465,7 +466,7 @@ func (g *generator) getContainers() ([]*RuntimeContainer, error) {
465466

466467
func newSignalChannel() <-chan os.Signal {
467468
sig := make(chan os.Signal, 1)
468-
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
469+
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
469470

470471
return sig
471472
}

generator_test.go renamed to internal/dockergen/generator_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15-
"github.com/fsouza/go-dockerclient"
15+
docker "github.com/fsouza/go-dockerclient"
1616
dockertest "github.com/fsouza/go-dockerclient/testing"
1717
)
1818

@@ -49,7 +49,7 @@ func TestGenerateFromEvents(t *testing.T) {
4949
}))
5050
server.CustomHandler("/containers/json", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5151
result := []docker.APIContainers{
52-
docker.APIContainers{
52+
{
5353
ID: containerID,
5454
Image: "base:latest",
5555
Command: "/bin/sh",
@@ -87,7 +87,7 @@ func TestGenerateFromEvents(t *testing.T) {
8787
},
8888
Image: "0ff407d5a7d9ed36acdf3e75de8cc127afecc9af234d05486be2981cdc01a38d",
8989
NetworkSettings: &docker.NetworkSettings{
90-
IPAddress: fmt.Sprintf("10.0.0.10"),
90+
IPAddress: "10.0.0.10",
9191
IPPrefixLen: 24,
9292
Gateway: "10.0.0.1",
9393
Bridge: "docker0",
@@ -147,24 +147,24 @@ func TestGenerateFromEvents(t *testing.T) {
147147
Endpoint: serverURL,
148148
Configs: ConfigFile{
149149
[]Config{
150-
Config{
150+
{
151151
Template: tmplFile.Name(),
152152
Dest: destFiles[0].Name(),
153153
Watch: false,
154154
},
155-
Config{
155+
{
156156
Template: tmplFile.Name(),
157157
Dest: destFiles[1].Name(),
158158
Watch: true,
159159
Wait: &Wait{0, 0},
160160
},
161-
Config{
161+
{
162162
Template: tmplFile.Name(),
163163
Dest: destFiles[2].Name(),
164164
Watch: true,
165165
Wait: &Wait{20 * time.Millisecond, 25 * time.Millisecond},
166166
},
167-
Config{
167+
{
168168
Template: tmplFile.Name(),
169169
Dest: destFiles[3].Name(),
170170
Watch: true,
File renamed without changes.

reflect_test.go renamed to internal/dockergen/reflect_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ func TestDeepGetNoPath(t *testing.T) {
1313
t.Fail()
1414
}
1515

16-
var returned RuntimeContainer
17-
returned = value.(RuntimeContainer)
16+
returned := value.(RuntimeContainer)
1817
if !returned.Equals(item) {
1918
t.Fail()
2019
}

template.go renamed to internal/dockergen/template.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ import (
2020
"text/template"
2121
)
2222

23-
func exists(path string) (bool, error) {
24-
_, err := os.Stat(path)
25-
if err == nil {
26-
return true, nil
27-
}
28-
if os.IsNotExist(err) {
29-
return false, nil
30-
}
31-
return false, err
32-
}
33-
3423
func getArrayValues(funcName string, entries interface{}) (*reflect.Value, error) {
3524
entriesVal := reflect.ValueOf(entries)
3625

@@ -45,7 +34,7 @@ func getArrayValues(funcName string, entries interface{}) (*reflect.Value, error
4534
case reflect.Array, reflect.Slice:
4635
break
4736
default:
48-
return nil, fmt.Errorf("Must pass an array or slice to '%v'; received %v; kind %v", funcName, entries, kind)
37+
return nil, fmt.Errorf("must pass an array or slice to '%v'; received %v; kind %v", funcName, entries, kind)
4938
}
5039
return &entriesVal, nil
5140
}
@@ -121,7 +110,7 @@ func groupByLabel(entries interface{}, label string) (map[string][]interface{},
121110
}
122111
return nil, nil
123112
}
124-
return nil, fmt.Errorf("Must pass an array or slice of RuntimeContainer to 'groupByLabel'; received %v", v)
113+
return nil, fmt.Errorf("must pass an array or slice of RuntimeContainer to 'groupByLabel'; received %v", v)
125114
}
126115
return generalizedGroupBy("groupByLabel", entries, getLabel, func(groups map[string][]interface{}, value interface{}, v interface{}) {
127116
groups[value.(string)] = append(groups[value.(string)], v)
@@ -261,7 +250,7 @@ func keys(input interface{}) (interface{}, error) {
261250

262251
val := reflect.ValueOf(input)
263252
if val.Kind() != reflect.Map {
264-
return nil, fmt.Errorf("Cannot call keys on a non-map value: %v", input)
253+
return nil, fmt.Errorf("cannot call keys on a non-map value: %v", input)
265254
}
266255

267256
vk := val.MapKeys()
@@ -435,7 +424,7 @@ func newTemplate(name string) *template.Template {
435424
"contains": contains,
436425
"dict": dict,
437426
"dir": dirList,
438-
"exists": exists,
427+
"exists": pathExists,
439428
"first": arrayFirst,
440429
"groupBy": groupBy,
441430
"groupByKeys": groupByKeys,
@@ -534,7 +523,7 @@ func GenerateFile(config Config, containers Context) bool {
534523
log.Fatalf("Unable to create empty destination file: %s\n", err)
535524
} else {
536525
emptyFile.Close()
537-
fi, err = os.Stat(config.Dest)
526+
fi, _ = os.Stat(config.Dest)
538527
}
539528
}
540529
if err := dest.Chmod(fi.Mode()); err != nil {
@@ -549,7 +538,7 @@ func GenerateFile(config Config, containers Context) bool {
549538
}
550539
}
551540

552-
if bytes.Compare(oldContents, contents) != 0 {
541+
if !bytes.Equal(oldContents, contents) {
553542
err = os.Rename(dest.Name(), config.Dest)
554543
if err != nil {
555544
log.Fatalf("Unable to create dest file %s: %s\n", config.Dest, err)

0 commit comments

Comments
 (0)