gopkg.in/docker/docker.v1@v1.13.1/plugin/v2/settable.go (about)

     1  package v2
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  type settable struct {
    10  	name  string
    11  	field string
    12  	value string
    13  }
    14  
    15  var (
    16  	allowedSettableFieldsEnv     = []string{"value"}
    17  	allowedSettableFieldsArgs    = []string{"value"}
    18  	allowedSettableFieldsDevices = []string{"path"}
    19  	allowedSettableFieldsMounts  = []string{"source"}
    20  
    21  	errMultipleFields = errors.New("multiple fields are settable, one must be specified")
    22  	errInvalidFormat  = errors.New("invalid format, must be <name>[.<field>][=<value>]")
    23  )
    24  
    25  func newSettables(args []string) ([]settable, error) {
    26  	sets := make([]settable, 0, len(args))
    27  	for _, arg := range args {
    28  		set, err := newSettable(arg)
    29  		if err != nil {
    30  			return nil, err
    31  		}
    32  		sets = append(sets, set)
    33  	}
    34  	return sets, nil
    35  }
    36  
    37  func newSettable(arg string) (settable, error) {
    38  	var set settable
    39  	if i := strings.Index(arg, "="); i == 0 {
    40  		return set, errInvalidFormat
    41  	} else if i < 0 {
    42  		set.name = arg
    43  	} else {
    44  		set.name = arg[:i]
    45  		set.value = arg[i+1:]
    46  	}
    47  
    48  	if i := strings.LastIndex(set.name, "."); i > 0 {
    49  		set.field = set.name[i+1:]
    50  		set.name = arg[:i]
    51  	}
    52  
    53  	return set, nil
    54  }
    55  
    56  // prettyName return name.field if there is a field, otherwise name.
    57  func (set *settable) prettyName() string {
    58  	if set.field != "" {
    59  		return fmt.Sprintf("%s.%s", set.name, set.field)
    60  	}
    61  	return set.name
    62  }
    63  
    64  func (set *settable) isSettable(allowedSettableFields []string, settable []string) (bool, error) {
    65  	if set.field == "" {
    66  		if len(settable) == 1 {
    67  			// if field is not specified and there only one settable, default to it.
    68  			set.field = settable[0]
    69  		} else if len(settable) > 1 {
    70  			return false, errMultipleFields
    71  		}
    72  	}
    73  
    74  	isAllowed := false
    75  	for _, allowedSettableField := range allowedSettableFields {
    76  		if set.field == allowedSettableField {
    77  			isAllowed = true
    78  			break
    79  		}
    80  	}
    81  
    82  	if isAllowed {
    83  		for _, settableField := range settable {
    84  			if set.field == settableField {
    85  				return true, nil
    86  			}
    87  		}
    88  	}
    89  
    90  	return false, nil
    91  }
    92  
    93  func updateSettingsEnv(env *[]string, set *settable) {
    94  	for i, e := range *env {
    95  		if parts := strings.SplitN(e, "=", 2); parts[0] == set.name {
    96  			(*env)[i] = fmt.Sprintf("%s=%s", set.name, set.value)
    97  			return
    98  		}
    99  	}
   100  
   101  	*env = append(*env, fmt.Sprintf("%s=%s", set.name, set.value))
   102  }