Skip to content

Commit 3f80afa

Browse files
authored
Merge pull request #207 from mat007/fix-volume-trailing-slash
2 parents b20750b + feef6fc commit 3f80afa

File tree

8 files changed

+93
-57
lines changed

8 files changed

+93
-57
lines changed

compatibility/services.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ func (c *AllowList) CheckConfigs(service *types.ServiceConfig) {
169169
c.Unsupported("services.configs")
170170
return
171171
}
172-
for i, s := range service.Secrets {
173-
ref := types.FileReferenceConfig(s)
172+
for i, conf := range service.Configs {
173+
ref := types.FileReferenceConfig(conf)
174174
c.CheckFileReference("configs", &ref)
175-
service.Secrets[i] = s
175+
service.Configs[i] = conf
176176
}
177177
}
178178
}

loader/loader.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
145145
op(opts)
146146
}
147147

148-
configs := []*types.Config{}
148+
var configs []*types.Config
149149
for i, file := range configDetails.ConfigFiles {
150150
configDict := file.Config
151151
if configDict == nil {
@@ -222,14 +222,14 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
222222
}
223223

224224
func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
225-
yaml, err := ParseYAML(b)
225+
yml, err := ParseYAML(b)
226226
if err != nil {
227227
return nil, err
228228
}
229229
if !opts.SkipInterpolation {
230-
return interp.Interpolate(yaml, *opts.Interpolate)
230+
return interp.Interpolate(yml, *opts.Interpolate)
231231
}
232-
return yaml, err
232+
return yml, err
233233
}
234234

235235
func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {
@@ -342,8 +342,8 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec
342342
reflect.TypeOf(types.UlimitsConfig{}): transformUlimits,
343343
reflect.TypeOf(types.UnitBytes(0)): transformSize,
344344
reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort,
345-
reflect.TypeOf(types.ServiceSecretConfig{}): transformStringSourceMap,
346-
reflect.TypeOf(types.ServiceConfigObjConfig{}): transformStringSourceMap,
345+
reflect.TypeOf(types.ServiceSecretConfig{}): transformFileReferenceConfig,
346+
reflect.TypeOf(types.ServiceConfigObjConfig{}): transformFileReferenceConfig,
347347
reflect.TypeOf(types.StringOrNumberList{}): transformStringOrNumberList,
348348
reflect.TypeOf(map[string]*types.ServiceNetworkConfig{}): transformServiceNetworkMap,
349349
reflect.TypeOf(types.Mapping{}): transformMappingOrListFunc("=", false),
@@ -372,7 +372,7 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec
372372
}
373373
}
374374

