Skip to content

Commit c558adc

Browse files
Mjaethersglours
authored andcommitted
ingest config files with ResourceLoader
Signed-off-by: Matthew Humphreys <[email protected]>
1 parent bff5006 commit c558adc

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

cli/options.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,24 @@ func (o *ProjectOptions) GetWorkingDir() (string, error) {
403403
return os.Getwd()
404404
}
405405

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...)
408409
if err != nil {
409410
return nil, err
410411
}
412+
configs := make([][]byte, len(config.ConfigFiles))
411413

412-
var configs []types.ConfigFile
413-
for _, f := range configPaths {
414+
for i, c := range config.ConfigFiles {
415+
var err error
414416
var b []byte
415-
if f == "-" {
417+
if c.Filename == "-" {
416418
b, err = io.ReadAll(os.Stdin)
417419
if err != nil {
418420
return nil, err
419421
}
420422
} else {
421-
f, err := filepath.Abs(f)
423+
f, err := filepath.Abs(c.Filename)
422424
if err != nil {
423425
return nil, err
424426
}
@@ -427,27 +429,31 @@ func (o *ProjectOptions) GetConfigFiles() ([]types.ConfigFile, error) {
427429
return nil, err
428430
}
429431
}
430-
configs = append(configs, types.ConfigFile{
431-
Filename: f,
432-
Content: b,
433-
})
432+
configs[i] = b
434433
}
435-
return configs, err
434+
for i, c := range configs {
435+
config.ConfigFiles[i].Content = c
436+
}
437+
return config, nil
436438
}
437439

438440
// LoadProject loads compose file according to options and bind to types.Project go structs
439441
func (o *ProjectOptions) LoadProject(ctx context.Context) (*types.Project, error) {
440-
configDetails, err := o.prepare()
442+
config, err := o.prepare(ctx)
441443
if err != nil {
442444
return nil, err
443445
}
444446

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...)
446452
if err != nil {
447453
return nil, err
448454
}
449455

450-
for _, config := range configDetails.ConfigFiles {
456+
for _, config := range config.ConfigFiles {
451457
project.ComposeFiles = append(project.ComposeFiles, config.Filename)
452458
}
453459

@@ -456,36 +462,31 @@ func (o *ProjectOptions) LoadProject(ctx context.Context) (*types.Project, error
456462

457463
// LoadModel loads compose file according to options and returns a raw (yaml tree) model
458464
func (o *ProjectOptions) LoadModel(ctx context.Context) (map[string]any, error) {
459-
configDetails, err := o.prepare()
465+
configDetails, err := o.prepare(ctx)
460466
if err != nil {
461467
return nil, err
462468
}
463469

464-
return loader.LoadModelWithContext(ctx, configDetails, o.loadOptions...)
470+
return loader.LoadModelWithContext(ctx, *configDetails, o.loadOptions...)
465471
}
466472

467473
// 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()
470476
if err != nil {
471-
return types.ConfigDetails{}, err
477+
return &types.ConfigDetails{}, err
472478
}
473479

474-
workingDir, err := o.GetWorkingDir()
480+
configDetails, err := o.ReadConfigFiles(ctx, o)
475481
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
483483
}
484484

485485
o.loadOptions = append(o.loadOptions,
486-
withNamePrecedenceLoad(workingDir, o),
486+
withNamePrecedenceLoad(defaultDir, o),
487487
withConvertWindowsPaths(o),
488488
withListeners(o))
489+
489490
return configDetails, nil
490491
}
491492

loader/loader.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,48 @@ func parseYAML(decoder *yaml.Decoder) (map[string]interface{}, PostProcessor, er
300300
return converted.(map[string]interface{}), &processor, nil
301301
}
302302

303+
// LoadConfigFiles ingests config files with ResourceLoader and returns config details with paths to local copies
304+
func LoadConfigFiles(ctx context.Context, configFiles []string, options ...func(*Options)) (*types.ConfigDetails, error) {
305+
if len(configFiles) < 1 {
306+
return &types.ConfigDetails{}, errors.New("no files specified")
307+
}
308+
309+
opts := &Options{}
310+
config := &types.ConfigDetails{
311+
ConfigFiles: make([]types.ConfigFile, len(configFiles)),
312+
}
313+
314+
for _, op := range options {
315+
op(opts)
316+
}
317+
opts.ResourceLoaders = append(opts.ResourceLoaders, localResourceLoader{})
318+
319+
for i, p := range configFiles {
320+
for _, loader := range opts.ResourceLoaders {
321+
if !loader.Accept(p) {
322+
continue
323+
}
324+
local, err := loader.Load(ctx, p)
325+
if err != nil {
326+
continue
327+
}
328+
if config.WorkingDir == "" {
329+
config.WorkingDir = filepath.Dir(local)
330+
331+
}
332+
abs, err := filepath.Abs(local)
333+
if err != nil {
334+
abs = local
335+
}
336+
config.ConfigFiles[i] = types.ConfigFile{
337+
Filename: abs,
338+
}
339+
break
340+
}
341+
}
342+
return config, nil
343+
}
344+
303345
// Load reads a ConfigDetails and returns a fully loaded configuration.
304346
// Deprecated: use LoadWithContext.
305347
func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.Project, error) {

0 commit comments

Comments
 (0)