2015-07-25 08:35:07 +00:00
|
|
|
// Package filters provides helper function to parse and handle command line
|
|
|
|
// filter, used for example in docker ps or docker images commands.
|
2014-07-29 00:23:38 +00:00
|
|
|
package filters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-11-26 01:27:11 +00:00
|
|
|
"fmt"
|
2014-10-13 06:12:44 +00:00
|
|
|
"regexp"
|
2014-07-29 00:23:38 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// Args stores filter arguments as map key:{array of values}.
|
|
|
|
// It contains a aggregation of the list of arguments (which are in the form
|
|
|
|
// of -f 'key=value') based on the key, and store values for the same key
|
|
|
|
// in an slice.
|
|
|
|
// e.g given -f 'label=label1=1' -f 'label=label2=2' -f 'image.name=ubuntu'
|
|
|
|
// the args will be {'label': {'label1=1','label2=2'}, 'image.name', {'ubuntu'}}
|
2015-11-26 01:27:11 +00:00
|
|
|
type Args struct {
|
|
|
|
fields map[string]map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewArgs initializes a new Args struct.
|
|
|
|
func NewArgs() Args {
|
|
|
|
return Args{fields: map[string]map[string]bool{}}
|
|
|
|
}
|
2014-07-29 00:23:38 +00:00
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// ParseFlag parses the argument to the filter flag. Like
|
2014-07-29 00:23:38 +00:00
|
|
|
//
|
|
|
|
// `docker ps -f 'created=today' -f 'image.name=ubuntu*'`
|
|
|
|
//
|
|
|
|
// If prev map is provided, then it is appended to, and returned. By default a new
|
|
|
|
// map is created.
|
|
|
|
func ParseFlag(arg string, prev Args) (Args, error) {
|
2015-07-25 08:35:07 +00:00
|
|
|
filters := prev
|
2014-07-29 00:23:38 +00:00
|
|
|
if len(arg) == 0 {
|
|
|
|
return filters, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(arg, "=") {
|
2015-07-25 08:35:07 +00:00
|
|
|
return filters, ErrBadFormat
|
2014-07-29 00:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f := strings.SplitN(arg, "=", 2)
|
2015-11-26 01:27:11 +00:00
|
|
|
|
2014-11-03 11:11:29 +00:00
|
|
|
name := strings.ToLower(strings.TrimSpace(f[0]))
|
|
|
|
value := strings.TrimSpace(f[1])
|
2015-11-26 01:27:11 +00:00
|
|
|
|
|
|
|
filters.Add(name, value)
|
2014-07-29 00:23:38 +00:00
|
|
|
|
|
|
|
return filters, nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// ErrBadFormat is an error returned in case of bad format for a filter.
|
|
|
|
var ErrBadFormat = errors.New("bad format of filter (expected name=value)")
|
2014-07-29 00:23:38 +00:00
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// ToParam packs the Args into an string for easy transport from client to server.
|
2014-07-29 00:23:38 +00:00
|
|
|
func ToParam(a Args) (string, error) {
|
|
|
|
// this way we don't URL encode {}, just empty space
|
2015-11-26 01:27:11 +00:00
|
|
|
if a.Len() == 0 {
|
2014-07-29 00:23:38 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2015-11-26 01:27:11 +00:00
|
|
|
buf, err := json.Marshal(a.fields)
|
2014-07-29 00:23:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// FromParam unpacks the filter Args.
|
2014-07-29 00:23:38 +00:00
|
|
|
func FromParam(p string) (Args, error) {
|
|
|
|
if len(p) == 0 {
|
2015-11-26 01:27:11 +00:00
|
|
|
return NewArgs(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := strings.NewReader(p)
|
|
|
|
d := json.NewDecoder(r)
|
|
|
|
|
|
|
|
m := map[string]map[string]bool{}
|
|
|
|
if err := d.Decode(&m); err != nil {
|
|
|
|
r.Seek(0, 0)
|
|
|
|
|
|
|
|
// Allow parsing old arguments in slice format.
|
|
|
|
// Because other libraries might be sending them in this format.
|
|
|
|
deprecated := map[string][]string{}
|
|
|
|
if deprecatedErr := d.Decode(&deprecated); deprecatedErr == nil {
|
|
|
|
m = deprecatedArgs(deprecated)
|
|
|
|
} else {
|
|
|
|
return NewArgs(), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Args{m}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the list of values associates with a field.
|
|
|
|
// It returns a slice of strings to keep backwards compatibility with old code.
|
|
|
|
func (filters Args) Get(field string) []string {
|
|
|
|
values := filters.fields[field]
|
|
|
|
if values == nil {
|
|
|
|
return make([]string, 0)
|
2014-07-29 00:23:38 +00:00
|
|
|
}
|
2015-11-26 01:27:11 +00:00
|
|
|
slice := make([]string, 0, len(values))
|
|
|
|
for key := range values {
|
|
|
|
slice = append(slice, key)
|
2014-07-29 00:23:38 +00:00
|
|
|
}
|
2015-11-26 01:27:11 +00:00
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a new value to a filter field.
|
|
|
|
func (filters Args) Add(name, value string) {
|
|
|
|
if _, ok := filters.fields[name]; ok {
|
|
|
|
filters.fields[name][value] = true
|
|
|
|
} else {
|
|
|
|
filters.fields[name] = map[string]bool{value: true}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Del removes a value from a filter field.
|
|
|
|
func (filters Args) Del(name, value string) {
|
|
|
|
if _, ok := filters.fields[name]; ok {
|
|
|
|
delete(filters.fields[name], value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of fields in the arguments.
|
|
|
|
func (filters Args) Len() int {
|
|
|
|
return len(filters.fields)
|
2014-07-29 00:23:38 +00:00
|
|
|
}
|
2014-10-13 06:12:44 +00:00
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// MatchKVList returns true if the values for the specified field maches the ones
|
|
|
|
// from the sources.
|
|
|
|
// e.g. given Args are {'label': {'label1=1','label2=1'}, 'image.name', {'ubuntu'}},
|
2015-11-26 01:27:11 +00:00
|
|
|
// field is 'label' and sources are {'label1': '1', 'label2': '2'}
|
2015-07-25 08:35:07 +00:00
|
|
|
// it returns true.
|
2015-01-07 00:04:10 +00:00
|
|
|
func (filters Args) MatchKVList(field string, sources map[string]string) bool {
|
2015-11-26 01:27:11 +00:00
|
|
|
fieldValues := filters.fields[field]
|
2015-01-07 00:04:10 +00:00
|
|
|
|
|
|
|
//do not filter if there is no filter set or cannot determine filter
|
|
|
|
if len(fieldValues) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if sources == nil || len(sources) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-26 01:27:11 +00:00
|
|
|
for name2match := range fieldValues {
|
2015-01-07 00:04:10 +00:00
|
|
|
testKV := strings.SplitN(name2match, "=", 2)
|
|
|
|
|
2015-11-26 01:27:11 +00:00
|
|
|
v, ok := sources[testKV[0]]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(testKV) == 2 && testKV[1] != v {
|
|
|
|
return false
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// Match returns true if the values for the specified field matches the source string
|
|
|
|
// e.g. given Args are {'label': {'label1=1','label2=1'}, 'image.name', {'ubuntu'}},
|
|
|
|
// field is 'image.name' and source is 'ubuntu'
|
|
|
|
// it returns true.
|
2014-10-13 06:12:44 +00:00
|
|
|
func (filters Args) Match(field, source string) bool {
|
2015-11-26 01:27:11 +00:00
|
|
|
if filters.ExactMatch(field, source) {
|
2014-10-13 06:12:44 +00:00
|
|
|
return true
|
|
|
|
}
|
2015-11-26 01:27:11 +00:00
|
|
|
|
|
|
|
fieldValues := filters.fields[field]
|
|
|
|
for name2match := range fieldValues {
|
2014-10-13 06:12:44 +00:00
|
|
|
match, err := regexp.MatchString(name2match, source)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if match {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-11-26 01:27:11 +00:00
|
|
|
|
|
|
|
// ExactMatch returns true if the source matches exactly one of the filters.
|
|
|
|
func (filters Args) ExactMatch(field, source string) bool {
|
|
|
|
fieldValues, ok := filters.fields[field]
|
|
|
|
//do not filter if there is no filter set or cannot determine filter
|
|
|
|
if !ok || len(fieldValues) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to march full name value to avoid O(N) regular expression matching
|
|
|
|
if fieldValues[source] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include returns true if the name of the field to filter is in the filters.
|
|
|
|
func (filters Args) Include(field string) bool {
|
|
|
|
_, ok := filters.fields[field]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate ensures that all the fields in the filter are valid.
|
|
|
|
// It returns an error as soon as it finds an invalid field.
|
|
|
|
func (filters Args) Validate(accepted map[string]bool) error {
|
|
|
|
for name := range filters.fields {
|
|
|
|
if !accepted[name] {
|
|
|
|
return fmt.Errorf("Invalid filter '%s'", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalkValues iterates over the list of filtered values for a field.
|
|
|
|
// It stops the iteration if it finds an error and it returns that error.
|
|
|
|
func (filters Args) WalkValues(field string, op func(value string) error) error {
|
|
|
|
if _, ok := filters.fields[field]; !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for v := range filters.fields[field] {
|
|
|
|
if err := op(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deprecatedArgs(d map[string][]string) map[string]map[string]bool {
|
|
|
|
m := map[string]map[string]bool{}
|
|
|
|
for k, v := range d {
|
|
|
|
values := map[string]bool{}
|
|
|
|
for _, vv := range v {
|
|
|
|
values[vv] = true
|
|
|
|
}
|
|
|
|
m[k] = values
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|