about summary refs log tree commit diff
path: root/context
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-02-08T10·50+0100
committerVincent Ambo <tazjin@gmail.com>2017-02-08T10·50+0100
commitbb45bfa737896c6f26ef4a816458bbfe508b8280 (patch)
treec6390349a55ace92615399c71459a2d1f757b6ec /context
parentaff2f7ac1de0e285b786b33b1b39905b6ecadd1f (diff)
feat context: Add types and loading functions
Diffstat (limited to 'context')
-rw-r--r--context/context.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/context/context.go b/context/context.go
new file mode 100644
index 000000000000..e842feae1dc5
--- /dev/null
+++ b/context/context.go
@@ -0,0 +1,51 @@
+package context
+
+import (
+	"encoding/json"
+	"github.com/polydawn/meep"
+	"io/ioutil"
+	"path"
+)
+
+type ResourceSet struct {
+	Name   string                 `json:"name"`
+	Values map[string]interface{} `json:"values"`
+}
+
+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
+
+	err = json.Unmarshal(file, &c)
+	if err != nil {
+		return nil, meep.New(
+			&ContextLoadingError{Filename: filename},
+			meep.Cause(err),
+		)
+	}
+
+	c.BaseDir = path.Dir(filename)
+
+	return &c, nil
+}