Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 99e58be

Browse files
committed
Create config separately
Signed-off-by: rajashree <[email protected]>
1 parent fa77a0c commit 99e58be

File tree

4 files changed

+47
-78
lines changed

4 files changed

+47
-78
lines changed

config/merge.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,55 @@ var (
2323
}
2424
)
2525

26+
// CreateConfig unmarshals bytes to config and creates config based on version
27+
func CreateConfig(bytes []byte) (*Config, error) {
28+
var config Config
29+
if err := yaml.Unmarshal(bytes, &config); err != nil {
30+
return nil, err
31+
}
32+
if config.Version == "2" {
33+
for key, value := range config.Networks {
34+
if value == nil {
35+
config.Networks[key] = &NetworkConfig{}
36+
}
37+
}
38+
for key, value := range config.Volumes {
39+
if value == nil {
40+
config.Volumes[key] = &VolumeConfig{}
41+
}
42+
}
43+
} else {
44+
var baseRawServices RawServiceMap
45+
if err := yaml.Unmarshal(bytes, &baseRawServices); err != nil {
46+
return nil, err
47+
}
48+
config.Services = baseRawServices
49+
}
50+
51+
return &config, nil
52+
}
53+
2654
// Merge merges a compose file into an existing set of service configs
2755
func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, bytes []byte, options *ParseOptions) (string, map[string]*ServiceConfig, map[string]*VolumeConfig, map[string]*NetworkConfig, error) {
2856
if options == nil {
2957
options = &defaultParseOptions
3058
}
3159

32-
var config Config
33-
if err := yaml.Unmarshal(bytes, &config); err != nil {
60+
config, err := CreateConfig(bytes)
61+
if err != nil {
3462
return "", nil, nil, nil, err
3563
}
64+
baseRawServices := config.Services
3665

3766
var serviceConfigs map[string]*ServiceConfig
38-
var volumeConfigs map[string]*VolumeConfig
39-
var networkConfigs map[string]*NetworkConfig
4067
if config.Version == "2" {
4168
var err error
42-
serviceConfigs, err = MergeServicesV2(existingServices, environmentLookup, resourceLookup, file, bytes, options)
43-
if err != nil {
44-
return "", nil, nil, nil, err
45-
}
46-
volumeConfigs, err = ParseVolumes(bytes)
47-
if err != nil {
48-
return "", nil, nil, nil, err
49-
}
50-
networkConfigs, err = ParseNetworks(bytes)
69+
serviceConfigs, err = MergeServicesV2(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
5170
if err != nil {
5271
return "", nil, nil, nil, err
5372
}
5473
} else {
55-
serviceConfigsV1, err := MergeServicesV1(existingServices, environmentLookup, resourceLookup, file, bytes, options)
74+
serviceConfigsV1, err := MergeServicesV1(existingServices, environmentLookup, resourceLookup, file, baseRawServices, options)
5675
if err != nil {
5776
return "", nil, nil, nil, err
5877
}
@@ -72,7 +91,7 @@ func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup
7291
}
7392
}
7493

75-
return config.Version, serviceConfigs, volumeConfigs, networkConfigs, nil
94+
return config.Version, serviceConfigs, config.Volumes, config.Networks, nil
7695
}
7796

7897
func adjustValues(configs map[string]*ServiceConfig) {

config/merge_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,17 @@ services:
406406
}
407407
}
408408

