Skip to content

Commit

Permalink
Moving loadFileFromPathSpec to PathSpec.LoadContents
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jun 17, 2024
1 parent 7ba5b47 commit a01c29a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
38 changes: 12 additions & 26 deletions pkg/lib/template/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,18 @@ package template
import (
"encoding/json"
"fmt"
"io"

"github.com/programmfabrik/apitest/pkg/lib/util"
)

func loadFileFromPathSpec(rawPathSpec, manifestDir string) (string, []byte, error) {
pathSpec, err := util.ParsePathSpec(rawPathSpec)
if err != nil {
return "", nil, fmt.Errorf("error parsing path spec: %w", err)
}

requestFile, err := util.OpenFileOrUrl(pathSpec.Path, manifestDir)
if err != nil {
return "", nil, fmt.Errorf("error opening path: %s", err)
}

defer requestFile.Close()
requestTmpl, err := io.ReadAll(requestFile)

if err != nil {
return "", nil, fmt.Errorf("error loading file %s: %s", requestFile, err)
}

return pathSpec.Path, requestTmpl, nil
}

func LoadManifestDataAsObject(data any, manifestDir string, loader Loader) (filepath string, res any, err error) {
switch typedData := data.(type) {
case string:
filepath, requestTmpl, err := loadFileFromPathSpec(typedData, manifestDir)
pathSpec, err := util.ParsePathSpec(typedData)
if err != nil {
return "", res, fmt.Errorf("error parsing pathSpec: %w", err)
}
requestTmpl, err := pathSpec.LoadContents(manifestDir)
if err != nil {
return "", res, fmt.Errorf("error loading fileFromPathSpec: %s", err)
}
Expand All @@ -53,7 +35,7 @@ func LoadManifestDataAsObject(data any, manifestDir string, loader Loader) (file
}
return "", res, fmt.Errorf("error unmarshalling: %s", err)
}
return filepath, jsonObject, nil
return pathSpec.Path, jsonObject, nil
case util.JsonObject:
return "", typedData, nil
case util.JsonArray:
Expand All @@ -69,11 +51,15 @@ func LoadManifestDataAsRawJson(data any, manifestDir string) (filepath string, r
err = res.UnmarshalJSON(typedData)
return
case string:
filepath, res, err := loadFileFromPathSpec(typedData, manifestDir)
pathSpec, err := util.ParsePathSpec(typedData)
if err != nil {
return "", res, fmt.Errorf("error parsing pathSpec: %w", err)
}
res, err := pathSpec.LoadContents(manifestDir)
if err != nil {
return "", res, fmt.Errorf("error loading fileFromPathSpec: %s", err)
}
return filepath, res, nil
return pathSpec.Path, res, nil
case util.JsonObject, util.JsonArray:
jsonMar, err := json.Marshal(typedData)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/lib/util/path_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"io"
"strconv"
"strings"
)
Expand Down Expand Up @@ -51,3 +52,19 @@ func IsPathSpec(s string) bool {
_, err := ParsePathSpec(s)
return err == nil
}

// Load loads the contents of the file pointed to by the PathSpec into a byte array.
func (ps PathSpec) LoadContents(manifestDir string) ([]byte, error) {
requestFile, err := OpenFileOrUrl(ps.Path, manifestDir)
if err != nil {
return nil, fmt.Errorf("error opening path: %w", err)
}
defer requestFile.Close()

contents, err := io.ReadAll(requestFile)
if err != nil {
return nil, fmt.Errorf("error loading file at %q: %w", ps, err)
}

return contents, nil
}

0 comments on commit a01c29a

Please sign in to comment.