@@ -56,21 +56,21 @@ func (m *MountOpt) Set(value string) error {
5656 }
5757
5858 setValueOnMap := func (target map [string ]string , value string ) {
59- parts := strings .SplitN (value , "=" , 2 )
60- if len (parts ) == 1 {
61- target [value ] = ""
62- } else {
63- target [parts [0 ]] = parts [1 ]
59+ k , v , _ := strings .Cut (value , "=" )
60+ if k != "" {
61+ target [k ] = v
6462 }
6563 }
6664
6765 mount .Type = mounttypes .TypeVolume // default to volume mounts
6866 // Set writable as the default
6967 for _ , field := range fields {
70- parts := strings .SplitN (field , "=" , 2 )
71- key := strings .ToLower (parts [0 ])
68+ key , val , ok := strings .Cut (field , "=" )
7269
73- if len (parts ) == 1 {
70+ // TODO(thaJeztah): these options should not be case-insensitive.
71+ key = strings .ToLower (key )
72+
73+ if ! ok {
7474 switch key {
7575 case "readonly" , "ro" :
7676 mount .ReadOnly = true
@@ -81,64 +81,61 @@ func (m *MountOpt) Set(value string) error {
8181 case "bind-nonrecursive" :
8282 bindOptions ().NonRecursive = true
8383 continue
84+ default :
85+ return fmt .Errorf ("invalid field '%s' must be a key=value pair" , field )
8486 }
8587 }
8688
87- if len (parts ) != 2 {
88- return fmt .Errorf ("invalid field '%s' must be a key=value pair" , field )
89- }
90-
91- value := parts [1 ]
9289 switch key {
9390 case "type" :
94- mount .Type = mounttypes .Type (strings .ToLower (value ))
91+ mount .Type = mounttypes .Type (strings .ToLower (val ))
9592 case "source" , "src" :
96- mount .Source = value
97- if strings .HasPrefix (value , "." + string (filepath .Separator )) || value == "." {
98- if abs , err := filepath .Abs (value ); err == nil {
93+ mount .Source = val
94+ if strings .HasPrefix (val , "." + string (filepath .Separator )) || val == "." {
95+ if abs , err := filepath .Abs (val ); err == nil {
9996 mount .Source = abs
10097 }
10198 }
10299 case "target" , "dst" , "destination" :
103- mount .Target = value
100+ mount .Target = val
104101 case "readonly" , "ro" :
105- mount .ReadOnly , err = strconv .ParseBool (value )
102+ mount .ReadOnly , err = strconv .ParseBool (val )
106103 if err != nil {
107- return fmt .Errorf ("invalid value for %s: %s" , key , value )
104+ return fmt .Errorf ("invalid value for %s: %s" , key , val )
108105 }
109106 case "consistency" :
110- mount .Consistency = mounttypes .Consistency (strings .ToLower (value ))
107+ mount .Consistency = mounttypes .Consistency (strings .ToLower (val ))
111108 case "bind-propagation" :
112- bindOptions ().Propagation = mounttypes .Propagation (strings .ToLower (value ))
109+ bindOptions ().Propagation = mounttypes .Propagation (strings .ToLower (val ))
113110 case "bind-nonrecursive" :
114- bindOptions ().NonRecursive , err = strconv .ParseBool (value )
111+ bindOptions ().NonRecursive , err = strconv .ParseBool (val )
115112 if err != nil {
116- return fmt .Errorf ("invalid value for %s: %s" , key , value )
113+ return fmt .Errorf ("invalid value for %s: %s" , key , val )
117114 }
118115 case "volume-nocopy" :
119- volumeOptions ().NoCopy , err = strconv .ParseBool (value )
116+ volumeOptions ().NoCopy , err = strconv .ParseBool (val )
120117 if err != nil {
121- return fmt .Errorf ("invalid value for volume-nocopy: %s" , value )
118+ return fmt .Errorf ("invalid value for volume-nocopy: %s" , val )
122119 }
123120 case "volume-label" :
124- setValueOnMap (volumeOptions ().Labels , value )
121+ setValueOnMap (volumeOptions ().Labels , val )
125122 case "volume-driver" :
126- volumeOptions ().DriverConfig .Name = value
123+ volumeOptions ().DriverConfig .Name = val
127124 case "volume-opt" :
128125 if volumeOptions ().DriverConfig .Options == nil {
129126 volumeOptions ().DriverConfig .Options = make (map [string ]string )
130127 }
131- setValueOnMap (volumeOptions ().DriverConfig .Options , value )
128+ setValueOnMap (volumeOptions ().DriverConfig .Options , val )
132129 case "tmpfs-size" :
133- sizeBytes , err := units .RAMInBytes (value )
130+ sizeBytes , err := units .RAMInBytes (val )
134131 if err != nil {
135- return fmt .Errorf ("invalid value for %s: %s" , key , value )
132+ return fmt .Errorf ("invalid value for %s: %s" , key , val )
136133 }
137134 tmpfsOptions ().SizeBytes = sizeBytes
138135 case "tmpfs-mode" :
139- ui64 , err := strconv .ParseUint (value , 8 , 32 )
136+ ui64 , err := strconv .ParseUint (val , 8 , 32 )
140137 if err != nil {
141- return fmt .Errorf ("invalid value for %s: %s" , key , value )
138+ return fmt .Errorf ("invalid value for %s: %s" , key , val )
142139 }
143140 tmpfsOptions ().Mode = os .FileMode (ui64 )
144141 default :
0 commit comments