Browse code

Replace uses of filters.Include() with filters.Contains()

The `filters.Include()` method was deprecated in favor of `filters.Contains()`
in 065118390a3ecaf0dbd2fa752d54d43f8f1e8ec6, but still used in various
locations.

This patch replaces uses of `filters.Include()` with `filters.Contains()`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2017/09/26 20:39:56
Showing 9 changed files
... ...
@@ -45,27 +45,27 @@ func filterNetworks(nws []types.NetworkResource, filter filters.Args) ([]types.N
45 45
 
46 46
 	displayNet := []types.NetworkResource{}
47 47
 	for _, nw := range nws {
48
-		if filter.Include("driver") {
48
+		if filter.Contains("driver") {
49 49
 			if !filter.ExactMatch("driver", nw.Driver) {
50 50
 				continue
51 51
 			}
52 52
 		}
53
-		if filter.Include("name") {
53
+		if filter.Contains("name") {
54 54
 			if !filter.Match("name", nw.Name) {
55 55
 				continue
56 56
 			}
57 57
 		}
58
-		if filter.Include("id") {
58
+		if filter.Contains("id") {
59 59
 			if !filter.Match("id", nw.ID) {
60 60
 				continue
61 61
 			}
62 62
 		}
63
-		if filter.Include("label") {
63
+		if filter.Contains("label") {
64 64
 			if !filter.MatchKVList("label", nw.Labels) {
65 65
 				continue
66 66
 			}
67 67
 		}
68
-		if filter.Include("scope") {
68
+		if filter.Contains("scope") {
69 69
 			if !filter.ExactMatch("scope", nw.Scope) {
70 70
 				continue
71 71
 			}
... ...
@@ -73,7 +73,7 @@ func filterNetworks(nws []types.NetworkResource, filter filters.Args) ([]types.N
73 73
 		displayNet = append(displayNet, nw)
74 74
 	}
75 75
 
76
-	if filter.Include("type") {
76
+	if filter.Contains("type") {
77 77
 		typeNet := []types.NetworkResource{}
78 78
 		errFilter := filter.WalkValues("type", func(fval string) error {
79 79
 			passList, err := filterNetworkByType(displayNet, fval)
... ...
@@ -337,6 +337,17 @@ func TestOnlyOneExactMatch(t *testing.T) {
337 337
 	}
338 338
 }
339 339
 
340
+func TestContains(t *testing.T) {
341
+	f := NewArgs()
342
+	if f.Contains("status") {
343
+		t.Fatal("Expected to not contain a status key, got true")
344
+	}
345
+	f.Add("status", "running")
346
+	if !f.Contains("status") {
347
+		t.Fatal("Expected to contain a status key, got false")
348
+	}
349
+}
350
+
340 351
 func TestInclude(t *testing.T) {
341 352
 	f := NewArgs()
342 353
 	if f.Include("status") {
... ...
@@ -74,7 +74,7 @@ func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Serv
74 74
 	services := make([]types.Service, 0, len(r.Services))
75 75
 
76 76
 	for _, service := range r.Services {
77
-		if options.Filters.Include("mode") {
77
+		if options.Filters.Contains("mode") {
78 78
 			var mode string
79 79
 			switch service.Spec.GetMode().(type) {
80 80
 			case *swarmapi.ServiceSpec_Global:
... ...
@@ -49,14 +49,14 @@ func (ef *Filter) filterContains(field string, values map[string]struct{}) bool
49 49
 }
50 50
 
51 51
 func (ef *Filter) matchScope(scope string) bool {
52
-	if !ef.filter.Include("scope") {
52
+	if !ef.filter.Contains("scope") {
53 53
 		return true
54 54
 	}
55 55
 	return ef.filter.ExactMatch("scope", scope)
56 56
 }
57 57
 
58 58
 func (ef *Filter) matchLabels(attributes map[string]string) bool {
59
-	if !ef.filter.Include("label") {
59
+	if !ef.filter.Contains("label") {
60 60
 		return true
61 61
 	}
62 62
 	return ef.filter.MatchKVList("label", attributes)
... ...
@@ -67,7 +67,7 @@ func (daemon *Daemon) Images(imageFilters filters.Args, all bool, withExtraAttrs
67 67
 		return nil, err
68 68
 	}
69 69
 
70
-	if imageFilters.Include("dangling") {
70
+	if imageFilters.Contains("dangling") {
71 71
 		if imageFilters.ExactMatch("dangling", "true") {
72 72
 			danglingOnly = true
73 73
 		} else if !imageFilters.ExactMatch("dangling", "false") {
... ...
@@ -116,7 +116,7 @@ func (daemon *Daemon) Images(imageFilters filters.Args, all bool, withExtraAttrs
116 116
 			}
117 117
 		}
118 118
 
119
-		if imageFilters.Include("label") {
119
+		if imageFilters.Contains("label") {
120 120
 			// Very old image that do not have image.Config (or even labels)
121 121
 			if img.Config == nil {
122 122
 				continue
... ...
@@ -150,7 +150,7 @@ func (daemon *Daemon) Images(imageFilters filters.Args, all bool, withExtraAttrs
150 150
 		newImage := newImage(img, size)
151 151
 
152 152
 		for _, ref := range daemon.referenceStore.References(id.Digest()) {
153
-			if imageFilters.Include("reference") {
153
+			if imageFilters.Contains("reference") {
154 154
 				var found bool
155 155
 				var matchErr error
156 156
 				for _, pattern := range imageFilters.Get("reference") {
... ...
@@ -173,11 +173,11 @@ func (daemon *Daemon) Images(imageFilters filters.Args, all bool, withExtraAttrs
173 173
 		if newImage.RepoDigests == nil && newImage.RepoTags == nil {
174 174
 			if all || len(daemon.stores[platform].imageStore.Children(id)) == 0 {
175 175
 
176
-				if imageFilters.Include("dangling") && !danglingOnly {
176
+				if imageFilters.Contains("dangling") && !danglingOnly {
177 177
 					//dangling=false case, so dangling image is not needed
178 178
 					continue
179 179
 				}
180
-				if imageFilters.Include("reference") { // skip images with no references if filtering by reference
180
+				if imageFilters.Contains("reference") { // skip images with no references if filtering by reference
181 181
 					continue
182 182
 				}
183 183
 				newImage.RepoDigests = []string{"<none>@<none>"}
... ...
@@ -276,7 +276,7 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
276 276
 	}
277 277
 
278 278
 	var taskFilter, isTask bool
279
-	if psFilters.Include("is-task") {
279
+	if psFilters.Contains("is-task") {
280 280
 		if psFilters.ExactMatch("is-task", "true") {
281 281
 			taskFilter = true
282 282
 			isTask = true
... ...
@@ -319,7 +319,7 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
319 319
 
320 320
 	imagesFilter := map[image.ID]bool{}
321 321
 	var ancestorFilter bool
322
-	if psFilters.Include("ancestor") {
322
+	if psFilters.Contains("ancestor") {
323 323
 		ancestorFilter = true
324 324
 		psFilters.WalkValues("ancestor", func(ancestor string) error {
325 325
 			id, platform, err := daemon.GetImageIDAndPlatform(ancestor)
... ...
@@ -465,7 +465,7 @@ func includeContainerInList(container *container.Snapshot, ctx *listContext) ite
465 465
 		return excludeContainer
466 466
 	}
467 467
 
468
-	if ctx.filters.Include("volume") {
468
+	if ctx.filters.Contains("volume") {
469 469
 		volumesByName := make(map[string]types.MountPoint)
470 470
 		for _, m := range container.Mounts {
471 471
 			if m.Name != "" {
... ...
@@ -509,7 +509,7 @@ func includeContainerInList(container *container.Snapshot, ctx *listContext) ite
509 509
 		networkExist = errors.New("container part of network")
510 510
 		noNetworks   = errors.New("container is not part of any networks")
511 511
 	)
512
-	if ctx.filters.Include("network") {
512
+	if ctx.filters.Contains("network") {
513 513
 		err := ctx.filters.WalkValues("network", func(value string) error {
514 514
 			if container.NetworkSettings == nil {
515 515
 				return noNetworks
... ...
@@ -627,17 +627,17 @@ func (daemon *Daemon) filterVolumes(vols []volume.Volume, filter filters.Args) (
627 627
 
628 628
 	var retVols []volume.Volume
629 629
 	for _, vol := range vols {
630
-		if filter.Include("name") {
630
+		if filter.Contains("name") {
631 631
 			if !filter.Match("name", vol.Name()) {
632 632
 				continue
633 633
 			}
634 634
 		}
635
-		if filter.Include("driver") {
635
+		if filter.Contains("driver") {
636 636
 			if !filter.ExactMatch("driver", vol.DriverName()) {
637 637
 				continue
638 638
 			}
639 639
 		}
640
-		if filter.Include("label") {
640
+		if filter.Contains("label") {
641 641
 			v, ok := vol.(volume.DetailedVolume)
642 642
 			if !ok {
643 643
 				continue
... ...
@@ -649,7 +649,7 @@ func (daemon *Daemon) filterVolumes(vols []volume.Volume, filter filters.Args) (
649 649
 		retVols = append(retVols, vol)
650 650
 	}
651 651
 	danglingOnly := false
652
-	if filter.Include("dangling") {
652
+	if filter.Contains("dangling") {
653 653
 		if filter.ExactMatch("dangling", "true") || filter.ExactMatch("dangling", "1") {
654 654
 			danglingOnly = true
655 655
 		} else if !filter.ExactMatch("dangling", "false") && !filter.ExactMatch("dangling", "0") {
... ...
@@ -182,7 +182,7 @@ func (daemon *Daemon) ImagesPrune(ctx context.Context, pruneFilters filters.Args
182 182
 	rep := &types.ImagesPruneReport{}
183 183
 
184 184
 	danglingOnly := true
185
-	if pruneFilters.Include("dangling") {
185
+	if pruneFilters.Contains("dangling") {
186 186
 		if pruneFilters.ExactMatch("dangling", "false") || pruneFilters.ExactMatch("dangling", "0") {
187 187
 			danglingOnly = false
188 188
 		} else if !pruneFilters.ExactMatch("dangling", "true") && !pruneFilters.ExactMatch("dangling", "1") {
... ...
@@ -440,7 +440,7 @@ func (daemon *Daemon) NetworksPrune(ctx context.Context, pruneFilters filters.Ar
440 440
 
441 441
 func getUntilFromPruneFilters(pruneFilters filters.Args) (time.Time, error) {
442 442
 	until := time.Time{}
443
-	if !pruneFilters.Include("until") {
443
+	if !pruneFilters.Contains("until") {
444 444
 		return until, nil
445 445
 	}
446 446
 	untilFilters := pruneFilters.Get("until")
... ...
@@ -464,8 +464,8 @@ func matchLabels(pruneFilters filters.Args, labels map[string]string) bool {
464 464
 		return false
465 465
 	}
466 466
 	// By default MatchKVList will return true if field (like 'label!') does not exist
467
-	// So we have to add additional Include("label!") check
468
-	if pruneFilters.Include("label!") {
467
+	// So we have to add additional Contains("label!") check
468
+	if pruneFilters.Contains("label!") {
469 469
 		if pruneFilters.MatchKVList("label!", labels) {
470 470
 			return false
471 471
 		}
... ...
@@ -33,21 +33,21 @@ func (daemon *Daemon) SearchRegistryForImages(ctx context.Context, filtersArgs s
33 33
 
34 34
 	var isAutomated, isOfficial bool
35 35
 	var hasStarFilter = 0
36
-	if searchFilters.Include("is-automated") {
36
+	if searchFilters.Contains("is-automated") {
37 37
 		if searchFilters.UniqueExactMatch("is-automated", "true") {
38 38
 			isAutomated = true
39 39
 		} else if !searchFilters.UniqueExactMatch("is-automated", "false") {
40 40
 			return nil, invalidFilter{"is-automated", searchFilters.Get("is-automated")}
41 41
 		}
42 42
 	}
43
-	if searchFilters.Include("is-official") {
43
+	if searchFilters.Contains("is-official") {
44 44
 		if searchFilters.UniqueExactMatch("is-official", "true") {
45 45
 			isOfficial = true
46 46
 		} else if !searchFilters.UniqueExactMatch("is-official", "false") {
47 47
 			return nil, invalidFilter{"is-official", searchFilters.Get("is-official")}
48 48
 		}
49 49
 	}
50
-	if searchFilters.Include("stars") {
50
+	if searchFilters.Contains("stars") {
51 51
 		hasStars := searchFilters.Get("stars")
52 52
 		for _, hasStar := range hasStars {
53 53
 			iHasStar, err := strconv.Atoi(hasStar)
... ...
@@ -67,17 +67,17 @@ func (daemon *Daemon) SearchRegistryForImages(ctx context.Context, filtersArgs s
67 67
 
68 68
 	filteredResults := []registrytypes.SearchResult{}
69 69
 	for _, result := range unfilteredResult.Results {
70
-		if searchFilters.Include("is-automated") {
70
+		if searchFilters.Contains("is-automated") {
71 71
 			if isAutomated != result.IsAutomated {
72 72
 				continue
73 73
 			}
74 74
 		}
75
-		if searchFilters.Include("is-official") {
75
+		if searchFilters.Contains("is-official") {
76 76
 			if isOfficial != result.IsOfficial {
77 77
 				continue
78 78
 			}
79 79
 		}
80
-		if searchFilters.Include("stars") {
80
+		if searchFilters.Contains("stars") {
81 81
 			if result.StarCount < hasStarFilter {
82 82
 				continue
83 83
 			}
... ...
@@ -365,7 +365,7 @@ func (pm *Manager) List(pluginFilters filters.Args) ([]types.Plugin, error) {
365 365
 
366 366
 	enabledOnly := false
367 367
 	disabledOnly := false
368
-	if pluginFilters.Include("enabled") {
368
+	if pluginFilters.Contains("enabled") {
369 369
 		if pluginFilters.ExactMatch("enabled", "true") {
370 370
 			enabledOnly = true
371 371
 		} else if pluginFilters.ExactMatch("enabled", "false") {
... ...
@@ -386,7 +386,7 @@ next:
386 386
 		if disabledOnly && p.PluginObj.Enabled {
387 387
 			continue
388 388
 		}
389
-		if pluginFilters.Include("capability") {
389
+		if pluginFilters.Contains("capability") {
390 390
 			for _, f := range p.GetTypes() {
391 391
 				if !pluginFilters.Match("capability", f.Capability) {
392 392
 					continue next