diff --git a/daemon/errors.go b/daemon/errors.go index bf75244a9a..8f4b950839 100644 --- a/daemon/errors.go +++ b/daemon/errors.go @@ -109,21 +109,6 @@ func (e containerFileNotFound) Error() string { func (containerFileNotFound) NotFound() {} -type invalidFilter struct { - filter string - value interface{} -} - -func (e invalidFilter) Error() string { - msg := "invalid filter '" + e.filter - if e.value != nil { - msg += fmt.Sprintf("=%s", e.value) - } - return msg + "'" -} - -func (e invalidFilter) InvalidParameter() {} - type startInvalidConfigError string func (e startInvalidConfigError) Error() string { diff --git a/daemon/images/image_list.go b/daemon/images/image_list.go index 8ab56a6820..001853cae1 100644 --- a/daemon/images/image_list.go +++ b/daemon/images/image_list.go @@ -38,18 +38,13 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions) return nil, err } - var danglingOnly bool - if opts.Filters.Contains("dangling") { - if opts.Filters.ExactMatch("dangling", "true") { - danglingOnly = true - } else if !opts.Filters.ExactMatch("dangling", "false") { - return nil, invalidFilter{"dangling", opts.Filters.Get("dangling")} - } + danglingOnly, err := opts.Filters.GetBoolOrDefault("dangling", false) + if err != nil { + return nil, err } var ( beforeFilter, sinceFilter time.Time - err error ) err = opts.Filters.WalkValues("before", func(value string) error { img, err := i.GetImage(ctx, value, imagetypes.GetImageOpts{}) diff --git a/daemon/images/image_prune.go b/daemon/images/image_prune.go index 0e4e22f22b..6c461231eb 100644 --- a/daemon/images/image_prune.go +++ b/daemon/images/image_prune.go @@ -46,13 +46,9 @@ func (i *ImageService) ImagesPrune(ctx context.Context, pruneFilters filters.Arg rep := &types.ImagesPruneReport{} - danglingOnly := true - if pruneFilters.Contains("dangling") { - if pruneFilters.ExactMatch("dangling", "false") || pruneFilters.ExactMatch("dangling", "0") { - danglingOnly = false - } else if !pruneFilters.ExactMatch("dangling", "true") && !pruneFilters.ExactMatch("dangling", "1") { - return nil, invalidFilter{"dangling", pruneFilters.Get("dangling")} - } + danglingOnly, err := pruneFilters.GetBoolOrDefault("dangling", true) + if err != nil { + return nil, err } until, err := getUntilFromPruneFilters(pruneFilters) diff --git a/daemon/images/image_search.go b/daemon/images/image_search.go index c824577cca..c31f9175ce 100644 --- a/daemon/images/image_search.go +++ b/daemon/images/image_search.go @@ -7,6 +7,8 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/registry" "github.com/docker/docker/dockerversion" + "github.com/docker/docker/errdefs" + "github.com/pkg/errors" ) var acceptedSearchFilterTags = map[string]bool{ @@ -27,28 +29,22 @@ func (i *ImageService) SearchRegistryForImages(ctx context.Context, searchFilter return nil, err } - var isAutomated, isOfficial bool - var hasStarFilter = 0 - if searchFilters.Contains("is-automated") { - if searchFilters.UniqueExactMatch("is-automated", "true") { - isAutomated = true - } else if !searchFilters.UniqueExactMatch("is-automated", "false") { - return nil, invalidFilter{"is-automated", searchFilters.Get("is-automated")} - } + isAutomated, err := searchFilters.GetBoolOrDefault("is-automated", false) + if err != nil { + return nil, err } - if searchFilters.Contains("is-official") { - if searchFilters.UniqueExactMatch("is-official", "true") { - isOfficial = true - } else if !searchFilters.UniqueExactMatch("is-official", "false") { - return nil, invalidFilter{"is-official", searchFilters.Get("is-official")} - } + isOfficial, err := searchFilters.GetBoolOrDefault("is-official", false) + if err != nil { + return nil, err } + + hasStarFilter := 0 if searchFilters.Contains("stars") { hasStars := searchFilters.Get("stars") for _, hasStar := range hasStars { iHasStar, err := strconv.Atoi(hasStar) if err != nil { - return nil, invalidFilter{"stars", hasStar} + return nil, errdefs.InvalidParameter(errors.Wrapf(err, "invalid filter 'stars=%s'", hasStar)) } if iHasStar > hasStarFilter { hasStarFilter = iHasStar diff --git a/daemon/images/locals.go b/daemon/images/locals.go index d62c345b72..77416aa488 100644 --- a/daemon/images/locals.go +++ b/daemon/images/locals.go @@ -1,26 +1,9 @@ package images // import "github.com/docker/docker/daemon/images" import ( - "fmt" - metrics "github.com/docker/go-metrics" ) -type invalidFilter struct { - filter string - value interface{} -} - -func (e invalidFilter) Error() string { - msg := "invalid filter '" + e.filter - if e.value != nil { - msg += fmt.Sprintf("=%s", e.value) - } - return msg + "'" -} - -func (e invalidFilter) InvalidParameter() {} - var imageActions metrics.LabeledTimer func init() { diff --git a/daemon/list.go b/daemon/list.go index c7776816ee..1c97a4e7fd 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -254,7 +254,7 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf err := psFilters.WalkValues("exited", func(value string) error { code, err := strconv.Atoi(value) if err != nil { - return err + return errdefs.InvalidParameter(errors.Wrapf(err, "invalid filter 'exited=%s'", value)) } filtExited = append(filtExited, code) return nil @@ -265,7 +265,7 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf err = psFilters.WalkValues("status", func(value string) error { if !container.IsValidStateString(value) { - return invalidFilter{"status", value} + return errdefs.InvalidParameter(fmt.Errorf("invalid filter 'status=%s'", value)) } config.All = true @@ -275,22 +275,15 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf return nil, err } - var taskFilter, isTask bool - if psFilters.Contains("is-task") { - if psFilters.ExactMatch("is-task", "true") { - taskFilter = true - isTask = true - } else if psFilters.ExactMatch("is-task", "false") { - taskFilter = true - isTask = false - } else { - return nil, invalidFilter{"is-task", psFilters.Get("is-task")} - } + taskFilter := psFilters.Contains("is-task") + isTask, err := psFilters.GetBoolOrDefault("is-task", false) + if err != nil { + return nil, err } err = psFilters.WalkValues("health", func(value string) error { if !container.IsValidHealthString(value) { - return errdefs.InvalidParameter(errors.Errorf("Unrecognised filter value for health: %s", value)) + return errdefs.InvalidParameter(fmt.Errorf("unrecognized filter value for health: %s", value)) } return nil diff --git a/plugin/backend_linux.go b/plugin/backend_linux.go index 2b2d4adb09..681a3624a2 100644 --- a/plugin/backend_linux.go +++ b/plugin/backend_linux.go @@ -318,12 +318,15 @@ func (pm *Manager) List(pluginFilters filters.Args) ([]types.Plugin, error) { enabledOnly := false disabledOnly := false if pluginFilters.Contains("enabled") { - if pluginFilters.ExactMatch("enabled", "true") { + enabledFilter, err := pluginFilters.GetBoolOrDefault("enabled", false) + if err != nil { + return nil, err + } + + if enabledFilter { enabledOnly = true - } else if pluginFilters.ExactMatch("enabled", "false") { - disabledOnly = true } else { - return nil, invalidFilter{"enabled", pluginFilters.Get("enabled")} + disabledOnly = true } } diff --git a/plugin/errors.go b/plugin/errors.go index 4aca5ff71d..1958b17761 100644 --- a/plugin/errors.go +++ b/plugin/errors.go @@ -26,21 +26,6 @@ func (name errDisabled) Error() string { func (name errDisabled) Conflict() {} -type invalidFilter struct { - filter string - value []string -} - -func (e invalidFilter) Error() string { - msg := "invalid filter '" + e.filter - if len(e.value) > 0 { - msg += fmt.Sprintf("=%s", e.value) - } - return msg + "'" -} - -func (invalidFilter) InvalidParameter() {} - type inUseError string func (e inUseError) Error() string { diff --git a/volume/service/convert.go b/volume/service/convert.go index 00ea5d2792..b109ce277e 100644 --- a/volume/service/convert.go +++ b/volume/service/convert.go @@ -114,11 +114,9 @@ func filtersToBy(filter filters.Args, acceptedFilters map[string]bool) (By, erro bys = append(bys, byLabelFilter(filter)) if filter.Contains("dangling") { - var dangling bool - if filter.ExactMatch("dangling", "true") || filter.ExactMatch("dangling", "1") { - dangling = true - } else if !filter.ExactMatch("dangling", "false") && !filter.ExactMatch("dangling", "0") { - return nil, invalidFilter{"dangling", filter.Get("dangling")} + dangling, err := filter.GetBoolOrDefault("dangling", false) + if err != nil { + return nil, err } bys = append(bys, ByReferenced(!dangling)) } @@ -138,7 +136,7 @@ func withPrune(filter filters.Args) error { all := filter.Get("all") switch { case len(all) > 1: - return invalidFilter{"all", all} + return errdefs.InvalidParameter(fmt.Errorf("invalid filter 'all=%s': only one value is expected", all)) case len(all) == 1: ok, err := strconv.ParseBool(all[0]) if err != nil { diff --git a/volume/service/errors.go b/volume/service/errors.go index c735fca6cd..3338102121 100644 --- a/volume/service/errors.go +++ b/volume/service/errors.go @@ -1,7 +1,6 @@ package service // import "github.com/docker/docker/volume/service" import ( - "fmt" "strings" ) @@ -94,18 +93,3 @@ func isErr(err error, expected error) bool { } return err == expected } - -type invalidFilter struct { - filter string - value interface{} -} - -func (e invalidFilter) Error() string { - msg := "invalid filter '" + e.filter - if e.value != nil { - msg += fmt.Sprintf("=%s", e.value) - } - return msg + "'" -} - -func (e invalidFilter) InvalidParameter() {}