Skip to content

Commit 5679ee1

Browse files
committed
fix lint and improve readbility
1 parent 27c09b7 commit 5679ee1

2 files changed

Lines changed: 89 additions & 32 deletions

File tree

ignite/config/chain/parse.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

ignite/config/chain/parse_test.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func TestHandleIncludes(t *testing.T) {
122122
{
123123
name: "Single valid include",
124124
baseConfig: `
125+
version: 1
125126
client:
126127
typescript:
127128
path: original
@@ -160,6 +161,7 @@ validators:
160161
{
161162
name: "Multiple valid includes",
162163
baseConfig: `
164+
version: 1
163165
client:
164166
typescript:
165167
path: original
@@ -177,6 +179,10 @@ build:
177179
proto:
178180
path: proto
179181
accounts:
182+
- name: bob
183+
coins:
184+
- 10000token
185+
- 100000000stake
180186
- name: alice
181187
coins:
182188
- 20000token
@@ -186,19 +192,24 @@ faucet:
186192
coins:
187193
- 5token
188194
- 100000stake
195+
- 5token
196+
- 100000stake
189197
host: 0.0.0.0:4500
190198
client:
191199
typescript:
192200
path: override-2
193201
openapi:
194202
path: docs/static/include2.yml
195203
validators:
204+
- name: alice
205+
bonded: 100000000stake
196206
- name: validator1
197207
bonded: 100000000stake`,
198208
},
199209
{
200210
name: "Invalid include file path",
201211
baseConfig: `
212+
version: 1
202213
client:
203214
typescript:
204215
path: original
@@ -209,8 +220,8 @@ include:
209220
{
210221
name: "Empty include list",
211222
baseConfig: `
212-
validation: sovereign
213223
version: 1
224+
validation: sovereign
214225
accounts:
215226
- name: alice
216227
coins:
@@ -255,6 +266,7 @@ validators:
255266
{
256267
name: "Empty values include",
257268
baseConfig: `
269+
version: 1
258270
include:
259271
- "./testdata/include1.yml"
260272
- "./testdata/include2.yml"
@@ -269,6 +281,10 @@ build:
269281
proto:
270282
path: proto
271283
accounts:
284+
- name: bob
285+
coins:
286+
- 10000token
287+
- 100000000stake
272288
- name: alice
273289
coins:
274290
- 20000token
@@ -278,21 +294,25 @@ faucet:
278294
coins:
279295
- 5token
280296
- 100000stake
297+
- 5token
298+
- 100000stake
281299
host: 0.0.0.0:4500
282300
client:
283301
typescript:
284302
path: override-2
285303
openapi:
286304
path: docs/static/include2.yml
287305
validators:
306+
- name: alice
307+
bonded: 100000000stake
288308
- name: validator1
289309
bonded: 100000000stake`,
290310
},
291311
{
292312
name: "HTTP include",
293313
baseConfig: fmt.Sprintf(`
294-
validation: sovereign
295314
version: 1
315+
validation: sovereign
296316
accounts:
297317
- name: alice
298318
coins:
@@ -323,13 +343,23 @@ build:
323343
proto:
324344
path: proto
325345
accounts:
346+
- name: alice
347+
coins:
348+
- 10000token
349+
- name: bob
350+
coins:
351+
- 10000token
352+
- 100000000stake
326353
- name: alice
327354
coins:
328355
- 20000token
329356
- 200000000stake
330357
faucet:
331358
name: alice
332359
coins:
360+
- 5token
361+
- 5token
362+
- 100000stake
333363
- 5token
334364
- 100000stake
335365
host: 0.0.0.0:4500
@@ -339,14 +369,18 @@ client:
339369
openapi:
340370
path: docs/static/include2.yml
341371
validators:
372+
- name: alice
373+
bonded: 100stake
374+
- name: alice
375+
bonded: 100000000stake
342376
- name: validator1
343377
bonded: 100000000stake`, server),
344378
},
345379
{
346380
name: "HTTP and local include",
347381
baseConfig: fmt.Sprintf(`
348-
validation: sovereign
349382
version: 1
383+
validation: sovereign
350384
accounts:
351385
- name: alice
352386
coins:
@@ -377,13 +411,23 @@ build:
377411
proto:
378412
path: proto
379413
accounts:
414+
- name: alice
415+
coins:
416+
- 10000token
417+
- name: bob
418+
coins:
419+
- 10000token
420+
- 100000000stake
380421
- name: alice
381422
coins:
382423
- 20000token
383424
- 200000000stake
384425
faucet:
385426
name: alice
386427
coins:
428+
- 5token
429+
- 5token
430+
- 100000stake
387431
- 5token
388432
- 100000stake
389433
host: 0.0.0.0:4500
@@ -393,6 +437,10 @@ client:
393437
openapi:
394438
path: docs/static/include2.yml
395439
validators:
440+
- name: alice
441+
bonded: 100stake
442+
- name: alice
443+
bonded: 100000000stake
396444
- name: validator1
397445
bonded: 100000000stake`, server),
398446
},

0 commit comments

Comments
 (0)