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
+53
View File
@@ -0,0 +1,53 @@
package filters
import (
"errors"
"fmt"
"io"
)
var DefaultFilterProcs = FilterProcSet{}
func Register(name string, fp FilterProc) error {
return DefaultFilterProcs.Register(name, fp)
}
var ErrorFilterExists = errors.New("filter already exists and ")
var ErrorFilterExistsConflict = errors.New("filter already exists and FilterProc are different")
type FilterProcSet map[string]FilterProc
func (fs FilterProcSet) Process(context string) {
}
func (fs FilterProcSet) Register(name string, fp FilterProc) error {
if v, ok := fs[name]; ok {
if v == fp {
return ErrorFilterExists
} else {
return ErrorFilterExistsConflict
}
}
fs[name] = fp
return nil
}
type FilterProc interface {
Process(context, key, value string, output io.Writer) error
}
type UnknownFilterProc struct{}
func (ufp UnknownFilterProc) Process(context, key, value string, output io.Writer) error {
if output != nil {
fmt.Fprintf(output, "do not know how to process [%s : %s]", key, value)
}
return nil
}
type Filter interface {
Scope() string
Target() string
Expressions() []string
Match(interface{}) bool
}
+47
View File
@@ -0,0 +1,47 @@
package filters
import (
"errors"
"strings"
)
/*
Parse the argument to the filter flag. Like
`docker ps -f 'created=today;image.name=ubuntu*'`
Filters delimited by ';', and expected to be 'name=value'
If prev map is provided, then it is appended to, and returned. By default a new
map is created.
*/
func ParseFlag(arg string, prev map[string]string) (map[string]string, error) {
var filters map[string]string
if prev != nil {
filters = prev
} else {
filters = map[string]string{}
}
if len(arg) == 0 {
return filters, nil
}
for _, chunk := range strings.Split(arg, ";") {
if !strings.Contains(chunk, "=") {
return filters, ErrorBadFormat
}
f := strings.SplitN(chunk, "=", 2)
filters[f[0]] = f[1]
}
return filters, nil
}
var ErrorBadFormat = errors.New("bad format of filter (expected name=value)")
func ToParam(f map[string]string) string {
fs := []string{}
for k, v := range f {
fs = append(fs, k+"="+v)
}
return strings.Join(fs, ";")
}