Skip to content

Commit 1c8c990

Browse files
committed
refactor: handle blank container filters env var
1 parent e0d2383 commit 1c8c990

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

cmd/docker-gen/main.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,6 @@ func initFlags() {
123123
"include stopped containers. Bypassed when providing a container status filter (-container-filter status=foo).")
124124
flag.Var(&containerFilter, "container-filter",
125125
"container filter for inclusion by docker-gen. You can pass this option multiple times to combine filters with AND. https://docs.docker.com/engine/reference/commandline/ps/#filter")
126-
// override containerFilter with DOCKER_CONTAINER_FILTERS environment variable
127-
if filters := strings.Split(os.Getenv("DOCKER_CONTAINER_FILTERS"), ","); len(filters) > 0 && filters[0] != "" {
128-
containerFilter = make(mapstringslice)
129-
for _, filter := range filters {
130-
containerFilter.Set(filter)
131-
}
132-
}
133126

134127
// Command notification options
135128
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated (e.g `restart xyz`)")
@@ -158,6 +151,23 @@ func initFlags() {
158151

159152
flag.Usage = usage
160153
flag.Parse()
154+
155+
// override containerFilter with DOCKER_CONTAINER_FILTERS environment variable
156+
if filtersEnvVar, found := os.LookupEnv("DOCKER_CONTAINER_FILTERS"); found && filtersEnvVar != "" {
157+
var nonBlankFilters []string
158+
for filter := range strings.SplitSeq(filtersEnvVar, ",") {
159+
if strings.TrimSpace(filter) != "" {
160+
nonBlankFilters = append(nonBlankFilters, filter)
161+
}
162+
}
163+
164+
if len(nonBlankFilters) > 0 {
165+
containerFilter = make(mapstringslice)
166+
for _, filter := range nonBlankFilters {
167+
containerFilter.Set(filter)
168+
}
169+
}
170+
}
161171
}
162172

163173
func main() {

0 commit comments

Comments
 (0)