diff options
author | Vincent Ambo <tazjin@gmail.com> | 2018-06-09T18·14+0200 |
---|---|---|
committer | Vincent Ambo <github@tazj.in> | 2018-06-09T18·21+0200 |
commit | ae6d960df941c3980c91e90e940b50ad59697eb7 (patch) | |
tree | e0166d016c855287c729540a26b4b305771c739e | |
parent | 141355f3505237b11480775fb83b5ba1bfcf7806 (diff) |
feat(context): Support loading import variables from absolute paths
This lets users specify the paths from which to import additional variables using absolute paths in addition to relative paths. This enables both loading of configuration files placed outside of the resource set folder (if desired), as well as special use-cases such as specifying `/dev/stdin` as an input path to read variables from standard input. This change supersedes #131
-rw-r--r-- | context/context.go | 15 | ||||
-rw-r--r-- | context/context_test.go | 16 |
2 files changed, 20 insertions, 11 deletions
diff --git a/context/context.go b/context/context.go index 42ad2e8c18c8..237d51435e91 100644 --- a/context/context.go +++ b/context/context.go @@ -65,7 +65,7 @@ func contextLoadingError(filename string, cause error) error { // Attempt to load and deserialise a Context from the specified file. func LoadContext(filename string, explicitVars *[]string) (*Context, error) { var ctx Context - err := util.LoadJsonOrYaml(filename, &ctx) + err := util.LoadData(filename, &ctx) if err != nil { return nil, contextLoadingError(filename, err) @@ -105,8 +105,17 @@ func (ctx *Context) loadImportedVariables() (map[string]interface{}, error) { allImportedVars := make(map[string]interface{}) for _, file := range ctx.VariableImportFiles { + // Ensure that the filename is not merged with the baseDir if + // it is set to an absolute path. + var filePath string + if path.IsAbs(file) { + filePath = file + } else { + filePath = path.Join(ctx.BaseDir, file) + } + var importedVars map[string]interface{} - err := util.LoadJsonOrYaml(path.Join(ctx.BaseDir, file), &importedVars) + err := util.LoadData(filePath, &importedVars) if err != nil { return nil, err @@ -176,7 +185,7 @@ func loadDefaultValues(rs *ResourceSet, c *Context) *map[string]interface{} { var defaultVars map[string]interface{} for _, filename := range util.DefaultFilenames { - err := util.LoadJsonOrYaml(path.Join(c.BaseDir, rs.Path, filename), &defaultVars) + err := util.LoadData(path.Join(c.BaseDir, rs.Path, filename), &defaultVars) if err == nil { return util.Merge(&defaultVars, &rs.Values) } diff --git a/context/context_test.go b/context/context_test.go index 6a4cec6c9383..0af9dfc1c22d 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -43,7 +43,7 @@ func TestLoadFlatContextFromFile(t *testing.T) { Parent: "", }, }, - BaseDir: "testdata", + BaseDir: "testdata", ImportedVars: make(map[string]interface{}, 0), ExplicitVars: make(map[string]interface{}, 0), } @@ -84,14 +84,14 @@ func TestLoadContextWithResourceSetCollections(t *testing.T) { Name: "collection/nested", Path: "collection/nested", Values: map[string]interface{}{ - "lizards": "good", + "lizards": "good", "globalVar": "lizards", }, Include: nil, Parent: "collection", }, }, - BaseDir: "testdata", + BaseDir: "testdata", ImportedVars: make(map[string]interface{}, 0), ExplicitVars: make(map[string]interface{}, 0), } @@ -125,7 +125,7 @@ func TestSubresourceVariableInheritance(t *testing.T) { Parent: "parent", }, }, - BaseDir: "testdata", + BaseDir: "testdata", ImportedVars: make(map[string]interface{}, 0), ExplicitVars: make(map[string]interface{}, 0), } @@ -157,7 +157,7 @@ func TestSubresourceVariableInheritanceOverride(t *testing.T) { Parent: "parent", }, }, - BaseDir: "testdata", + BaseDir: "testdata", ImportedVars: make(map[string]interface{}, 0), ExplicitVars: make(map[string]interface{}, 0), } @@ -221,7 +221,7 @@ func TestValuesOverride(t *testing.T) { "artist": "Pallida", "track": "Tractor Beam", }, - "place": "Oslo", + "place": "Oslo", "globalVar": "very global!", } @@ -260,7 +260,7 @@ func TestExplicitPathLoading(t *testing.T) { Parent: "", }, }, - BaseDir: "testdata", + BaseDir: "testdata", ImportedVars: make(map[string]interface{}, 0), ExplicitVars: make(map[string]interface{}, 0), } @@ -288,7 +288,7 @@ func TestExplicitSubresourcePathLoading(t *testing.T) { Values: make(map[string]interface{}, 0), }, }, - BaseDir: "testdata", + BaseDir: "testdata", ImportedVars: make(map[string]interface{}, 0), ExplicitVars: make(map[string]interface{}, 0), } |