375-
// keys needs to be converted to strings for jsonschema
375+
// keys need to be converted to strings for jsonschema
376376
func convertToStringKeysRecursive(value interface{}, keyPrefix string) (interface{}, error) {
377377
if mapping, ok := value.(map[interface{}]interface{}); ok {
378378
dict := make(map[string]interface{})
@@ -396,7 +396,7 @@ func convertToStringKeysRecursive(value interface{}, keyPrefix string) (interfac
396396
return dict, nil
397397
}
398398
if list, ok := value.([]interface{}); ok {
399-
convertedList := []interface{}{}
399+
var convertedList []interface{}
400400
for index, entry := range list {
401401
newKeyPrefix := fmt.Sprintf("%s[%d]", keyPrefix, index)
402402
convertedEntry, err := convertToStringKeysRecursive(entry, newKeyPrefix)
@@ -532,7 +532,7 @@ func LoadService(name string, serviceDict map[string]interface{}, workingDir str
532532
}
533533

534534
for i, volume := range serviceConfig.Volumes {
535-
if volume.Type != "bind" {
535+
if volume.Type != types.VolumeTypeBind {
536536
continue
537537
}
538538

@@ -552,8 +552,8 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l
552552
environment := types.MappingWithEquals{}
553553

554554
if len(serviceConfig.EnvFile) > 0 {
555-
for _, file := range serviceConfig.EnvFile {
556-
filePath := absPath(workingDir, file)
555+
for _, envFile := range serviceConfig.EnvFile {
556+
filePath := absPath(workingDir, envFile)
557557
file, err := os.Open(filePath)
558558
if err != nil {
559559
return err
@@ -797,7 +797,7 @@ var transformServicePort TransformerFunc = func(data interface{}) (interface{},
797797
// We process the list instead of individual items here.
798798
// The reason is that one entry might be mapped to multiple ServicePortConfig.
799799
// Therefore we take an input of a list and return an output of a list.
800-
ports := []interface{}{}
800+
var ports []interface{}
801801
for _, entry := range entries {
802802
switch value := entry.(type) {
803803
case int:
@@ -852,17 +852,27 @@ var transformServiceDeviceRequest TransformerFunc = func(data interface{}) (inte
852852
}
853853
}
854854

855-
var transformStringSourceMap TransformerFunc = func(data interface{}) (interface{}, error) {
855+
var transformFileReferenceConfig TransformerFunc = func(data interface{}) (interface{}, error) {
856856
switch value := data.(type) {
857857
case string:
858858
return map[string]interface{}{"source": value}, nil
859859
case map[string]interface{}:
860-
return groupXFieldsIntoExtensions(data.(map[string]interface{})), nil
860+
if target, ok := value["target"]; ok {
861+
value["target"] = cleanTarget(target.(string))
862+
}
863+
return groupXFieldsIntoExtensions(value), nil
861864
default:
862865
return data, errors.Errorf("invalid type %T for secret", value)
863866
}
864867
}
865868

869+
func cleanTarget(target string) string {
870+
if target == "" {
871+
return ""
872+
}
873+
return path.Clean(target)
874+
}
875+
866876
var transformBuildConfig TransformerFunc = func(data interface{}) (interface{}, error) {
867877
switch value := data.(type) {
868878
case string:
@@ -906,9 +916,15 @@ var transformExtendsConfig TransformerFunc = func(data interface{}) (interface{}
906916
var transformServiceVolumeConfig TransformerFunc = func(data interface{}) (interface{}, error) {
907917
switch value := data.(type) {
908918
case string:
909-
return ParseVolume(value)
919+
volume, err := ParseVolume(value)
920+
volume.Target = cleanTarget(volume.Target)
921+
return volume, err
910922
case map[string]interface{}:
911-
return groupXFieldsIntoExtensions(data.(map[string]interface{})), nil
923+
data := groupXFieldsIntoExtensions(data.(map[string]interface{}))
924+
if target, ok := data["target"]; ok {
925+
data["target"] = cleanTarget(target.(string))
926+
}
927+
return data, nil
912928
default:
913929
return data, errors.Errorf("invalid type %T for service volume", value)
914930
}
@@ -971,7 +987,7 @@ func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool
971987
switch value := mappingOrList.(type) {
972988
case map[string]interface{}:
973989
return toMapStringString(value, allowNil)
974-
case ([]interface{}):
990+
case []interface{}:
975991
result := make(map[string]interface{})
976992
for _, value := range value {
977993
parts := strings.SplitN(value.(string), sep, 2)
@@ -1054,7 +1070,7 @@ func toString(value interface{}, allowNil bool) interface{} {
10541070
}
10551071

10561072
func toStringList(value map[string]interface{}, separator string, allowNil bool) []string {
1057-
output := []string{}
1073+
var output []string
10581074
for key, value := range value {
10591075
if value == nil && !allowNil {
10601076
continue

loader/loader_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ services:
18121812
assert.NilError(t, err)
18131813
}
18141814

1815-
func TestLoadService(t *testing.T) {
1815+
func TestLoadServiceWithEnvFile(t *testing.T) {
18161816
file, err := os.CreateTemp("", "test-compose-go")
18171817
assert.NilError(t, err)
18181818
defer os.Remove(file.Name())
@@ -1830,3 +1830,33 @@ func TestLoadService(t *testing.T) {
18301830
assert.NilError(t, err)
18311831
assert.Equal(t, "YES", *s.Environment["HALLO"])
18321832
}
1833+
1834+
func TestLoadServiceWithVolumes(t *testing.T) {
1835+
m := map[string]interface{}{
1836+
"volumes": []interface{}{
1837+
"source:/path 1/",
1838+
map[string]interface{}{
1839+
"target": "/path 2/",
1840+
},
1841+
},
1842+
"configs": []interface{}{
1843+
map[string]interface{}{
1844+
"target": "/path 3/",
1845+
},
1846+
},
1847+
"secrets": []interface{}{
1848+
map[string]interface{}{
1849+
"target": "/path 4/",
1850+
},
1851+
},
1852+
}
1853+
s, err := LoadService("Test Name", m, ".", nil, true)
1854+
assert.NilError(t, err)
1855+
assert.Equal(t, len(s.Volumes), 2)
1856+
assert.Equal(t, "/path 1", s.Volumes[0].Target)
1857+
assert.Equal(t, "/path 2", s.Volumes[1].Target)
1858+
assert.Equal(t, len(s.Configs), 1)
1859+
assert.Equal(t, "/path 3", s.Configs[0].Target)
1860+
assert.Equal(t, len(s.Secrets), 1)
1861+
assert.Equal(t, "/path 4", s.Secrets[0].Target)
1862+
}

loader/merge.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func toServiceVolumeConfigsMap(s interface{}) (map[interface{}]interface{}, erro
184184
}
185185

186186
func toServiceSecretConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
187-
s := []types.ServiceSecretConfig{}
187+
var s []types.ServiceSecretConfig
188188
for _, v := range m {
189189
s = append(s, v.(types.ServiceSecretConfig))
190190
}
@@ -194,7 +194,7 @@ func toServiceSecretConfigsSlice(dst reflect.Value, m map[interface{}]interface{
194194
}
195195

196196
func toSServiceConfigObjConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
197-
s := []types.ServiceConfigObjConfig{}
197+
var s []types.ServiceConfigObjConfig
198198
for _, v := range m {
199199
s = append(s, v.(types.ServiceConfigObjConfig))
200200
}
@@ -204,7 +204,7 @@ func toSServiceConfigObjConfigsSlice(dst reflect.Value, m map[interface{}]interf
204204
}
205205

206206
func toServicePortConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
207-
s := []types.ServicePortConfig{}
207+
var s []types.ServicePortConfig
208208
for _, v := range m {
209209
s = append(s, v.(types.ServicePortConfig))
210210
}
@@ -225,7 +225,7 @@ func toServicePortConfigsSlice(dst reflect.Value, m map[interface{}]interface{})
225225
}
226226

227227
func toServiceVolumeConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
228-
s := []types.ServiceVolumeConfig{}
228+
var s []types.ServiceVolumeConfig
229229
for _, v := range m {
230230
s = append(s, v.(types.ServiceVolumeConfig))
231231
}
@@ -234,7 +234,7 @@ func toServiceVolumeConfigsSlice(dst reflect.Value, m map[interface{}]interface{
234234
return nil
235235
}
236236

237-
type tomapFn func(s interface{}) (map[interface{}]interface{}, error)
237+
type toMapFn func(s interface{}) (map[interface{}]interface{}, error)
238238
type writeValueFromMapFn func(reflect.Value, map[interface{}]interface{}) error
239239

240240
func safelyMerge(mergeFn func(dst, src reflect.Value) error) func(dst, src reflect.Value) error {
@@ -250,13 +250,13 @@ func safelyMerge(mergeFn func(dst, src reflect.Value) error) func(dst, src refle
250250
}
251251
}
252252

253-
func mergeSlice(tomap tomapFn, writeValue writeValueFromMapFn) func(dst, src reflect.Value) error {
253+
func mergeSlice(toMap toMapFn, writeValue writeValueFromMapFn) func(dst, src reflect.Value) error {
254254
return func(dst, src reflect.Value) error {
255-
dstMap, err := sliceToMap(tomap, dst)
255+
dstMap, err := sliceToMap(toMap, dst)
256256
if err != nil {
257257
return err
258258
}
259-
srcMap, err := sliceToMap(tomap, src)
259+
srcMap, err := sliceToMap(toMap, src)
260260
if err != nil {
261261
return err
262262
}
@@ -267,12 +267,12 @@ func mergeSlice(tomap tomapFn, writeValue writeValueFromMapFn) func(dst, src ref
267267
}
268268
}
269269

270-
func sliceToMap(tomap tomapFn, v reflect.Value) (map[interface{}]interface{}, error) {
270+
func sliceToMap(toMap toMapFn, v reflect.Value) (map[interface{}]interface{}, error) {
271271
// check if valid
272272
if !v.IsValid() {
273273
return nil, errors.Errorf("invalid value : %+v", v)
274274
}
275-
return tomap(v.Interface())
275+
return toMap(v.Interface())
276276
}
277277

278278
func mergeLoggingConfig(dst, src reflect.Value) error {

loader/volume.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package loader
1818

1919
import (
20-
"path"
2120
"strings"
2221
"unicode"
2322
"unicode/utf8"
@@ -57,7 +56,6 @@ func ParseVolume(spec string) (types.ServiceVolumeConfig, error) {
5756
}
5857
}
5958

60-
volume.Target = path.Clean(volume.Target)
6159
populateType(&volume)
6260
return volume, nil
6361
}

loader/volume_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ func TestParseVolumeAnonymousVolume(t *testing.T) {
3434
}
3535
}
3636

37-
func TestParseVolumeCleanTarget(t *testing.T) {
38-
volume, err := ParseVolume("/path/")
39-
expected := types.ServiceVolumeConfig{Type: "volume", Target: "/path", Volume: &types.ServiceVolumeVolume{}}
40-
assert.NilError(t, err)
41-
assert.Check(t, is.DeepEqual(expected, volume))
42-
}
43-
4437
func TestParseVolumeAnonymousVolumeWindows(t *testing.T) {
4538
for _, path := range []string{"C:\\path", "Z:\\path\\foo"} {
4639
volume, err := ParseVolume(path)

types/project.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Project struct {
4646

4747
// ServiceNames return names for all services in this Compose config
4848
func (p Project) ServiceNames() []string {
49-
names := []string{}
49+
var names []string
5050
for _, s := range p.Services {
5151
names = append(names, s.Name)
5252
}
@@ -56,7 +56,7 @@ func (p Project) ServiceNames() []string {
5656

5757
// VolumeNames return names for all volumes in this Compose config
5858
func (p Project) VolumeNames() []string {
59-
names := []string{}
59+
var names []string
6060
for k := range p.Volumes {
6161
names = append(names, k)
6262
}
@@ -66,7 +66,7 @@ func (p Project) VolumeNames() []string {
6666

6767
// NetworkNames return names for all volumes in this Compose config
6868
func (p Project) NetworkNames() []string {
69-
names := []string{}
69+
var names []string
7070
for k := range p.Networks {
7171
names = append(names, k)
7272
}
@@ -76,7 +76,7 @@ func (p Project) NetworkNames() []string {
7676

7777
// SecretNames return names for all secrets in this Compose config
7878
func (p Project) SecretNames() []string {
79-
names := []string{}
79+
var names []string
8080
for k := range p.Secrets {
8181
names = append(names, k)
8282
}
@@ -86,7 +86,7 @@ func (p Project) SecretNames() []string {
8686

8787
// ConfigNames return names for all configs in this Compose config
8888
func (p Project) ConfigNames() []string {
89-
names := []string{}
89+
var names []string
9090
for k := range p.Configs {
9191
names = append(names, k)
9292
}
@@ -179,12 +179,12 @@ func (p *Project) RelativePath(path string) string {
179179
}
180180

181181
// HasProfile return true if service has no profile declared or has at least one profile matching
182-
func (service ServiceConfig) HasProfile(profiles []string) bool {
183-
if len(service.Profiles) == 0 {
182+
func (s ServiceConfig) HasProfile(profiles []string) bool {
183+
if len(s.Profiles) == 0 {
184184
return true
185185
}
186186
for _, p := range profiles {
187-
for _, sp := range service.Profiles {
187+
for _, sp := range s.Profiles {
188188
if sp == p {
189189
return true
190190
}
@@ -327,7 +327,6 @@ func (p *Project) ResolveImages(resolver func(named reference.Named) (digest.Dig
327327
if err != nil {
328328
return err
329329
}
330-
331330
named, err = reference.WithDigest(named, digest)
332331
if err != nil {
333332
return err

0 commit comments

Comments
 (0)