summaryrefslogtreecommitdiffstats
path: root/model/manifest.go
blob: 50115398283b98dc9a7583d4c39ece1fac9be3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"

	"gopkg.in/yaml.v2"
)

const (
	PLUGIN_CONFIG_TYPE_TEXT      = "text"
	PLUGIN_CONFIG_TYPE_BOOL      = "bool"
	PLUGIN_CONFIG_TYPE_RADIO     = "radio"
	PLUGIN_CONFIG_TYPE_DROPDOWN  = "dropdown"
	PLUGIN_CONFIG_TYPE_GENERATED = "generated"
)

type PluginOption struct {
	DisplayName string `json:"display_name" yaml:"display_name"`
	Value       string `json:"value" yaml:"value"`
}

type PluginSetting struct {
	DisplayName        string          `json:"display_name" yaml:"display_name"`
	Type               string          `json:"type" yaml:"type"`
	HelpText           string          `json:"help_text" yaml:"help_text"`
	RegenerateHelpText string          `json:"regenerate_help_text,omitempty" yaml:"regenerate_help_text,omitempty"`
	Default            interface{}     `json:"default" yaml:"default"`
	Options            []*PluginOption `json:"options,omitempty" yaml:"options,omitempty"`
}

type PluginSettingsSchema struct {
	Header   string                    `json:"header" yaml:"header"`
	Footer   string                    `json:"footer" yaml:"footer"`
	Settings map[string]*PluginSetting `json:"settings" yaml:"settings"`
}

type Manifest struct {
	Id             string                `json:"id" yaml:"id"`
	Name           string                `json:"name,omitempty" yaml:"name,omitempty"`
	Description    string                `json:"description,omitempty" yaml:"description,omitempty"`
	Version        string                `json:"version" yaml:"version"`
	Backend        *ManifestBackend      `json:"backend,omitempty" yaml:"backend,omitempty"`
	Webapp         *ManifestWebapp       `json:"webapp,omitempty" yaml:"webapp,omitempty"`
	SettingsSchema *PluginSettingsSchema `json:"settings_schema,omitempty" yaml:"settings_schema,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
	}
}

func (m *Manifest) HasClient() bool {
	return m.Webapp != nil
}

func (m *Manifest) ClientManifest() *Manifest {
	cm := new(Manifest)
	*cm = *m
	cm.Name = ""
	cm.Description = ""
	cm.Backend = nil
	return cm
}

// 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
}