Skip to content

Commit

Permalink
Add support for ~ homedir expansion while loading contexts from disk.
Browse files Browse the repository at this point in the history
Return contexts by value instead of reference.
Error messages clean-up.
  • Loading branch information
silphid committed May 19, 2021
1 parent 3c0c504 commit 8b02a95
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/internal/contextFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -53,29 +54,33 @@ func readContextFileFromWorkingDirectory() ([]byte, error) {

// readContextFileFromFilePath reads the contextfile from the fs
func readContextFileFromFilePath(path string) ([]byte, error) {
path, err := homedir.Expand(path)
if err != nil {
return nil, err
}
return os.ReadFile(path)
}

// readContextFileFromNetwork reads the contextfile from the network over http.
func readContextFileFromNetwork(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed request: %w", err)
return nil, fmt.Errorf("failed fetching context file from network: %w", err)
}
defer resp.Body.Close()

return io.ReadAll(resp.Body)
}

// parseContextFile unmarshals the contextFile data and resolves any parent contextfiles
func parseContextFile(data []byte) (*Contexts, error) {
func parseContextFile(data []byte) (Contexts, error) {
var ctxFile ContextFile
if err := yaml.Unmarshal(data, &ctxFile); err != nil {
return nil, fmt.Errorf("failed to decode context file: %w", err)
return Contexts{}, fmt.Errorf("failed to decode context file: %w", err)
}

if ctxFile.Version != currentVersion {
return nil, fmt.Errorf("unsupported context file version")
return Contexts{}, fmt.Errorf("unsupported context file version")
}

contexts := Contexts{
Expand All @@ -86,17 +91,17 @@ func parseContextFile(data []byte) (*Contexts, error) {
if ctxFile.Parent != "" {
parent, err := readAndParseContextFile(ctxFile.Parent)
if err != nil {
return nil, fmt.Errorf("failed to resolve parent contexts : %s : %w", ctxFile.Parent, err)
return Contexts{}, fmt.Errorf("failed to resolve parent context %q: %w", ctxFile.Parent, err)
}
contexts = parent.Merge(contexts)
}

return &contexts, nil
return contexts, nil
}

// readAndParseContextFile reads and parses the context file from a path. If empty will work from current working directory
// looking for default .yeyrc.yaml file, if starts with https: will download from network. Otherwise searches path in filesystem
func readAndParseContextFile(path string) (*Contexts, error) {
func readAndParseContextFile(path string) (Contexts, error) {
var bytes []byte
var err error

Expand All @@ -109,14 +114,14 @@ func readAndParseContextFile(path string) (*Contexts, error) {
}

if err != nil {
return nil, fmt.Errorf("failed to read contextfile: %w", err)
return Contexts{}, fmt.Errorf("failed to read contextfile: %w", err)
}

return parseContextFile(bytes)
}

// ReadAndParseContextFile reads the context file and returns the contexts. It starts by reading from current working directory
// and resolves all parent context files
func ReadAndParseContextFile() (*Contexts, error) {
func ReadAndParseContextFile() (Contexts, error) {
return readAndParseContextFile("")
}

0 comments on commit 8b02a95

Please sign in to comment.