The pattern of parsing bool was repeated across multiple files and
caused the duplication of the invalidFilter error helper.
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
| ... | ... |
@@ -109,21 +109,6 @@ func (e containerFileNotFound) Error() string {
|
| 109 | 109 |
|
| 110 | 110 |
func (containerFileNotFound) NotFound() {}
|
| 111 | 111 |
|
| 112 |
-type invalidFilter struct {
|
|
| 113 |
- filter string |
|
| 114 |
- value interface{}
|
|
| 115 |
-} |
|
| 116 |
- |
|
| 117 |
-func (e invalidFilter) Error() string {
|
|
| 118 |
- msg := "invalid filter '" + e.filter |
|
| 119 |
- if e.value != nil {
|
|
| 120 |
- msg += fmt.Sprintf("=%s", e.value)
|
|
| 121 |
- } |
|
| 122 |
- return msg + "'" |
|
| 123 |
-} |
|
| 124 |
- |
|
| 125 |
-func (e invalidFilter) InvalidParameter() {}
|
|
| 126 |
- |
|
| 127 | 112 |
type startInvalidConfigError string |
| 128 | 113 |
|
| 129 | 114 |
func (e startInvalidConfigError) Error() string {
|
| ... | ... |
@@ -38,18 +38,13 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions) |
| 38 | 38 |
return nil, err |
| 39 | 39 |
} |
| 40 | 40 |
|
| 41 |
- var danglingOnly bool |
|
| 42 |
- if opts.Filters.Contains("dangling") {
|
|
| 43 |
- if opts.Filters.ExactMatch("dangling", "true") {
|
|
| 44 |
- danglingOnly = true |
|
| 45 |
- } else if !opts.Filters.ExactMatch("dangling", "false") {
|
|
| 46 |
- return nil, invalidFilter{"dangling", opts.Filters.Get("dangling")}
|
|
| 47 |
- } |
|
| 41 |
+ danglingOnly, err := opts.Filters.GetBoolOrDefault("dangling", false)
|
|
| 42 |
+ if err != nil {
|
|
| 43 |
+ return nil, err |
|
| 48 | 44 |
} |
| 49 | 45 |
|
| 50 | 46 |
var ( |
| 51 | 47 |
beforeFilter, sinceFilter time.Time |
| 52 |
- err error |
|
| 53 | 48 |
) |
| 54 | 49 |
err = opts.Filters.WalkValues("before", func(value string) error {
|
| 55 | 50 |
img, err := i.GetImage(ctx, value, imagetypes.GetImageOpts{})
|
| ... | ... |
@@ -46,13 +46,9 @@ func (i *ImageService) ImagesPrune(ctx context.Context, pruneFilters filters.Arg |
| 46 | 46 |
|
| 47 | 47 |
rep := &types.ImagesPruneReport{}
|
| 48 | 48 |
|
| 49 |
- danglingOnly := true |
|
| 50 |
- if pruneFilters.Contains("dangling") {
|
|
| 51 |
- if pruneFilters.ExactMatch("dangling", "false") || pruneFilters.ExactMatch("dangling", "0") {
|
|
| 52 |
- danglingOnly = false |
|
| 53 |
- } else if !pruneFilters.ExactMatch("dangling", "true") && !pruneFilters.ExactMatch("dangling", "1") {
|
|
| 54 |
- return nil, invalidFilter{"dangling", pruneFilters.Get("dangling")}
|
|
| 55 |
- } |
|
| 49 |
+ danglingOnly, err := pruneFilters.GetBoolOrDefault("dangling", true)
|
|
| 50 |
+ if err != nil {
|
|
| 51 |
+ return nil, err |
|
| 56 | 52 |
} |
| 57 | 53 |
|
| 58 | 54 |
until, err := getUntilFromPruneFilters(pruneFilters) |
| ... | ... |
@@ -7,6 +7,8 @@ import ( |
| 7 | 7 |
"github.com/docker/docker/api/types/filters" |
| 8 | 8 |
"github.com/docker/docker/api/types/registry" |
| 9 | 9 |
"github.com/docker/docker/dockerversion" |
| 10 |
+ "github.com/docker/docker/errdefs" |
|
| 11 |
+ "github.com/pkg/errors" |
|
| 10 | 12 |
) |
| 11 | 13 |
|
| 12 | 14 |
var acceptedSearchFilterTags = map[string]bool{
|
| ... | ... |
@@ -27,28 +29,22 @@ func (i *ImageService) SearchRegistryForImages(ctx context.Context, searchFilter |
| 27 | 27 |
return nil, err |
| 28 | 28 |
} |
| 29 | 29 |
|
| 30 |
- var isAutomated, isOfficial bool |
|
| 31 |
- var hasStarFilter = 0 |
|
| 32 |
- if searchFilters.Contains("is-automated") {
|
|
| 33 |
- if searchFilters.UniqueExactMatch("is-automated", "true") {
|
|
| 34 |
- isAutomated = true |
|
| 35 |
- } else if !searchFilters.UniqueExactMatch("is-automated", "false") {
|
|
| 36 |
- return nil, invalidFilter{"is-automated", searchFilters.Get("is-automated")}
|
|
| 37 |
- } |
|
| 30 |
+ isAutomated, err := searchFilters.GetBoolOrDefault("is-automated", false)
|
|
| 31 |
+ if err != nil {
|
|
| 32 |
+ return nil, err |
|
| 38 | 33 |
} |
| 39 |
- if searchFilters.Contains("is-official") {
|
|
| 40 |
- if searchFilters.UniqueExactMatch("is-official", "true") {
|
|
| 41 |
- isOfficial = true |
|
| 42 |
- } else if !searchFilters.UniqueExactMatch("is-official", "false") {
|
|
| 43 |
- return nil, invalidFilter{"is-official", searchFilters.Get("is-official")}
|
|
| 44 |
- } |
|
| 34 |
+ isOfficial, err := searchFilters.GetBoolOrDefault("is-official", false)
|
|
| 35 |
+ if err != nil {
|
|
| 36 |
+ return nil, err |
|
| 45 | 37 |
} |
| 38 |
+ |
|
| 39 |
+ hasStarFilter := 0 |
|
| 46 | 40 |
if searchFilters.Contains("stars") {
|
| 47 | 41 |
hasStars := searchFilters.Get("stars")
|
| 48 | 42 |
for _, hasStar := range hasStars {
|
| 49 | 43 |
iHasStar, err := strconv.Atoi(hasStar) |
| 50 | 44 |
if err != nil {
|
| 51 |
- return nil, invalidFilter{"stars", hasStar}
|
|
| 45 |
+ return nil, errdefs.InvalidParameter(errors.Wrapf(err, "invalid filter 'stars=%s'", hasStar)) |
|
| 52 | 46 |
} |
| 53 | 47 |
if iHasStar > hasStarFilter {
|
| 54 | 48 |
hasStarFilter = iHasStar |
| ... | ... |
@@ -1,26 +1,9 @@ |
| 1 | 1 |
package images // import "github.com/docker/docker/daemon/images" |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "fmt" |
|
| 5 |
- |
|
| 6 | 4 |
metrics "github.com/docker/go-metrics" |
| 7 | 5 |
) |
| 8 | 6 |
|
| 9 |
-type invalidFilter struct {
|
|
| 10 |
- filter string |
|
| 11 |
- value interface{}
|
|
| 12 |
-} |
|
| 13 |
- |
|
| 14 |
-func (e invalidFilter) Error() string {
|
|
| 15 |
- msg := "invalid filter '" + e.filter |
|
| 16 |
- if e.value != nil {
|
|
| 17 |
- msg += fmt.Sprintf("=%s", e.value)
|
|
| 18 |
- } |
|
| 19 |
- return msg + "'" |
|
| 20 |
-} |
|
| 21 |
- |
|
| 22 |
-func (e invalidFilter) InvalidParameter() {}
|
|
| 23 |
- |
|
| 24 | 7 |
var imageActions metrics.LabeledTimer |
| 25 | 8 |
|
| 26 | 9 |
func init() {
|
| ... | ... |
@@ -254,7 +254,7 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf |
| 254 | 254 |
err := psFilters.WalkValues("exited", func(value string) error {
|
| 255 | 255 |
code, err := strconv.Atoi(value) |
| 256 | 256 |
if err != nil {
|
| 257 |
- return err |
|
| 257 |
+ return errdefs.InvalidParameter(errors.Wrapf(err, "invalid filter 'exited=%s'", value)) |
|
| 258 | 258 |
} |
| 259 | 259 |
filtExited = append(filtExited, code) |
| 260 | 260 |
return nil |
| ... | ... |
@@ -265,7 +265,7 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf |
| 265 | 265 |
|
| 266 | 266 |
err = psFilters.WalkValues("status", func(value string) error {
|
| 267 | 267 |
if !container.IsValidStateString(value) {
|
| 268 |
- return invalidFilter{"status", value}
|
|
| 268 |
+ return errdefs.InvalidParameter(fmt.Errorf("invalid filter 'status=%s'", value))
|
|
| 269 | 269 |
} |
| 270 | 270 |
|
| 271 | 271 |
config.All = true |
| ... | ... |
@@ -275,22 +275,15 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf |
| 275 | 275 |
return nil, err |
| 276 | 276 |
} |
| 277 | 277 |
|
| 278 |
- var taskFilter, isTask bool |
|
| 279 |
- if psFilters.Contains("is-task") {
|
|
| 280 |
- if psFilters.ExactMatch("is-task", "true") {
|
|
| 281 |
- taskFilter = true |
|
| 282 |
- isTask = true |
|
| 283 |
- } else if psFilters.ExactMatch("is-task", "false") {
|
|
| 284 |
- taskFilter = true |
|
| 285 |
- isTask = false |
|
| 286 |
- } else {
|
|
| 287 |
- return nil, invalidFilter{"is-task", psFilters.Get("is-task")}
|
|
| 288 |
- } |
|
| 278 |
+ taskFilter := psFilters.Contains("is-task")
|
|
| 279 |
+ isTask, err := psFilters.GetBoolOrDefault("is-task", false)
|
|
| 280 |
+ if err != nil {
|
|
| 281 |
+ return nil, err |
|
| 289 | 282 |
} |
| 290 | 283 |
|
| 291 | 284 |
err = psFilters.WalkValues("health", func(value string) error {
|
| 292 | 285 |
if !container.IsValidHealthString(value) {
|
| 293 |
- return errdefs.InvalidParameter(errors.Errorf("Unrecognised filter value for health: %s", value))
|
|
| 286 |
+ return errdefs.InvalidParameter(fmt.Errorf("unrecognized filter value for health: %s", value))
|
|
| 294 | 287 |
} |
| 295 | 288 |
|
| 296 | 289 |
return nil |
| ... | ... |
@@ -318,12 +318,15 @@ func (pm *Manager) List(pluginFilters filters.Args) ([]types.Plugin, error) {
|
| 318 | 318 |
enabledOnly := false |
| 319 | 319 |
disabledOnly := false |
| 320 | 320 |
if pluginFilters.Contains("enabled") {
|
| 321 |
- if pluginFilters.ExactMatch("enabled", "true") {
|
|
| 321 |
+ enabledFilter, err := pluginFilters.GetBoolOrDefault("enabled", false)
|
|
| 322 |
+ if err != nil {
|
|
| 323 |
+ return nil, err |
|
| 324 |
+ } |
|
| 325 |
+ |
|
| 326 |
+ if enabledFilter {
|
|
| 322 | 327 |
enabledOnly = true |
| 323 |
- } else if pluginFilters.ExactMatch("enabled", "false") {
|
|
| 324 |
- disabledOnly = true |
|
| 325 | 328 |
} else {
|
| 326 |
- return nil, invalidFilter{"enabled", pluginFilters.Get("enabled")}
|
|
| 329 |
+ disabledOnly = true |
|
| 327 | 330 |
} |
| 328 | 331 |
} |
| 329 | 332 |
|
| ... | ... |
@@ -26,21 +26,6 @@ func (name errDisabled) Error() string {
|
| 26 | 26 |
|
| 27 | 27 |
func (name errDisabled) Conflict() {}
|
| 28 | 28 |
|
| 29 |
-type invalidFilter struct {
|
|
| 30 |
- filter string |
|
| 31 |
- value []string |
|
| 32 |
-} |
|
| 33 |
- |
|
| 34 |
-func (e invalidFilter) Error() string {
|
|
| 35 |
- msg := "invalid filter '" + e.filter |
|
| 36 |
- if len(e.value) > 0 {
|
|
| 37 |
- msg += fmt.Sprintf("=%s", e.value)
|
|
| 38 |
- } |
|
| 39 |
- return msg + "'" |
|
| 40 |
-} |
|
| 41 |
- |
|
| 42 |
-func (invalidFilter) InvalidParameter() {}
|
|
| 43 |
- |
|
| 44 | 29 |
type inUseError string |
| 45 | 30 |
|
| 46 | 31 |
func (e inUseError) Error() string {
|
| ... | ... |
@@ -114,11 +114,9 @@ func filtersToBy(filter filters.Args, acceptedFilters map[string]bool) (By, erro |
| 114 | 114 |
bys = append(bys, byLabelFilter(filter)) |
| 115 | 115 |
|
| 116 | 116 |
if filter.Contains("dangling") {
|
| 117 |
- var dangling bool |
|
| 118 |
- if filter.ExactMatch("dangling", "true") || filter.ExactMatch("dangling", "1") {
|
|
| 119 |
- dangling = true |
|
| 120 |
- } else if !filter.ExactMatch("dangling", "false") && !filter.ExactMatch("dangling", "0") {
|
|
| 121 |
- return nil, invalidFilter{"dangling", filter.Get("dangling")}
|
|
| 117 |
+ dangling, err := filter.GetBoolOrDefault("dangling", false)
|
|
| 118 |
+ if err != nil {
|
|
| 119 |
+ return nil, err |
|
| 122 | 120 |
} |
| 123 | 121 |
bys = append(bys, ByReferenced(!dangling)) |
| 124 | 122 |
} |
| ... | ... |
@@ -138,7 +136,7 @@ func withPrune(filter filters.Args) error {
|
| 138 | 138 |
all := filter.Get("all")
|
| 139 | 139 |
switch {
|
| 140 | 140 |
case len(all) > 1: |
| 141 |
- return invalidFilter{"all", all}
|
|
| 141 |
+ return errdefs.InvalidParameter(fmt.Errorf("invalid filter 'all=%s': only one value is expected", all))
|
|
| 142 | 142 |
case len(all) == 1: |
| 143 | 143 |
ok, err := strconv.ParseBool(all[0]) |
| 144 | 144 |
if err != nil {
|
| ... | ... |
@@ -1,7 +1,6 @@ |
| 1 | 1 |
package service // import "github.com/docker/docker/volume/service" |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "fmt" |
|
| 5 | 4 |
"strings" |
| 6 | 5 |
) |
| 7 | 6 |
|
| ... | ... |
@@ -94,18 +93,3 @@ func isErr(err error, expected error) bool {
|
| 94 | 94 |
} |
| 95 | 95 |
return err == expected |
| 96 | 96 |
} |
| 97 |
- |
|
| 98 |
-type invalidFilter struct {
|
|
| 99 |
- filter string |
|
| 100 |
- value interface{}
|
|
| 101 |
-} |
|
| 102 |
- |
|
| 103 |
-func (e invalidFilter) Error() string {
|
|
| 104 |
- msg := "invalid filter '" + e.filter |
|
| 105 |
- if e.value != nil {
|
|
| 106 |
- msg += fmt.Sprintf("=%s", e.value)
|
|
| 107 |
- } |
|
| 108 |
- return msg + "'" |
|
| 109 |
-} |
|
| 110 |
- |
|
| 111 |
-func (e invalidFilter) InvalidParameter() {}
|