Move filter parsing to common utils

Signed-off-by: Jakub Guzik <jakubmguzik@gmail.com>
This commit is contained in:
Jakub Guzik
2021-05-08 22:47:23 +02:00
parent 59dd357509
commit 3aae346946
2 changed files with 20 additions and 10 deletions

View File

@ -35,6 +35,21 @@ func ReadPodIDFiles(files []string) ([]string, error) {
return ids, nil return ids, nil
} }
// ParseFilters transforms one filter format to another and validates input
func ParseFilters(filter []string) (map[string][]string, error) {
// TODO Remove once filter refactor is finished and url.Values done.
filters := map[string][]string{}
for _, f := range filter {
t := strings.SplitN(f, "=", 2)
filters = make(map[string][]string)
if len(t) < 2 {
return map[string][]string{}, errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
}
filters[t[0]] = append(filters[t[0]], t[1])
}
return filters, nil
}
// createExpose parses user-provided exposed port definitions and converts them // createExpose parses user-provided exposed port definitions and converts them
// into SpecGen format. // into SpecGen format.
// TODO: The SpecGen format should really handle ranges more sanely - we could // TODO: The SpecGen format should really handle ranges more sanely - we could

View File

@ -4,16 +4,15 @@ import (
"bufio" "bufio"
"context" "context"
"fmt" "fmt"
"net/url"
"os" "os"
"strings" "strings"
"github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/completion"
"github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/registry"
"github.com/containers/podman/v3/cmd/podman/utils" "github.com/containers/podman/v3/cmd/podman/utils"
"github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/cmd/podman/validate"
"github.com/containers/podman/v3/pkg/domain/entities" "github.com/containers/podman/v3/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -50,6 +49,7 @@ func init() {
func prune(cmd *cobra.Command, args []string) error { func prune(cmd *cobra.Command, args []string) error {
var ( var (
pruneOptions = entities.ContainerPruneOptions{} pruneOptions = entities.ContainerPruneOptions{}
err error
) )
if !force { if !force {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
@ -64,14 +64,9 @@ func prune(cmd *cobra.Command, args []string) error {
} }
} }
// TODO Remove once filter refactor is finished and url.Values done. pruneOptions.Filters, err = common.ParseFilters(filter)
for _, f := range filter { if err != nil {
t := strings.SplitN(f, "=", 2) return err
pruneOptions.Filters = make(url.Values)
if len(t) < 2 {
return errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
}
pruneOptions.Filters.Add(t[0], t[1])
} }
responses, err := registry.ContainerEngine().ContainerPrune(context.Background(), pruneOptions) responses, err := registry.ContainerEngine().ContainerPrune(context.Background(), pruneOptions)