Skip to content

Commit aa0b789

Browse files
committed
feat: use filters for -only-exposed / -only-published
1 parent 5aa8bc9 commit aa0b789

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
@@ -180,8 +180,6 @@ func main() {
180180
NotifyCmd: notifyCmd,
181181
NotifyOutput: notifyOutput,
182182
NotifyContainers: make(map[string]int),
183-
OnlyExposed: onlyExposed,
184-
OnlyPublished: onlyPublished,
185183
ContainerFilter: containerFilter,
186184
Interval: interval,
187185
KeepBlankLines: keepBlankLines,
@@ -196,8 +194,25 @@ func main() {
196194
cfg.NotifyContainersFilter = notifyContainerFilter
197195
cfg.NotifyContainersSignal = notifyContainerSignal
198196
}
199-
if len(containerFilter) == 0 && !includeStopped {
200-
cfg.ContainerFilter = map[string][]string{"status": {"running"}}
197+
if len(cfg.ContainerFilter["status"]) == 0 {
198+
if includeStopped {
199+
cfg.ContainerFilter["status"] = []string{
200+
"created",
201+
"restarting",
202+
"running",
203+
"removing",
204+
"paused",
205+
"exited",
206+
"dead",
207+
}
208+
} else {
209+
cfg.ContainerFilter["status"] = []string{"running"}
210+
}
211+
}
212+
if onlyPublished && len(cfg.ContainerFilter["publish"]) == 0 {
213+
cfg.ContainerFilter["publish"] = []string{"1-65535"}
214+
} else if onlyExposed && len(cfg.ContainerFilter["expose"]) == 0 {
215+
cfg.ContainerFilter["expose"] = []string{"1-65535"}
201216
}
202217
configs = config.ConfigFile{
203218
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
@@ -206,7 +206,7 @@ func TestGenerateFromEvents(t *testing.T) {
206206
// │ start │ │ stop │ │ start │
207207
// └───────┘ └──────┘ └───────┘
208208

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

211211
for i, counter := range expectedCounters {
212212
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)