Lint on pkg/* packages

- pkg/useragent
- pkg/units
- pkg/ulimit
- pkg/truncindex
- pkg/timeoutconn
- pkg/term
- pkg/tarsum
- pkg/tailfile
- pkg/systemd
- pkg/stringutils
- pkg/stringid
- pkg/streamformatter
- pkg/sockets
- pkg/signal
- pkg/proxy
- pkg/progressreader
- pkg/pools
- pkg/plugins
- pkg/pidfile
- pkg/parsers
- pkg/parsers/filters
- pkg/parsers/kernel
- pkg/parsers/operatingsystem

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2015-07-25 10:35:07 +02:00
parent a922d62168
commit 9bcb3cba83
56 changed files with 455 additions and 195 deletions

View file

@ -1,3 +1,5 @@
// Package filters provides helper function to parse and handle command line
// filter, used for example in docker ps or docker images commands.
package filters
import (
@ -7,16 +9,22 @@ import (
"strings"
)
// 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'}}
type Args map[string][]string
// Parse the argument to the filter flag. Like
// ParseFlag parses the argument to the filter flag. Like
//
// `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) {
var filters Args = prev
filters := prev
if prev == nil {
filters = Args{}
}
@ -25,7 +33,7 @@ func ParseFlag(arg string, prev Args) (Args, error) {
}
if !strings.Contains(arg, "=") {
return filters, ErrorBadFormat
return filters, ErrBadFormat
}
f := strings.SplitN(arg, "=", 2)
@ -36,9 +44,10 @@ func ParseFlag(arg string, prev Args) (Args, error) {
return filters, nil
}
var ErrorBadFormat = errors.New("bad format of filter (expected name=value)")
// ErrBadFormat is an error returned in case of bad format for a filter.
var ErrBadFormat = errors.New("bad format of filter (expected name=value)")
// packs the Args into an string for easy transport from client to server
// ToParam packs the Args into an string for easy transport from client to server.
func ToParam(a Args) (string, error) {
// this way we don't URL encode {}, just empty space
if len(a) == 0 {
@ -52,7 +61,7 @@ func ToParam(a Args) (string, error) {
return string(buf), nil
}
// unpacks the filter Args
// FromParam unpacks the filter Args.
func FromParam(p string) (Args, error) {
args := Args{}
if len(p) == 0 {
@ -64,6 +73,11 @@ func FromParam(p string) (Args, error) {
return args, nil
}
// 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'}},
// field is 'label' and sources are {'label':{'label1=1','label2=2','label3=3'}}
// it returns true.
func (filters Args) MatchKVList(field string, sources map[string]string) bool {
fieldValues := filters[field]
@ -96,6 +110,10 @@ outer:
return true
}
// 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.
func (filters Args) Match(field, source string) bool {
fieldValues := filters[field]

View file

@ -39,8 +39,8 @@ func TestParseArgsEdgeCase(t *testing.T) {
if args == nil || len(args) != 0 {
t.Fatalf("Expected an empty Args (map), got %v", args)
}
if args, err = ParseFlag("anything", args); err == nil || err != ErrorBadFormat {
t.Fatalf("Expected ErrorBadFormat, got %v", err)
if args, err = ParseFlag("anything", args); err == nil || err != ErrBadFormat {
t.Fatalf("Expected ErrBadFormat, got %v", err)
}
}