@@ -196,36 +196,11 @@ func handleIncludes(cfg *Config) error {
196196
197197 for _ , includePath := range cfg .Include {
198198 if u , err := url .ParseRequestURI (includePath ); err == nil && u .Scheme != "" {
199- // Download file from URL to temp file
200- tmpFile , err := os .CreateTemp ("" , "config-*.yml" )
199+ includePath , err = fetchConfigFile (includePath )
201200 if err != nil {
202- return errors .Wrapf (err , "failed to create temp file for URL" )
201+ return errors .Wrapf (err , "failed to fetch included config file '%s'" , includePath )
203202 }
204- defer func () {
205- tmpFile .Close ()
206- os .Remove (tmpFile .Name ())
207- }()
208-
209- client := & http.Client {Timeout : 30 * time .Second }
210- resp , err := client .Get (includePath )
211- if err != nil {
212- return errors .Wrapf (err , "failed to download from URL '%s'" , includePath )
213- }
214- defer resp .Body .Close ()
215-
216- if resp .StatusCode != http .StatusOK {
217- return errors .Errorf ("failed to download file, status code: %d" , resp .StatusCode )
218- }
219-
220- if _ , err = io .Copy (tmpFile , resp .Body ); err != nil {
221- return errors .Wrapf (err , "failed to save downloaded file from '%s'" , includePath )
222- }
223-
224- if _ , err = tmpFile .Seek (0 , io .SeekStart ); err != nil {
225- return errors .Wrapf (err , "failed to rewind temp file from '%s'" , includePath )
226- }
227-
228- includePath = tmpFile .Name ()
203+ defer os .Remove (includePath )
229204 }
230205
231206 // Resolve path - if relative, use base directory
@@ -246,11 +221,45 @@ func handleIncludes(cfg *Config) error {
246221 return errors .Wrapf (err , "failed to parse included config file '%s'" , includePath )
247222 }
248223
224+ if cfg .Version != includeCfg .Version {
225+ return errors .Errorf ("included config version '%d' does not match with chain config version '%d'" , includeCfg .Version , cfg .Version )
226+ }
227+
249228 // Merge the included config with the primary config
250- if err = mergo .Merge (cfg , includeCfg , mergo .WithOverride ); err != nil {
229+ if err = mergo .Merge (cfg , includeCfg , mergo .WithAppendSlice , mergo . WithOverride ); err != nil {
251230 return errors .Wrapf (err , "failed to merge included file '%s'" , includePath )
252231 }
253232 }
254233
255234 return nil
256235}
236+
237+ func fetchConfigFile (URL string ) (string , error ) {
238+ // Download file from URL to temp file
239+ tmpFile , err := os .CreateTemp ("" , "config-*.yml" )
240+ if err != nil {
241+ return "" , errors .Wrapf (err , "failed to create temp file for URL" )
242+ }
243+ defer tmpFile .Close ()
244+
245+ client := & http.Client {Timeout : 30 * time .Second }
246+ resp , err := client .Get (URL )
247+ if err != nil {
248+ return "" , errors .Wrapf (err , "failed to download from URL '%s'" , URL )
249+ }
250+ defer resp .Body .Close ()
251+
252+ if resp .StatusCode != http .StatusOK {
253+ return "" , errors .Errorf ("failed to download file, status code: %d" , resp .StatusCode )
254+ }
255+
256+ if _ , err = io .Copy (tmpFile , resp .Body ); err != nil {
257+ return "" , errors .Wrapf (err , "failed to save downloaded file from '%s'" , URL )
258+ }
259+
260+ if _ , err = tmpFile .Seek (0 , io .SeekStart ); err != nil {
261+ return "" , errors .Wrapf (err , "failed to rewind temp file from '%s'" , URL )
262+ }
263+
264+ return tmpFile .Name (), nil
265+ }
0 commit comments