filters, for images: start with untagged/tagged boolean

This is a new feature and flag. (replaces the suggestion of a flag for
--untagged images).
The concept is to have a syntax to filter. This begins with this
filtering for the 'images' subcommand, and at that only filtering for
whether images are untagged.
  example like: docker rmi $(docker images -q --filter 'untagged=true')

Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)
This commit is contained in:
Vincent Batts
2014-06-02 16:33:50 -04:00
parent afb2d5de3d
commit 5f3812ec97
6 changed files with 1678 additions and 20 deletions
+46 -8
View File
@@ -26,12 +26,14 @@ import (
"github.com/dotcloud/docker/dockerversion"
"github.com/dotcloud/docker/engine"
"github.com/dotcloud/docker/nat"
"github.com/dotcloud/docker/opts"
"github.com/dotcloud/docker/pkg/signal"
"github.com/dotcloud/docker/pkg/term"
"github.com/dotcloud/docker/pkg/units"
"github.com/dotcloud/docker/registry"
"github.com/dotcloud/docker/runconfig"
"github.com/dotcloud/docker/utils"
"github.com/dotcloud/docker/utils/filters"
)
func (cli *DockerCli) CmdHelp(args ...string) error {
@@ -1145,6 +1147,9 @@ func (cli *DockerCli) CmdImages(args ...string) error {
flViz := cmd.Bool([]string{"#v", "#viz", "#-viz"}, false, "Output graph in graphviz format")
flTree := cmd.Bool([]string{"#t", "#tree", "#-tree"}, false, "Output graph in tree format")
var flFilter opts.ListOpts
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'tagged=false')")
if err := cmd.Parse(args); err != nil {
return nil
}
@@ -1153,11 +1158,41 @@ func (cli *DockerCli) CmdImages(args ...string) error {
return nil
}
filter := cmd.Arg(0)
// Consolidate all filter flags, and sanity check them early.
// They'll get process in the daemon/server.
imageFilters := map[string]string{}
for _, f := range flFilter.GetAll() {
var err error
imageFilters, err = filters.ParseFlag(f, imageFilters)
if err != nil {
return err
}
}
/*
var (
untagged bool
)
for k,v := range imageFilters {
}
*/
// seeing -all untagged images is redundant, and no point in seeing a visualization of that
/*
if *flUntagged && (*all || *flViz || *flTree) {
fmt.Fprintln(cli.err, "Notice: --untagged is not to be used with --all, --tree or --viz")
*flUntagged = false
}
*/
matchName := cmd.Arg(0)
// FIXME: --viz and --tree are deprecated. Remove them in a future version.
if *flViz || *flTree {
body, _, err := readBody(cli.call("GET", "/images/json?all=1", nil, false))
v := url.Values{
"all": []string{"1"},
"filters": []string{filters.ToParam(imageFilters)},
}
body, _, err := readBody(cli.call("GET", "/images/json?"+v.Encode(), nil, false))
if err != nil {
return err
}
@@ -1187,13 +1222,13 @@ func (cli *DockerCli) CmdImages(args ...string) error {
}
}
if filter != "" {
if filter == image.Get("Id") || filter == utils.TruncateID(image.Get("Id")) {
if matchName != "" {
if matchName == image.Get("Id") || matchName == utils.TruncateID(image.Get("Id")) {
startImage = image
}
for _, repotag := range image.GetList("RepoTags") {
if repotag == filter {
if repotag == matchName {
startImage = image
}
}
@@ -1211,16 +1246,19 @@ func (cli *DockerCli) CmdImages(args ...string) error {
root := engine.NewTable("Created", 1)
root.Add(startImage)
cli.WalkTree(*noTrunc, root, byParent, "", printNode)
} else if filter == "" {
} else if matchName == "" {
cli.WalkTree(*noTrunc, roots, byParent, "", printNode)
}
if *flViz {
fmt.Fprintf(cli.out, " base [style=invisible]\n}\n")
}
} else {
v := url.Values{}
v := url.Values{
"filters": []string{filters.ToParam(imageFilters)},
}
if cmd.NArg() == 1 {
v.Set("filter", filter)
// FIXME rename this parameter, to not be confused with the filters flag
v.Set("filter", matchName)
}
if *all {
v.Set("all", "1")