2014-02-12 06:46:55 +04:00
|
|
|
package opts
|
2013-11-26 21:46:06 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-13 16:02:12 +04:00
|
|
|
"net"
|
2013-11-22 21:55:27 +04:00
|
|
|
"regexp"
|
2013-11-26 21:46:06 +04:00
|
|
|
"strings"
|
2016-06-14 05:52:49 +03:00
|
|
|
|
2016-09-06 21:18:12 +03:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2013-11-26 21:46:06 +04:00
|
|
|
)
|
|
|
|
|
2014-09-16 07:30:10 +04:00
|
|
|
var (
|
2015-07-12 11:33:30 +03:00
|
|
|
alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
|
|
|
|
domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
|
2014-09-16 07:30:10 +04:00
|
|
|
)
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// ListOpts holds a list of values and a validation function.
|
2013-11-26 21:46:06 +04:00
|
|
|
type ListOpts struct {
|
2014-08-10 05:13:44 +04:00
|
|
|
values *[]string
|
2013-11-26 21:46:06 +04:00
|
|
|
validator ValidatorFctType
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// NewListOpts creates a new ListOpts with the specified validator.
|
2013-11-26 21:46:06 +04:00
|
|
|
func NewListOpts(validator ValidatorFctType) ListOpts {
|
2014-08-10 05:13:44 +04:00
|
|
|
var values []string
|
2015-05-05 07:18:28 +03:00
|
|
|
return *NewListOptsRef(&values, validator)
|
2014-08-10 05:13:44 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// NewListOptsRef creates a new ListOpts with the specified values and validator.
|
2015-05-05 07:18:28 +03:00
|
|
|
func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
|
2014-08-10 05:13:44 +04:00
|
|
|
return &ListOpts{
|
|
|
|
values: values,
|
2013-11-26 21:46:06 +04:00
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *ListOpts) String() string {
|
2014-08-10 05:13:44 +04:00
|
|
|
return fmt.Sprintf("%v", []string((*opts.values)))
|
2013-11-26 21:46:06 +04:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:57:55 +03:00
|
|
|
// Set validates if needed the input value and adds it to the
|
2013-11-26 21:46:06 +04:00
|
|
|
// internal slice.
|
|
|
|
func (opts *ListOpts) Set(value string) error {
|
|
|
|
if opts.validator != nil {
|
|
|
|
v, err := opts.validator(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value = v
|
|
|
|
}
|
2014-08-10 05:13:44 +04:00
|
|
|
(*opts.values) = append((*opts.values), value)
|
2013-11-26 21:46:06 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// Delete removes the specified element from the slice.
|
2013-11-26 21:46:06 +04:00
|
|
|
func (opts *ListOpts) Delete(key string) {
|
2014-08-10 05:13:44 +04:00
|
|
|
for i, k := range *opts.values {
|
2013-11-26 21:46:06 +04:00
|
|
|
if k == key {
|
2014-08-10 05:13:44 +04:00
|
|
|
(*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
|
2013-11-26 21:46:06 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMap returns the content of values in a map in order to avoid
|
|
|
|
// duplicates.
|
|
|
|
func (opts *ListOpts) GetMap() map[string]struct{} {
|
|
|
|
ret := make(map[string]struct{})
|
2014-08-10 05:13:44 +04:00
|
|
|
for _, k := range *opts.values {
|
2013-11-26 21:46:06 +04:00
|
|
|
ret[k] = struct{}{}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// GetAll returns the values of slice.
|
2013-11-26 21:46:06 +04:00
|
|
|
func (opts *ListOpts) GetAll() []string {
|
2014-08-10 05:13:44 +04:00
|
|
|
return (*opts.values)
|
2013-11-26 21:46:06 +04:00
|
|
|
}
|
|
|
|
|
2015-11-07 01:22:48 +03:00
|
|
|
// GetAllOrEmpty returns the values of the slice
|
|
|
|
// or an empty slice when there are no values.
|
|
|
|
func (opts *ListOpts) GetAllOrEmpty() []string {
|
|
|
|
v := *opts.values
|
|
|
|
if v == nil {
|
|
|
|
return make([]string, 0)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// Get checks the existence of the specified key.
|
2013-11-26 21:46:06 +04:00
|
|
|
func (opts *ListOpts) Get(key string) bool {
|
2014-08-10 05:13:44 +04:00
|
|
|
for _, k := range *opts.values {
|
2013-11-26 21:46:06 +04:00
|
|
|
if k == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the amount of element in the slice.
|
|
|
|
func (opts *ListOpts) Len() int {
|
2014-08-10 05:13:44 +04:00
|
|
|
return len((*opts.values))
|
2013-11-26 21:46:06 +04:00
|
|
|
}
|
|
|
|
|
2016-05-17 00:20:29 +03:00
|
|
|
// Type returns a string name for this Option type
|
|
|
|
func (opts *ListOpts) Type() string {
|
|
|
|
return "list"
|
|
|
|
}
|
|
|
|
|
2015-12-11 02:35:10 +03:00
|
|
|
// NamedOption is an interface that list and map options
|
|
|
|
// with names implement.
|
|
|
|
type NamedOption interface {
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NamedListOpts is a ListOpts with a configuration name.
|
|
|
|
// This struct is useful to keep reference to the assigned
|
|
|
|
// field name in the internal configuration struct.
|
|
|
|
type NamedListOpts struct {
|
|
|
|
name string
|
|
|
|
ListOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ NamedOption = &NamedListOpts{}
|
|
|
|
|
|
|
|
// NewNamedListOptsRef creates a reference to a new NamedListOpts struct.
|
|
|
|
func NewNamedListOptsRef(name string, values *[]string, validator ValidatorFctType) *NamedListOpts {
|
|
|
|
return &NamedListOpts{
|
|
|
|
name: name,
|
|
|
|
ListOpts: *NewListOptsRef(values, validator),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the NamedListOpts in the configuration.
|
|
|
|
func (o *NamedListOpts) Name() string {
|
|
|
|
return o.name
|
|
|
|
}
|
|
|
|
|
2016-08-29 13:37:14 +03:00
|
|
|
// MapOpts holds a map of values and a validation function.
|
2015-05-05 00:39:48 +03:00
|
|
|
type MapOpts struct {
|
|
|
|
values map[string]string
|
|
|
|
validator ValidatorFctType
|
|
|
|
}
|
|
|
|
|
2015-07-12 11:33:30 +03:00
|
|
|
// Set validates if needed the input value and add it to the
|
|
|
|
// internal map, by splitting on '='.
|
2015-05-05 00:39:48 +03:00
|
|
|
func (opts *MapOpts) Set(value string) error {
|
|
|
|
if opts.validator != nil {
|
|
|
|
v, err := opts.validator(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value = v
|
|
|
|
}
|
|
|
|
vals := strings.SplitN(value, "=", 2)
|
|
|
|
if len(vals) == 1 {
|
|
|
|
(opts.values)[vals[0]] = ""
|
|
|
|
} else {
|
|
|
|
(opts.values)[vals[0]] = vals[1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// GetAll returns the values of MapOpts as a map.
|
2015-06-12 16:25:32 +03:00
|
|
|
func (opts *MapOpts) GetAll() map[string]string {
|
|
|
|
return opts.values
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:39:48 +03:00
|
|
|
func (opts *MapOpts) String() string {
|
|
|
|
return fmt.Sprintf("%v", map[string]string((opts.values)))
|
|
|
|
}
|
|
|
|
|
2016-04-19 19:59:48 +03:00
|
|
|
// Type returns a string name for this Option type
|
|
|
|
func (opts *MapOpts) Type() string {
|
|
|
|
return "map"
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// NewMapOpts creates a new MapOpts with the specified map of values and a validator.
|
2015-05-05 07:18:28 +03:00
|
|
|
func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
|
|
|
|
if values == nil {
|
|
|
|
values = make(map[string]string)
|
|
|
|
}
|
2015-05-05 00:39:48 +03:00
|
|
|
return &MapOpts{
|
|
|
|
values: values,
|
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 02:35:10 +03:00
|
|
|
// NamedMapOpts is a MapOpts struct with a configuration name.
|
|
|
|
// This struct is useful to keep reference to the assigned
|
|
|
|
// field name in the internal configuration struct.
|
|
|
|
type NamedMapOpts struct {
|
|
|
|
name string
|
|
|
|
MapOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ NamedOption = &NamedMapOpts{}
|
|
|
|
|
|
|
|
// NewNamedMapOpts creates a reference to a new NamedMapOpts struct.
|
|
|
|
func NewNamedMapOpts(name string, values map[string]string, validator ValidatorFctType) *NamedMapOpts {
|
|
|
|
return &NamedMapOpts{
|
|
|
|
name: name,
|
|
|
|
MapOpts: *NewMapOpts(values, validator),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the NamedMapOpts in the configuration.
|
|
|
|
func (o *NamedMapOpts) Name() string {
|
|
|
|
return o.name
|
|
|
|
}
|
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// ValidatorFctType defines a validator function that returns a validated string and/or an error.
|
2013-11-26 21:46:06 +04:00
|
|
|
type ValidatorFctType func(val string) (string, error)
|
2015-07-12 11:33:30 +03:00
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
|
2014-10-07 05:54:52 +04:00
|
|
|
type ValidatorFctListType func(val string) ([]string, error)
|
2013-11-26 21:46:06 +04:00
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// ValidateIPAddress validates an Ip address.
|
2014-07-10 01:47:55 +04:00
|
|
|
func ValidateIPAddress(val string) (string, error) {
|
2014-06-13 16:02:12 +04:00
|
|
|
var ip = net.ParseIP(strings.TrimSpace(val))
|
|
|
|
if ip != nil {
|
|
|
|
return ip.String(), nil
|
2013-11-22 21:55:27 +04:00
|
|
|
}
|
2014-06-13 16:02:12 +04:00
|
|
|
return "", fmt.Errorf("%s is not an ip address", val)
|
2013-11-22 21:55:27 +04:00
|
|
|
}
|
2014-02-07 20:48:14 +04:00
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// ValidateDNSSearch validates domain for resolvconf search configuration.
|
|
|
|
// A zero length domain is represented by a dot (.).
|
2015-07-12 11:33:30 +03:00
|
|
|
func ValidateDNSSearch(val string) (string, error) {
|
2014-06-26 15:03:23 +04:00
|
|
|
if val = strings.Trim(val, " "); val == "." {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
return validateDomain(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateDomain(val string) (string, error) {
|
2014-09-16 07:30:10 +04:00
|
|
|
if alphaRegexp.FindString(val) == "" {
|
2014-02-07 20:48:14 +04:00
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|
2014-09-16 07:30:10 +04:00
|
|
|
ns := domainRegexp.FindSubmatch([]byte(val))
|
2014-11-25 14:45:20 +03:00
|
|
|
if len(ns) > 0 && len(ns[1]) < 255 {
|
2014-02-07 20:48:14 +04:00
|
|
|
return string(ns[1]), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|
2014-07-18 22:48:19 +04:00
|
|
|
|
2015-08-27 10:33:21 +03:00
|
|
|
// ValidateLabel validates that the specified string is a valid label, and returns it.
|
|
|
|
// Labels are in the form on key=value.
|
2014-11-20 21:36:05 +03:00
|
|
|
func ValidateLabel(val string) (string, error) {
|
2015-07-12 11:33:30 +03:00
|
|
|
if strings.Count(val, "=") < 1 {
|
2014-11-20 21:36:05 +03:00
|
|
|
return "", fmt.Errorf("bad attribute format: %s", val)
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
2016-03-29 15:24:28 +03:00
|
|
|
|
2016-05-08 04:36:10 +03:00
|
|
|
// ValidateSysctl validates a sysctl and returns it.
|
2016-03-29 15:24:28 +03:00
|
|
|
func ValidateSysctl(val string) (string, error) {
|
|
|
|
validSysctlMap := map[string]bool{
|
|
|
|
"kernel.msgmax": true,
|
|
|
|
"kernel.msgmnb": true,
|
|
|
|
"kernel.msgmni": true,
|
|
|
|
"kernel.sem": true,
|
|
|
|
"kernel.shmall": true,
|
|
|
|
"kernel.shmmax": true,
|
|
|
|
"kernel.shmmni": true,
|
|
|
|
"kernel.shm_rmid_forced": true,
|
|
|
|
}
|
|
|
|
validSysctlPrefixes := []string{
|
|
|
|
"net.",
|
|
|
|
"fs.mqueue.",
|
|
|
|
}
|
|
|
|
arr := strings.Split(val, "=")
|
|
|
|
if len(arr) < 2 {
|
|
|
|
return "", fmt.Errorf("sysctl '%s' is not whitelisted", val)
|
|
|
|
}
|
|
|
|
if validSysctlMap[arr[0]] {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vp := range validSysctlPrefixes {
|
|
|
|
if strings.HasPrefix(arr[0], vp) {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("sysctl '%s' is not whitelisted", val)
|
|
|
|
}
|
2016-06-14 05:52:49 +03:00
|
|
|
|
|
|
|
// FilterOpt is a flag type for validating filters
|
|
|
|
type FilterOpt struct {
|
|
|
|
filter filters.Args
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFilterOpt returns a new FilterOpt
|
|
|
|
func NewFilterOpt() FilterOpt {
|
|
|
|
return FilterOpt{filter: filters.NewArgs()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *FilterOpt) String() string {
|
|
|
|
repr, err := filters.ToParam(o.filter)
|
|
|
|
if err != nil {
|
|
|
|
return "invalid filters"
|
|
|
|
}
|
|
|
|
return repr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the value of the opt by parsing the command line value
|
|
|
|
func (o *FilterOpt) Set(value string) error {
|
|
|
|
var err error
|
|
|
|
o.filter, err = filters.ParseFlag(value, o.filter)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the option type
|
|
|
|
func (o *FilterOpt) Type() string {
|
|
|
|
return "filter"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value of this option
|
|
|
|
func (o *FilterOpt) Value() filters.Args {
|
|
|
|
return o.filter
|
|
|
|
}
|