@@ -29,25 +29,22 @@ func CreateConfig(bytes []byte) (*Config, error) {
2929 if err := yaml .Unmarshal (bytes , & config ); err != nil {
3030 return nil , err
3131 }
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 {
32+
33+ if config .Version != "2" {
4434 var baseRawServices RawServiceMap
4535 if err := yaml .Unmarshal (bytes , & baseRawServices ); err != nil {
4636 return nil , err
4737 }
4838 config .Services = baseRawServices
4939 }
5040
41+ if config .Volumes == nil {
42+ config .Volumes = make (map [string ]interface {})
43+ }
44+ if config .Networks == nil {
45+ config .Networks = make (map [string ]interface {})
46+ }
47+
5148 return & config , nil
5249}
5350
@@ -63,6 +60,34 @@ func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup
6360 }
6461 baseRawServices := config .Services
6562
63+ if options .Interpolate {
64+ if err := InterpolateRawServiceMap (& baseRawServices , environmentLookup ); err != nil {
65+ return "" , nil , nil , nil , err
66+ }
67+
68+ for k , v := range config .Volumes {
69+ if err := Interpolate (k , & v , environmentLookup ); err != nil {
70+ return "" , nil , nil , nil , err
71+ }
72+ config .Volumes [k ] = v
73+ }
74+
75+ for k , v := range config .Networks {
76+ if err := Interpolate (k , & v , environmentLookup ); err != nil {
77+ return "" , nil , nil , nil , err
78+ }
79+ config .Networks [k ] = v
80+ }
81+ }
82+
83+ if options .Preprocess != nil {
84+ var err error
85+ baseRawServices , err = options .Preprocess (baseRawServices )
86+ if err != nil {
87+ return "" , nil , nil , nil , err
88+ }
89+ }
90+
6691 var serviceConfigs map [string ]* ServiceConfig
6792 if config .Version == "2" {
6893 var err error
@@ -91,7 +116,29 @@ func Merge(existingServices *ServiceConfigs, environmentLookup EnvironmentLookup
91116 }
92117 }
93118
94- return config .Version , serviceConfigs , config .Volumes , config .Networks , nil
119+ var volumes map [string ]* VolumeConfig
120+ var networks map [string ]* NetworkConfig
121+ if err := utils .Convert (config .Volumes , & volumes ); err != nil {
122+ return "" , nil , nil , nil , err
123+ }
124+ if err := utils .Convert (config .Networks , & networks ); err != nil {
125+ return "" , nil , nil , nil , err
126+ }
127+
128+ return config .Version , serviceConfigs , volumes , networks , nil
129+ }
130+
131+ // InterpolateRawServiceMap replaces varialbse in raw service map struct based on environment lookup
132+ func InterpolateRawServiceMap (baseRawServices * RawServiceMap , environmentLookup EnvironmentLookup ) error {
133+ for k , v := range * baseRawServices {
134+ for k2 , v2 := range v {
135+ if err := Interpolate (k2 , & v2 , environmentLookup ); err != nil {
136+ return err
137+ }
138+ (* baseRawServices )[k ][k2 ] = v2
139+ }
140+ }
141+ return nil
95142}
96143
97144func adjustValues (configs map [string ]* ServiceConfig ) {
0 commit comments