Skip to content

Commit 6e61ca7

Browse files
committed
feat: use filters for -only-exposed / -only-published
1 parent 9801401 commit 6e61ca7

File tree

6 files changed

+22
-68
lines changed

6 files changed

+22
-68
lines changed

cmd/docker-gen/main.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ func main() {
176176
NotifyCmd: notifyCmd,
177177
NotifyOutput: notifyOutput,
178178
NotifyContainers: make(map[string]int),
179-
OnlyExposed: onlyExposed,
180-
OnlyPublished: onlyPublished,
181179
ContainerFilter: containerFilter,
182180
Interval: interval,
183181
KeepBlankLines: keepBlankLines,
@@ -192,8 +190,25 @@ func main() {
192190
cfg.NotifyContainersFilter = notifyContainerFilter
193191
cfg.NotifyContainersSignal = notifyContainerSignal
194192
}
195-
if len(containerFilter) == 0 && !includeStopped {
196-
cfg.ContainerFilter = map[string][]string{"status": {"running"}}
193+
if len(cfg.ContainerFilter["status"]) == 0 {
194+
if includeStopped {
195+
cfg.ContainerFilter["status"] = []string{
196+
"created",
197+
"restarting",
198+
"running",
199+
"removing",
200+
"paused",
201+
"exited",
202+
"dead",
203+
}
204+
} else {
205+
cfg.ContainerFilter["status"] = []string{"running"}
206+
}
207+
}
208+
if onlyPublished && len(cfg.ContainerFilter["publish"]) == 0 {
209+
cfg.ContainerFilter["publish"] = []string{"1-65535"}
210+
} else if onlyExposed && len(cfg.ContainerFilter["publish"]) == 0 {
211+
cfg.ContainerFilter["expose"] = []string{"1-65535"}
197212
}
198213
configs = config.ConfigFile{
199214
Config: []config.Config{cfg},

internal/config/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ type Config struct {
1616
NotifyContainers map[string]int
1717
NotifyContainersFilter map[string][]string
1818
NotifyContainersSignal int
19-
OnlyExposed bool
20-
OnlyPublished bool
2119
ContainerFilter map[string][]string
2220
Interval int
2321
KeepBlankLines bool

internal/context/context.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,6 @@ func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {
105105
return r.ID == o.ID && r.Image == o.Image
106106
}
107107

108-
func (r *RuntimeContainer) PublishedAddresses() []Address {
109-
mapped := []Address{}
110-
for _, address := range r.Addresses {
111-
if address.HostPort != "" {
112-
mapped = append(mapped, address)
113-
}
114-
}
115-
return mapped
116-
}
117-
118108
type DockerImage struct {
119109
Registry string
120110
Repository string

internal/context/context_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -132,37 +132,6 @@ func TestGetCurrentContainerEmpty(t *testing.T) {
132132
assert.Equal(t, "", GetCurrentContainerID())
133133
}
134134

135-
func TestPublishedAddresses(t *testing.T) {
136-
container := &RuntimeContainer{
137-
Addresses: []Address{
138-
{
139-
IP: "172.19.0.1",
140-
HostPort: "80",
141-
},
142-
{
143-
IP: "172.19.0.2",
144-
},
145-
{
146-
IP: "172.19.0.3",
147-
HostPort: "8080",
148-
},
149-
},
150-
}
151-
152-
expected := []Address{
153-
{
154-
IP: "172.19.0.1",
155-
HostPort: "80",
156-
},
157-
{
158-
IP: "172.19.0.3",
159-
HostPort: "8080",
160-
},
161-
}
162-
163-
assert.ElementsMatch(t, expected, container.PublishedAddresses())
164-
}
165-
166135
func TestRuntimeContainerEquals(t *testing.T) {
167136
rc1 := &RuntimeContainer{
168137
ID: "baz",

internal/generator/generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func TestGenerateFromEvents(t *testing.T) {
207207
// │ start │ │ stop │ │ start │
208208
// └───────┘ └──────┘ └───────┘
209209

210-
expectedCounters := []int{1, 5, 6, 7}
210+
expectedCounters := []int{1, 8, 9, 10}
211211

212212
for i, counter := range expectedCounters {
213213
value, _ = os.ReadFile(destFiles[i].Name())

internal/template/template.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,7 @@ func removeBlankLines(reader io.Reader, writer io.Writer) {
150150
}
151151

152152
func GenerateFile(config config.Config, containers context.Context) bool {
153-
filteredContainers := context.Context{}
154-
if config.OnlyPublished {
155-
for _, container := range containers {
156-
if len(container.PublishedAddresses()) > 0 {
157-
filteredContainers = append(filteredContainers, container)
158-
}
159-
}
160-
} else if config.OnlyExposed {
161-
for _, container := range containers {
162-
if len(container.Addresses) > 0 {
163-
filteredContainers = append(filteredContainers, container)
164-
}
165-
}
166-
} else {
167-
filteredContainers = containers
168-
}
169-
170-
contents := executeTemplate(config.Template, filteredContainers)
153+
contents := executeTemplate(config.Template, containers)
171154

172155
if !config.KeepBlankLines {
173156
buf := new(bytes.Buffer)
@@ -186,8 +169,7 @@ func GenerateFile(config config.Config, containers context.Context) bool {
186169
if err != nil {
187170
log.Fatalf("Unable to write to dest file %s: %s\n", config.Dest, err)
188171
}
189-
190-
log.Printf("Generated '%s' from %d containers", config.Dest, len(filteredContainers))
172+
log.Printf("Generated '%s' from %d containers", config.Dest, len(containers))
191173
return true
192174
}
193175
return false

0 commit comments

Comments
 (0)