409-
func TestParseNetworks(t *testing.T) {
410-
composeFile := []byte(`networks:
409+
func TestNilNetworks(t *testing.T) {
410+
composeFile := []byte(`
411+
version: '2'
412+
networks:
411413
public:`)
412414

413-
networkConfigs, err := ParseNetworks(composeFile)
415+
config, err := CreateConfig(composeFile)
414416
if err != nil {
415417
t.Fatal(err)
416418
}
417-
for key, networkConfig := range networkConfigs {
419+
for key, networkConfig := range config.Networks {
418420
if networkConfig == nil {
419421
t.Fatalf("networkConfig %s was nil, shouldn't be", key)
420422
}

config/merge_v1.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ import (
66

77
"github.com/Sirupsen/logrus"
88
"github.com/docker/libcompose/utils"
9-
"gopkg.in/yaml.v2"
109
)
1110

1211
// MergeServicesV1 merges a v1 compose file into an existing set of service configs
13-
func MergeServicesV1(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, bytes []byte, options *ParseOptions) (map[string]*ServiceConfigV1, error) {
14-
datas := make(RawServiceMap)
15-
if err := yaml.Unmarshal(bytes, &datas); err != nil {
16-
return nil, err
17-
}
18-
12+
func MergeServicesV1(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, datas RawServiceMap, options *ParseOptions) (map[string]*ServiceConfigV1, error) {
1913
if options.Interpolate {
2014
if err := Interpolate(environmentLookup, &datas); err != nil {
2115
return nil, err
@@ -116,10 +110,11 @@ func parseV1(resourceLookup ResourceLookup, environmentLookup EnvironmentLookup,
116110
return nil, err
117111
}
118112

119-
var baseRawServices RawServiceMap
120-
if err := yaml.Unmarshal(bytes, &baseRawServices); err != nil {
113+
config, err := CreateConfig(bytes)
114+
if err != nil {
121115
return nil, err
122116
}
117+
baseRawServices := config.Services
123118

124119
if options.Interpolate {
125120
err = Interpolate(environmentLookup, &baseRawServices)

config/merge_v2.go

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,10 @@ import (
66

77
"github.com/Sirupsen/logrus"
88
"github.com/docker/libcompose/utils"
9-
"gopkg.in/yaml.v2"
109
)
1110

1211
// MergeServicesV2 merges a v2 compose file into an existing set of service configs
13-
func MergeServicesV2(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, bytes []byte, options *ParseOptions) (map[string]*ServiceConfig, error) {
14-
var config Config
15-
if err := yaml.Unmarshal(bytes, &config); err != nil {
16-
return nil, err
17-
}
18-
19-
datas := config.Services
20-
12+
func MergeServicesV2(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, datas RawServiceMap, options *ParseOptions) (map[string]*ServiceConfig, error) {
2113
if options.Interpolate {
2214
if err := Interpolate(environmentLookup, &datas); err != nil {
2315
return nil, err
@@ -65,44 +57,6 @@ func MergeServicesV2(existingServices *ServiceConfigs, environmentLookup Environ
6557
return serviceConfigs, nil
6658
}
6759

68-
// ParseVolumes parses volumes in a compose file
69-
func ParseVolumes(bytes []byte) (map[string]*VolumeConfig, error) {
70-
volumeConfigs := make(map[string]*VolumeConfig)
71-
72-
var config Config
73-
if err := yaml.Unmarshal(bytes, &config); err != nil {
74-
return nil, err
75-
}
76-
77-
if err := utils.Convert(config.Volumes, &volumeConfigs); err != nil {
78-
return nil, err
79-
}
80-
81-
return volumeConfigs, nil
82-
}
83-
84-
// ParseNetworks parses networks in a compose file
85-
func ParseNetworks(bytes []byte) (map[string]*NetworkConfig, error) {
86-
networkConfigs := make(map[string]*NetworkConfig)
87-
88-
var config Config
89-
if err := yaml.Unmarshal(bytes, &config); err != nil {
90-
return nil, err
91-
}
92-
93-
if err := utils.Convert(config.Networks, &networkConfigs); err != nil {
94-
return nil, err
95-
}
96-
97-
for key, value := range networkConfigs {
98-
if value == nil {
99-
networkConfigs[key] = &NetworkConfig{}
100-
}
101-
}
102-
103-
return networkConfigs, nil
104-
}
105-
10660
func parseV2(resourceLookup ResourceLookup, environmentLookup EnvironmentLookup, inFile string, serviceData RawService, datas RawServiceMap, options *ParseOptions) (RawService, error) {
10761
serviceData, err := readEnvFile(resourceLookup, inFile, serviceData)
10862
if err != nil {
@@ -147,11 +101,10 @@ func parseV2(resourceLookup ResourceLookup, environmentLookup EnvironmentLookup,
147101
return nil, err
148102
}
149103

150-
var config Config
151-
if err := yaml.Unmarshal(bytes, &config); err != nil {
104+
config, err := CreateConfig(bytes)
105+
if err != nil {
152106
return nil, err
153107
}
154-
155108
baseRawServices := config.Services
156109

157110
if options.Interpolate {

0 commit comments

Comments
 (0)