@@ -33,50 +33,10 @@ func (m *MountOpt) Set(value string) error {
3333 return err
3434 }
3535
36- mount := mounttypes.Mount {}
37-
38- volumeOptions := func () * mounttypes.VolumeOptions {
39- if mount .VolumeOptions == nil {
40- mount .VolumeOptions = & mounttypes.VolumeOptions {
41- Labels : make (map [string ]string ),
42- }
43- }
44- if mount .VolumeOptions .DriverConfig == nil {
45- mount .VolumeOptions .DriverConfig = & mounttypes.Driver {}
46- }
47- return mount .VolumeOptions
48- }
49-
50- imageOptions := func () * mounttypes.ImageOptions {
51- if mount .ImageOptions == nil {
52- mount .ImageOptions = new (mounttypes.ImageOptions )
53- }
54- return mount .ImageOptions
55- }
56-
57- bindOptions := func () * mounttypes.BindOptions {
58- if mount .BindOptions == nil {
59- mount .BindOptions = new (mounttypes.BindOptions )
60- }
61- return mount .BindOptions
36+ mount := mounttypes.Mount {
37+ Type : mounttypes .TypeVolume , // default to volume mounts
6238 }
6339
64- tmpfsOptions := func () * mounttypes.TmpfsOptions {
65- if mount .TmpfsOptions == nil {
66- mount .TmpfsOptions = new (mounttypes.TmpfsOptions )
67- }
68- return mount .TmpfsOptions
69- }
70-
71- setValueOnMap := func (target map [string ]string , value string ) {
72- k , v , _ := strings .Cut (value , "=" )
73- if k != "" {
74- target [k ] = v
75- }
76- }
77-
78- mount .Type = mounttypes .TypeVolume // default to volume mounts
79-
8040 for _ , field := range fields {
8141 key , val , hasValue := strings .Cut (field , "=" )
8242 if k := strings .TrimSpace (key ); k != key {
@@ -124,54 +84,53 @@ func (m *MountOpt) Set(value string) error {
12484 case "consistency" :
12585 mount .Consistency = mounttypes .Consistency (strings .ToLower (val ))
12686 case "bind-propagation" :
127- bindOptions ( ).Propagation = mounttypes .Propagation (strings .ToLower (val ))
87+ ensureBindOptions ( & mount ).Propagation = mounttypes .Propagation (strings .ToLower (val ))
12888 case "bind-nonrecursive" :
12989 return errors .New ("bind-nonrecursive is deprecated, use bind-recursive=disabled instead" )
13090 case "bind-recursive" :
13191 switch val {
13292 case "enabled" : // read-only mounts are recursively read-only if Engine >= v25 && kernel >= v5.12, otherwise writable
13393 // NOP
13494 case "disabled" : // previously "bind-nonrecursive=true"
135- bindOptions ( ).NonRecursive = true
95+ ensureBindOptions ( & mount ).NonRecursive = true
13696 case "writable" : // conforms to the default read-only bind-mount of Docker v24; read-only mounts are recursively mounted but not recursively read-only
137- bindOptions ( ).ReadOnlyNonRecursive = true
97+ ensureBindOptions ( & mount ).ReadOnlyNonRecursive = true
13898 case "readonly" : // force recursively read-only, or raise an error
139- bindOptions ( ).ReadOnlyForceRecursive = true
99+ ensureBindOptions ( & mount ).ReadOnlyForceRecursive = true
140100 // TODO: implicitly set propagation and error if the user specifies a propagation in a future refactor/UX polish pass
141101 // https://github.com/docker/cli/pull/4316#discussion_r1341974730
142102 default :
143103 return fmt .Errorf (`invalid value for %s: %s (must be "enabled", "disabled", "writable", or "readonly")` , key , val )
144104 }
145105 case "volume-subpath" :
146- volumeOptions ( ).Subpath = val
106+ ensureVolumeOptions ( & mount ).Subpath = val
147107 case "volume-nocopy" :
148- volumeOptions ( ).NoCopy , err = parseBoolValue (key , val , hasValue )
108+ ensureVolumeOptions ( & mount ).NoCopy , err = parseBoolValue (key , val , hasValue )
149109 if err != nil {
150110 return err
151111 }
152112 case "volume-label" :
153- setValueOnMap (volumeOptions ().Labels , val )
113+ volumeOpts := ensureVolumeOptions (& mount )
114+ volumeOpts .Labels = setValueOnMap (volumeOpts .Labels , val )
154115 case "volume-driver" :
155- volumeOptions (). DriverConfig .Name = val
116+ ensureVolumeDriver ( & mount ) .Name = val
156117 case "volume-opt" :
157- if volumeOptions ().DriverConfig .Options == nil {
158- volumeOptions ().DriverConfig .Options = make (map [string ]string )
159- }
160- setValueOnMap (volumeOptions ().DriverConfig .Options , val )
118+ volumeDriver := ensureVolumeDriver (& mount )
119+ volumeDriver .Options = setValueOnMap (volumeDriver .Options , val )
161120 case "image-subpath" :
162- imageOptions ( ).Subpath = val
121+ ensureImageOptions ( & mount ).Subpath = val
163122 case "tmpfs-size" :
164123 sizeBytes , err := units .RAMInBytes (val )
165124 if err != nil {
166125 return fmt .Errorf ("invalid value for %s: %s" , key , val )
167126 }
168- tmpfsOptions ( ).SizeBytes = sizeBytes
127+ ensureTmpfsOptions ( & mount ).SizeBytes = sizeBytes
169128 case "tmpfs-mode" :
170129 ui64 , err := strconv .ParseUint (val , 8 , 32 )
171130 if err != nil {
172131 return fmt .Errorf ("invalid value for %s: %s" , key , val )
173132 }
174- tmpfsOptions ( ).Mode = os .FileMode (ui64 )
133+ ensureTmpfsOptions ( & mount ).Mode = os .FileMode (ui64 )
175134 default :
176135 return fmt .Errorf ("unknown option '%s' in '%s'" , key , field )
177136 }
0 commit comments