@@ -403,22 +403,24 @@ func (o *ProjectOptions) GetWorkingDir() (string, error) {
403
403
return os .Getwd ()
404
404
}
405
405
406
- func (o * ProjectOptions ) GetConfigFiles () ([]types.ConfigFile , error ) {
407
- configPaths , err := o .getConfigPaths ()
406
+ // ReadConfigFiles reads ConfigFiles and populates the content field
407
+ func (o ProjectOptions ) ReadConfigFiles (ctx context.Context , options * ProjectOptions ) (* types.ConfigDetails , error ) {
408
+ config , err := loader .LoadConfigFiles (ctx , options .ConfigPaths , options .loadOptions ... )
408
409
if err != nil {
409
410
return nil , err
410
411
}
412
+ configs := make ([][]byte , len (config .ConfigFiles ))
411
413
412
- var configs []types. ConfigFile
413
- for _ , f := range configPaths {
414
+ for i , c := range config . ConfigFiles {
415
+ var err error
414
416
var b []byte
415
- if f == "-" {
417
+ if c . Filename == "-" {
416
418
b , err = io .ReadAll (os .Stdin )
417
419
if err != nil {
418
420
return nil , err
419
421
}
420
422
} else {
421
- f , err := filepath .Abs (f )
423
+ f , err := filepath .Abs (c . Filename )
422
424
if err != nil {
423
425
return nil , err
424
426
}
@@ -427,27 +429,31 @@ func (o *ProjectOptions) GetConfigFiles() ([]types.ConfigFile, error) {
427
429
return nil , err
428
430
}
429
431
}
430
- configs = append (configs , types.ConfigFile {
431
- Filename : f ,
432
- Content : b ,
433
- })
432
+ configs [i ] = b
434
433
}
435
- return configs , err
434
+ for i , c := range configs {
435
+ config .ConfigFiles [i ].Content = c
436
+ }
437
+ return config , nil
436
438
}
437
439
438
440
// LoadProject loads compose file according to options and bind to types.Project go structs
439
441
func (o * ProjectOptions ) LoadProject (ctx context.Context ) (* types.Project , error ) {
440
- configDetails , err := o .prepare ()
442
+ config , err := o .prepare (ctx )
441
443
if err != nil {
442
444
return nil , err
443
445
}
444
446
445
- project , err := loader .LoadWithContext (ctx , configDetails , o .loadOptions ... )
447
+ project , err := loader .LoadWithContext (ctx , types.ConfigDetails {
448
+ ConfigFiles : config .ConfigFiles ,
449
+ WorkingDir : config .WorkingDir ,
450
+ Environment : o .Environment ,
451
+ }, o .loadOptions ... )
446
452
if err != nil {
447
453
return nil , err
448
454
}
449
455
450
- for _ , config := range configDetails .ConfigFiles {
456
+ for _ , config := range config .ConfigFiles {
451
457
project .ComposeFiles = append (project .ComposeFiles , config .Filename )
452
458
}
453
459
@@ -456,36 +462,31 @@ func (o *ProjectOptions) LoadProject(ctx context.Context) (*types.Project, error
456
462
457
463
// LoadModel loads compose file according to options and returns a raw (yaml tree) model
458
464
func (o * ProjectOptions ) LoadModel (ctx context.Context ) (map [string ]any , error ) {
459
- configDetails , err := o .prepare ()
465
+ configDetails , err := o .prepare (ctx )
460
466
if err != nil {
461
467
return nil , err
462
468
}
463
469
464
- return loader .LoadModelWithContext (ctx , configDetails , o .loadOptions ... )
470
+ return loader .LoadModelWithContext (ctx , * configDetails , o .loadOptions ... )
465
471
}
466
472
467
473
// prepare converts ProjectOptions into loader's types.ConfigDetails and configures default load options
468
- func (o * ProjectOptions ) prepare () (types.ConfigDetails , error ) {
469
- configs , err := o .GetConfigFiles ()
474
+ func (o * ProjectOptions ) prepare (ctx context. Context ) (* types.ConfigDetails , error ) {
475
+ defaultDir , err := o .GetWorkingDir ()
470
476
if err != nil {
471
- return types.ConfigDetails {}, err
477
+ return & types.ConfigDetails {}, err
472
478
}
473
479
474
- workingDir , err := o .GetWorkingDir ( )
480
+ configDetails , err := o .ReadConfigFiles ( ctx , o )
475
481
if err != nil {
476
- return types.ConfigDetails {}, err
477
- }
478
-
479
- configDetails := types.ConfigDetails {
480
- ConfigFiles : configs ,
481
- WorkingDir : workingDir ,
482
- Environment : o .Environment ,
482
+ return configDetails , err
483
483
}
484
484
485
485
o .loadOptions = append (o .loadOptions ,
486
- withNamePrecedenceLoad (workingDir , o ),
486
+ withNamePrecedenceLoad (defaultDir , o ),
487
487
withConvertWindowsPaths (o ),
488
488
withListeners (o ))
489
+
489
490
return configDetails , nil
490
491
}
491
492
0 commit comments