Use GetBoolOrDefault to remove duplicated invalidFilter usages
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>
This commit is contained in:
parent
0d68591c8e
commit
a654cbfd2f
10 changed files with 35 additions and 117 deletions
|
@ -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 {
|
||||
|
|
|
@ -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{})
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")}
|
||||
}
|
||||
}
|
||||
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")}
|
||||
isAutomated, err := searchFilters.GetBoolOrDefault("is-automated", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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() {}
|
||||
|
|
Loading…
Reference in a new issue