diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-05-14 13:12:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-14 13:12:40 -0400 |
commit | fabaa256676d3cfb611f89922ccaf3405718a6f0 (patch) | |
tree | 82648373f876b243f5c513fa74934940d1b95811 /cmd | |
parent | d05cc0a04a0f5c401624de249505bd160231e2ff (diff) | |
parent | 5e6405334c3d0422b5ec7cd09ef9c58d8f558dba (diff) | |
download | podman-fabaa256676d3cfb611f89922ccaf3405718a6f0.tar.gz podman-fabaa256676d3cfb611f89922ccaf3405718a6f0.tar.bz2 podman-fabaa256676d3cfb611f89922ccaf3405718a6f0.zip |
Merge pull request #10254 from jmguzik/prune-filter-cli
Add support for cli network prune --filter flag
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/podman/common/util.go | 15 | ||||
-rw-r--r-- | cmd/podman/containers/prune.go | 15 | ||||
-rw-r--r-- | cmd/podman/networks/prune.go | 16 |
3 files changed, 32 insertions, 14 deletions
diff --git a/cmd/podman/common/util.go b/cmd/podman/common/util.go index 2a8b2040c..afee55914 100644 --- a/cmd/podman/common/util.go +++ b/cmd/podman/common/util.go @@ -35,6 +35,21 @@ func ReadPodIDFiles(files []string) ([]string, error) { 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 // into SpecGen format. // TODO: The SpecGen format should really handle ranges more sanely - we could diff --git a/cmd/podman/containers/prune.go b/cmd/podman/containers/prune.go index 9acf5589b..837d90f70 100644 --- a/cmd/podman/containers/prune.go +++ b/cmd/podman/containers/prune.go @@ -4,16 +4,15 @@ import ( "bufio" "context" "fmt" - "net/url" "os" "strings" "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/utils" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/pkg/domain/entities" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -50,6 +49,7 @@ func init() { func prune(cmd *cobra.Command, args []string) error { var ( pruneOptions = entities.ContainerPruneOptions{} + err error ) if !force { 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. - for _, f := range filter { - t := strings.SplitN(f, "=", 2) - 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]) + pruneOptions.Filters, err = common.ParseFilters(filter) + if err != nil { + return err } responses, err := registry.ContainerEngine().ContainerPrune(context.Background(), pruneOptions) diff --git a/cmd/podman/networks/prune.go b/cmd/podman/networks/prune.go index ebba06db5..bcc55f0f4 100644 --- a/cmd/podman/networks/prune.go +++ b/cmd/podman/networks/prune.go @@ -6,6 +6,7 @@ import ( "os" "strings" + "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/utils" @@ -31,12 +32,14 @@ var ( var ( networkPruneOptions entities.NetworkPruneOptions force bool + filter = []string{} ) -func networkPruneFlags(flags *pflag.FlagSet) { - //TODO: Not implemented but for future reference - //flags.StringSliceVar(&networkPruneOptions.Filters,"filters", []string{}, "provide filter values (e.g. 'until=<timestamp>')") +func networkPruneFlags(cmd *cobra.Command, flags *pflag.FlagSet) { flags.BoolVarP(&force, "force", "f", false, "do not prompt for confirmation") + filterFlagName := "filter" + flags.StringArrayVar(&filter, filterFlagName, []string{}, "Provide filter values (e.g. 'label=<key>=<value>')") + _ = cmd.RegisterFlagCompletionFunc(filterFlagName, completion.AutocompleteNone) } func init() { @@ -46,12 +49,13 @@ func init() { Parent: networkCmd, }) flags := networkPruneCommand.Flags() - networkPruneFlags(flags) + networkPruneFlags(networkPruneCommand, flags) } func networkPrune(cmd *cobra.Command, _ []string) error { var ( errs utils.OutputErrors + err error ) if !force { reader := bufio.NewReader(os.Stdin) @@ -65,6 +69,10 @@ func networkPrune(cmd *cobra.Command, _ []string) error { return nil } } + networkPruneOptions.Filters, err = common.ParseFilters(filter) + if err != nil { + return err + } responses, err := registry.ContainerEngine().NetworkPrune(registry.Context(), networkPruneOptions) if err != nil { setExitCode(err) |