2017-02-08 11:50:39 +01:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-02-08 13:11:10 +01:00
|
|
|
"fmt"
|
2017-02-08 11:50:39 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2017-02-08 13:11:10 +01:00
|
|
|
"strings"
|
2017-02-08 12:58:53 +01:00
|
|
|
|
2017-02-08 13:11:10 +01:00
|
|
|
"github.com/ghodss/yaml"
|
2017-02-08 12:58:53 +01:00
|
|
|
"github.com/polydawn/meep"
|
2017-02-08 11:50:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ResourceSet struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Values map[string]interface{} `json:"values"`
|
2017-02-08 17:14:40 +01:00
|
|
|
|
|
|
|
// Fields for resource set collections
|
|
|
|
Include []ResourceSet `json:"include"`
|
2017-02-08 17:32:19 +01:00
|
|
|
Parent string
|
2017-02-08 11:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
Name string `json:"context"`
|
|
|
|
Global map[string]interface{} `json:"global"`
|
|
|
|
ResourceSets []ResourceSet `json:"include"`
|
|
|
|
BaseDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContextLoadingError struct {
|
|
|
|
meep.AllTraits
|
|
|
|
Filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to load and deserialise a Context from the specified file.
|
|
|
|
func LoadContextFromFile(filename string) (*Context, error) {
|
|
|
|
file, err := ioutil.ReadFile(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, meep.New(
|
|
|
|
&ContextLoadingError{Filename: filename},
|
|
|
|
meep.Cause(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Context
|
|
|
|
|
2017-02-08 13:11:10 +01:00
|
|
|
if strings.HasSuffix(filename, "json") {
|
|
|
|
err = json.Unmarshal(file, &c)
|
|
|
|
} else if strings.HasSuffix(filename, "yaml") || strings.HasSuffix(filename, "yml") {
|
|
|
|
err = yaml.Unmarshal(file, &c)
|
|
|
|
} else {
|
|
|
|
return nil, meep.New(
|
|
|
|
&ContextLoadingError{Filename: filename},
|
|
|
|
meep.Cause(fmt.Errorf("File format not supported. Must be JSON or YAML.")),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-02-08 11:50:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, meep.New(
|
|
|
|
&ContextLoadingError{Filename: filename},
|
|
|
|
meep.Cause(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:32:19 +01:00
|
|
|
c.ResourceSets = *flattenResourceSetCollections(&c.ResourceSets)
|
2017-02-08 11:50:39 +01:00
|
|
|
c.BaseDir = path.Dir(filename)
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
2017-02-08 17:14:40 +01:00
|
|
|
|
|
|
|
// Flattens resource set collections, i.e. resource sets that themselves have an additional 'include' field set.
|
|
|
|
// Those will be regarded as a short-hand for including multiple resource sets from a subfolder.
|
|
|
|
// See https://github.com/tazjin/kontemplate/issues/9 for more information.
|
|
|
|
func flattenResourceSetCollections(rs *[]ResourceSet) *[]ResourceSet {
|
|
|
|
flattened := make([]ResourceSet, 0)
|
|
|
|
|
|
|
|
for _, r := range *rs {
|
|
|
|
if len(r.Include) == 0 {
|
|
|
|
flattened = append(flattened, r)
|
|
|
|
} else {
|
|
|
|
for _, subResourceSet := range r.Include {
|
2017-02-08 17:32:19 +01:00
|
|
|
subResourceSet.Parent = r.Name
|
2017-02-08 17:21:55 +01:00
|
|
|
subResourceSet.Name = path.Join(r.Name, subResourceSet.Name)
|
2017-02-08 17:14:40 +01:00
|
|
|
flattened = append(flattened, subResourceSet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &flattened
|
|
|
|
}
|