Skip to content

Commit

Permalink
feat: Add new error check for empty configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Matovidlo committed Dec 18, 2024
1 parent dbde931 commit 1b8582e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 11 additions & 2 deletions internal/pkg/template/manifest/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func evaluateFile(ctx context.Context, file *filesystem.RawFile, jsonnetCtx *jso
}

content := newFile()
// Decode string and set Configs object
if err := json.DecodeString(jsonContent, content); err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,17 +87,25 @@ func (f *file) validate(ctx context.Context) error {
return nil
}

func (f *file) records() []model.ObjectManifest {
func (f *file) records() (oms []model.ObjectManifest, err error) {
out := make([]model.ObjectManifest, 0, len(f.Configs))
for _, config := range f.Configs {
if config == nil {
continue
}

out = append(out, &config.ConfigManifest)
for _, row := range config.Rows {
row.ComponentID = config.ComponentID
row.ConfigID = config.ID
out = append(out, row)
}
}
return out
if len(out) == 0 {
return nil, errors.New("unable to create template using invalid manifest configuration")
}

return out, nil
}

func (f *file) setRecords(records []model.ObjectManifest) {
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/template/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,14 @@ func (f *File) Evaluate(ctx context.Context, jsonnetCtx *jsonnet.Context) (*Mani
// Create manifest
m := New()

// Get records
records, err := content.records()
if err != nil {
return nil, errors.Errorf(`cannot load configurations from manifest "%s": %w`, f.file.Path(), err)
}

// Set records
if err := m.records.SetRecords(content.records()); err != nil {
if err := m.records.SetRecords(records); err != nil {
return nil, errors.Errorf(`cannot load manifest: %w`, err)
}

Expand Down

0 comments on commit 1b8582e

Please sign in to comment.