github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/model/manifest.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "io" 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "strings" 14 15 "gopkg.in/yaml.v2" 16 ) 17 18 type PluginOption struct { 19 // The display name for the option. 20 DisplayName string `json:"display_name" yaml:"display_name"` 21 22 // The string value for the option. 23 Value string `json:"value" yaml:"value"` 24 } 25 26 type PluginSetting struct { 27 // The key that the setting will be assigned to in the configuration file. 28 Key string `json:"key" yaml:"key"` 29 30 // The display name for the setting. 31 DisplayName string `json:"display_name" yaml:"display_name"` 32 33 // The type of the setting. 34 // 35 // "bool" will result in a boolean true or false setting. 36 // 37 // "dropdown" will result in a string setting that allows the user to select from a list of 38 // pre-defined options. 39 // 40 // "generated" will result in a string setting that is set to a random, cryptographically secure 41 // string. 42 // 43 // "radio" will result in a string setting that allows the user to select from a short selection 44 // of pre-defined options. 45 // 46 // "text" will result in a string setting that can be typed in manually. 47 // 48 // "username" will result in a text setting that will autocomplete to a username. 49 Type string `json:"type" yaml:"type"` 50 51 // The help text to display to the user. 52 HelpText string `json:"help_text" yaml:"help_text"` 53 54 // The help text to display alongside the "Regenerate" button for settings of the "generated" type. 55 RegenerateHelpText string `json:"regenerate_help_text,omitempty" yaml:"regenerate_help_text,omitempty"` 56 57 // The placeholder to display for "text", "generated" and "username" types when blank. 58 Placeholder string `json:"placeholder" yaml:"placeholder"` 59 60 // The default value of the setting. 61 Default interface{} `json:"default" yaml:"default"` 62 63 // For "radio" or "dropdown" settings, this is the list of pre-defined options that the user can choose 64 // from. 65 Options []*PluginOption `json:"options,omitempty" yaml:"options,omitempty"` 66 } 67 68 type PluginSettingsSchema struct { 69 // Optional text to display above the settings. 70 Header string `json:"header" yaml:"header"` 71 72 // Optional text to display below the settings. 73 Footer string `json:"footer" yaml:"footer"` 74 75 // A list of setting definitions. 76 Settings []*PluginSetting `json:"settings" yaml:"settings"` 77 } 78 79 // The plugin manifest defines the metadata required to load and present your plugin. The manifest 80 // file should be named plugin.json or plugin.yaml and placed in the top of your 81 // plugin bundle. 82 // 83 // Example plugin.yaml: 84 // 85 // id: com.mycompany.myplugin 86 // name: My Plugin 87 // description: This is my plugin. It does stuff. 88 // server: 89 // executable: myplugin 90 // settings_schema: 91 // settings: 92 // - key: enable_extra_thing 93 // type: bool 94 // display_name: Enable Extra Thing 95 // help_text: When true, an extra thing will be enabled! 96 // default: false 97 type Manifest struct { 98 // The id is a globally unique identifier that represents your plugin. Ids must be at least 99 // 3 characters, at most 190 characters and must match ^[a-zA-Z0-9-_\.]+$. 100 // Reverse-DNS notation using a name you control is a good option, e.g. "com.mycompany.myplugin". 101 Id string `json:"id" yaml:"id"` 102 103 // The name to be displayed for the plugin. 104 Name string `json:"name,omitempty" yaml:"name,omitempty"` 105 106 // A description of what your plugin is and does. 107 Description string `json:"description,omitempty" yaml:"description,omitempty"` 108 109 // A version number for your plugin. Semantic versioning is recommended: http://semver.org 110 Version string `json:"version" yaml:"version"` 111 112 // Server defines the server-side portion of your plugin. 113 Server *ManifestServer `json:"server,omitempty" yaml:"server,omitempty"` 114 115 // Backend is a deprecated flag for defining the server-side portion of your plugin. Going forward, use Server instead. 116 Backend *ManifestServer `json:"backend,omitempty" yaml:"backend,omitempty"` 117 118 // If your plugin extends the web app, you'll need to define webapp. 119 Webapp *ManifestWebapp `json:"webapp,omitempty" yaml:"webapp,omitempty"` 120 121 // To allow administrators to configure your plugin via the Mattermost system console, you can 122 // provide your settings schema. 123 SettingsSchema *PluginSettingsSchema `json:"settings_schema,omitempty" yaml:"settings_schema,omitempty"` 124 } 125 126 type ManifestServer struct { 127 // Executables are the paths to your executable binaries, specifying multiple entry points 128 // for different platforms when bundled together in a single plugin. 129 Executables *ManifestExecutables `json:"executables,omitempty" yaml:"executables,omitempty"` 130 131 // Executable is the path to your executable binary. This should be relative to the root 132 // of your bundle and the location of the manifest file. 133 // 134 // On Windows, this file must have a ".exe" extension. 135 // 136 // If your plugin is compiled for multiple platforms, consider bundling them together 137 // and using the Executables field instead. 138 Executable string `json:"executable" yaml:"executable"` 139 } 140 141 type ManifestExecutables struct { 142 // LinuxAmd64 is the path to your executable binary for the corresponding platform 143 LinuxAmd64 string `json:"linux-amd64,omitempty" yaml:"linux-amd64,omitempty"` 144 // DarwinAmd64 is the path to your executable binary for the corresponding platform 145 DarwinAmd64 string `json:"darwin-amd64,omitempty" yaml:"darwin-amd64,omitempty"` 146 // WindowsAmd64 is the path to your executable binary for the corresponding platform 147 // This file must have a ".exe" extension 148 WindowsAmd64 string `json:"windows-amd64,omitempty" yaml:"windows-amd64,omitempty"` 149 } 150 151 type ManifestWebapp struct { 152 // The path to your webapp bundle. This should be relative to the root of your bundle and the 153 // location of the manifest file. 154 BundlePath string `json:"bundle_path" yaml:"bundle_path"` 155 156 // BundleHash is the 64-bit FNV-1a hash of the webapp bundle, computed when the plugin is loaded 157 BundleHash []byte `json:"-"` 158 } 159 160 func (m *Manifest) ToJson() string { 161 b, _ := json.Marshal(m) 162 return string(b) 163 } 164 165 func ManifestListToJson(m []*Manifest) string { 166 b, _ := json.Marshal(m) 167 return string(b) 168 } 169 170 func ManifestFromJson(data io.Reader) *Manifest { 171 var m *Manifest 172 json.NewDecoder(data).Decode(&m) 173 return m 174 } 175 176 func ManifestListFromJson(data io.Reader) []*Manifest { 177 var manifests []*Manifest 178 json.NewDecoder(data).Decode(&manifests) 179 return manifests 180 } 181 182 func (m *Manifest) HasClient() bool { 183 return m.Webapp != nil 184 } 185 186 func (m *Manifest) ClientManifest() *Manifest { 187 cm := new(Manifest) 188 *cm = *m 189 cm.Name = "" 190 cm.Description = "" 191 cm.Server = nil 192 if cm.Webapp != nil { 193 cm.Webapp = new(ManifestWebapp) 194 *cm.Webapp = *m.Webapp 195 cm.Webapp.BundlePath = "/static/" + m.Id + "/" + fmt.Sprintf("%s_%x_bundle.js", m.Id, m.Webapp.BundleHash) 196 } 197 return cm 198 } 199 200 // GetExecutableForRuntime returns the path to the executable for the given runtime architecture. 201 // 202 // If the manifest defines multiple executables, but none match, or if only a single executable 203 // is defined, the Executable field will be returned. This method does not guarantee that the 204 // resulting binary can actually execute on the given platform. 205 func (m *Manifest) GetExecutableForRuntime(goOs, goArch string) string { 206 server := m.Server 207 208 // Support the deprecated backend parameter. 209 if server == nil { 210 server = m.Backend 211 } 212 213 if server == nil { 214 return "" 215 } 216 217 var executable string 218 if server.Executables != nil { 219 if goOs == "linux" && goArch == "amd64" { 220 executable = server.Executables.LinuxAmd64 221 } else if goOs == "darwin" && goArch == "amd64" { 222 executable = server.Executables.DarwinAmd64 223 } else if goOs == "windows" && goArch == "amd64" { 224 executable = server.Executables.WindowsAmd64 225 } 226 } 227 228 if executable == "" { 229 executable = server.Executable 230 } 231 232 return executable 233 } 234 235 func (m *Manifest) HasServer() bool { 236 return m.Server != nil || m.Backend != nil 237 } 238 239 func (m *Manifest) HasWebapp() bool { 240 return m.Webapp != nil 241 } 242 243 // FindManifest will find and parse the manifest in a given directory. 244 // 245 // In all cases other than a does-not-exist error, path is set to the path of the manifest file that was 246 // found. 247 // 248 // Manifests are JSON or YAML files named plugin.json, plugin.yaml, or plugin.yml. 249 func FindManifest(dir string) (manifest *Manifest, path string, err error) { 250 for _, name := range []string{"plugin.yml", "plugin.yaml"} { 251 path = filepath.Join(dir, name) 252 f, ferr := os.Open(path) 253 if ferr != nil { 254 if !os.IsNotExist(ferr) { 255 err = ferr 256 return 257 } 258 continue 259 } 260 b, ioerr := ioutil.ReadAll(f) 261 f.Close() 262 if ioerr != nil { 263 err = ioerr 264 return 265 } 266 var parsed Manifest 267 err = yaml.Unmarshal(b, &parsed) 268 if err != nil { 269 return 270 } 271 manifest = &parsed 272 manifest.Id = strings.ToLower(manifest.Id) 273 return 274 } 275 276 path = filepath.Join(dir, "plugin.json") 277 f, ferr := os.Open(path) 278 if ferr != nil { 279 if os.IsNotExist(ferr) { 280 path = "" 281 } 282 err = ferr 283 return 284 } 285 defer f.Close() 286 var parsed Manifest 287 err = json.NewDecoder(f).Decode(&parsed) 288 if err != nil { 289 return 290 } 291 manifest = &parsed 292 manifest.Id = strings.ToLower(manifest.Id) 293 return 294 }