From af0d9bdfe4a50c4c691e2939cb08dfb05dfa3688 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 28 Feb 2017 11:11:55 +0100 Subject: [PATCH] Refactor publish/expose filter to remove duplication Signed-off-by: Vincent Demeester --- daemon/list.go | 61 +++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/daemon/list.go b/daemon/list.go index f104fe05a0..30b0a278c5 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -333,49 +333,13 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte } publishFilter := map[nat.Port]bool{} - err = psFilters.WalkValues("publish", func(value string) error { - if strings.Contains(value, ":") { - return fmt.Errorf("filter for 'publish' should not contain ':': %v", value) - } - //support two formats, original format /[] or /[] - proto, port := nat.SplitProtoPort(value) - start, end, err := nat.ParsePortRange(port) - if err != nil { - return fmt.Errorf("error while looking up for publish %v: %s", value, err) - } - for i := start; i <= end; i++ { - p, err := nat.NewPort(proto, strconv.FormatUint(i, 10)) - if err != nil { - return fmt.Errorf("error while looking up for publish %v: %s", value, err) - } - publishFilter[p] = true - } - return nil - }) + err = psFilters.WalkValues("publish", portOp("publish", publishFilter)) if err != nil { return nil, err } exposeFilter := map[nat.Port]bool{} - err = psFilters.WalkValues("expose", func(value string) error { - if strings.Contains(value, ":") { - return fmt.Errorf("filter for 'expose' should not contain ':': %v", value) - } - //support two formats, original format /[] or /[] - proto, port := nat.SplitProtoPort(value) - start, end, err := nat.ParsePortRange(port) - if err != nil { - return fmt.Errorf("error while looking up for 'expose' %v: %s", value, err) - } - for i := start; i <= end; i++ { - p, err := nat.NewPort(proto, strconv.FormatUint(i, 10)) - if err != nil { - return fmt.Errorf("error while looking up for 'expose' %v: %s", value, err) - } - exposeFilter[p] = true - } - return nil - }) + err = psFilters.WalkValues("expose", portOp("expose", exposeFilter)) if err != nil { return nil, err } @@ -395,6 +359,27 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte names: daemon.nameIndex.GetAll(), }, nil } +func portOp(key string, filter map[nat.Port]bool) func(value string) error { + return func(value string) error { + if strings.Contains(value, ":") { + return fmt.Errorf("filter for '%s' should not contain ':': %s", key, value) + } + //support two formats, original format /[] or /[] + proto, port := nat.SplitProtoPort(value) + start, end, err := nat.ParsePortRange(port) + if err != nil { + return fmt.Errorf("error while looking up for %s %s: %s", key, value, err) + } + for i := start; i <= end; i++ { + p, err := nat.NewPort(proto, strconv.FormatUint(i, 10)) + if err != nil { + return fmt.Errorf("error while looking up for %s %s: %s", key, value, err) + } + filter[p] = true + } + return nil + } +} // includeContainerInList decides whether a container should be included in the output or not based in the filter. // It also decides if the iteration should be stopped or not.