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