summaryrefslogtreecommitdiffstats
path: root/model/manifest.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-09-01 09:00:27 -0400
committerGitHub <noreply@github.com>2017-09-01 09:00:27 -0400
commit899ab31fff9b34bc125faf75b79a89e390deb2cf (patch)
tree41dc5832268504e54a0b2188eedcf89b7828dd12 /model/manifest.go
parent74b5e52c4eb54000dcb5a7b46c0977d732bce80f (diff)
downloadchat-899ab31fff9b34bc125faf75b79a89e390deb2cf.tar.gz
chat-899ab31fff9b34bc125faf75b79a89e390deb2cf.tar.bz2
chat-899ab31fff9b34bc125faf75b79a89e390deb2cf.zip
Implement experimental REST API endpoints for plugins (#7279)
* Implement experimental REST API endpoints for plugins * Updates per feedback and rebase * Update tests * Further updates * Update extraction of plugins * Use OS temp dir for plugins instead of search path * Fail extraction on paths that attempt to traverse upward * Update pluginenv ActivePlugins()
Diffstat (limited to 'model/manifest.go')
-rw-r--r--model/manifest.go118
1 files changed, 118 insertions, 0 deletions
diff --git a/model/manifest.go b/model/manifest.go
new file mode 100644
index 000000000..e61ccc8ad
--- /dev/null
+++ b/model/manifest.go
@@ -0,0 +1,118 @@
+package model
+
+import (
+ "encoding/json"
+ "io"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+
+ "gopkg.in/yaml.v2"
+)
+
+type Manifest struct {
+ Id string `json:"id" yaml:"id"`
+ Name string `json:"name" yaml:"name"`
+ Description string `json:"description" yaml:"description"`
+ Backend *ManifestBackend `json:"backend,omitempty" yaml:"backend,omitempty"`
+ Webapp *ManifestWebapp `json:"webapp,omitempty" yaml:"webapp,omitempty"`
+}
+
+type ManifestBackend struct {
+ Executable string `json:"executable" yaml:"executable"`
+}
+
+type ManifestWebapp struct {
+ BundlePath string `json:"bundle_path" yaml:"bundle_path"`
+}
+
+func (m *Manifest) ToJson() string {
+ b, err := json.Marshal(m)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
+func ManifestListToJson(m []*Manifest) string {
+ b, err := json.Marshal(m)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
+func ManifestFromJson(data io.Reader) *Manifest {
+ decoder := json.NewDecoder(data)
+ var m Manifest
+ err := decoder.Decode(&m)
+ if err == nil {
+ return &m
+ } else {
+ return nil
+ }
+}
+
+func ManifestListFromJson(data io.Reader) []*Manifest {
+ decoder := json.NewDecoder(data)
+ var manifests []*Manifest
+ err := decoder.Decode(&manifests)
+ if err == nil {
+ return manifests
+ } else {
+ return nil
+ }
+}
+
+// FindManifest will find and parse the manifest in a given directory.
+//
+// In all cases other than a does-not-exist error, path is set to the path of the manifest file that was
+// found.
+//
+// Manifests are JSON or YAML files named plugin.json, plugin.yaml, or plugin.yml.
+func FindManifest(dir string) (manifest *Manifest, path string, err error) {
+ for _, name := range []string{"plugin.yml", "plugin.yaml"} {
+ path = filepath.Join(dir, name)
+ f, ferr := os.Open(path)
+ if ferr != nil {
+ if !os.IsNotExist(ferr) {
+ err = ferr
+ return
+ }
+ continue
+ }
+ b, ioerr := ioutil.ReadAll(f)
+ f.Close()
+ if ioerr != nil {
+ err = ioerr
+ return
+ }
+ var parsed Manifest
+ err = yaml.Unmarshal(b, &parsed)
+ if err != nil {
+ return
+ }
+ manifest = &parsed
+ return
+ }
+
+ path = filepath.Join(dir, "plugin.json")
+ f, ferr := os.Open(path)
+ if ferr != nil {
+ if os.IsNotExist(ferr) {
+ path = ""
+ }
+ err = ferr
+ return
+ }
+ defer f.Close()
+ var parsed Manifest
+ err = json.NewDecoder(f).Decode(&parsed)
+ if err != nil {
+ return
+ }
+ manifest = &parsed
+ return
+}