github.com/vnforks/kid@v5.11.1+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.json:
    88  //
    89  //
    90  //    {
    91  //      "id": "com.mycompany.myplugin",
    92  //      "name": "My Plugin",
    93  //      "description": "This is my plugin",
    94  //      "version": "0.1.0",
    95  //      "min_server_version": "5.6.0",
    96  //      "server": {
    97  //        "executables": {
    98  //          "linux-amd64": "server/dist/plugin-linux-amd64",
    99  //          "darwin-amd64": "server/dist/plugin-darwin-amd64",
   100  //          "windows-amd64": "server/dist/plugin-windows-amd64.exe"
   101  //        }
   102  //      },
   103  //      "webapp": {
   104  //          "bundle_path": "webapp/dist/main.js"
   105  //      },
   106  //      "settings_schema": {
   107  //        "header": "Some header text",
   108  //        "footer": "Some footer text",
   109  //        "settings": [{
   110  //          "key": "someKey",
   111  //          "display_name": "Enable Extra Feature",
   112  //          "type": "bool",
   113  //          "help_text": "When true, an extra feature will be enabled!",
   114  //          "default": "false"
   115  //        }]
   116  //      },
   117  //      "props": {
   118  //        "someKey": "someData"
   119  //      }
   120  //    }
   121  type Manifest struct {
   122  	// The id is a globally unique identifier that represents your plugin. Ids must be at least
   123  	// 3 characters, at most 190 characters and must match ^[a-zA-Z0-9-_\.]+$.
   124  	// Reverse-DNS notation using a name you control is a good option, e.g. "com.mycompany.myplugin".
   125  	Id string `json:"id" yaml:"id"`
   126  
   127  	// The name to be displayed for the plugin.
   128  	Name string `json:"name,omitempty" yaml:"name,omitempty"`
   129  
   130  	// A description of what your plugin is and does.
   131  	Description string `json:"description,omitempty" yaml:"description,omitempty"`
   132  
   133  	// A version number for your plugin. Semantic versioning is recommended: http://semver.org
   134  	Version string `json:"version" yaml:"version"`
   135  
   136  	// The minimum Mattermost server version required for your plugin.
   137  	//
   138  	// Minimum server version: 5.6
   139  	MinServerVersion string `json:"min_server_version,omitempty" yaml:"min_server_version,omitempty"`
   140  
   141  	// Server defines the server-side portion of your plugin.
   142  	Server *ManifestServer `json:"server,omitempty" yaml:"server,omitempty"`
   143  
   144  	// Backend is a deprecated flag for defining the server-side portion of your plugin. Going forward, use Server instead.
   145  	Backend *ManifestServer `json:"backend,omitempty" yaml:"backend,omitempty"`
   146  
   147  	// If your plugin extends the web app, you'll need to define webapp.
   148  	Webapp *ManifestWebapp `json:"webapp,omitempty" yaml:"webapp,omitempty"`
   149  
   150  	// To allow administrators to configure your plugin via the Mattermost system console, you can
   151  	// provide your settings schema.
   152  	SettingsSchema *PluginSettingsSchema `json:"settings_schema,omitempty" yaml:"settings_schema,omitempty"`
   153  
   154  	// Plugins can store any kind of data in Props to allow other plugins to use it.
   155  	Props map[string]interface{} `json:"props,omitempty" yaml:"props,omitempty"`
   156  }
   157  
   158  type ManifestServer struct {
   159  	// Executables are the paths to your executable binaries, specifying multiple entry points
   160  	// for different platforms when bundled together in a single plugin.
   161  	Executables *ManifestExecutables `json:"executables,omitempty" yaml:"executables,omitempty"`
   162  
   163  	// Executable is the path to your executable binary. This should be relative to the root
   164  	// of your bundle and the location of the manifest file.
   165  	//
   166  	// On Windows, this file must have a ".exe" extension.
   167  	//
   168  	// If your plugin is compiled for multiple platforms, consider bundling them together
   169  	// and using the Executables field instead.
   170  	Executable string `json:"executable" yaml:"executable"`
   171  }
   172  
   173  type ManifestExecutables struct {
   174  	// LinuxAmd64 is the path to your executable binary for the corresponding platform
   175  	LinuxAmd64 string `json:"linux-amd64,omitempty" yaml:"linux-amd64,omitempty"`
   176  	// DarwinAmd64 is the path to your executable binary for the corresponding platform
   177  	DarwinAmd64 string `json:"darwin-amd64,omitempty" yaml:"darwin-amd64,omitempty"`
   178  	// WindowsAmd64 is the path to your executable binary for the corresponding platform
   179  	// This file must have a ".exe" extension
   180  	WindowsAmd64 string `json:"windows-amd64,omitempty" yaml:"windows-amd64,omitempty"`
   181  }
   182  
   183  type ManifestWebapp struct {
   184  	// The path to your webapp bundle. This should be relative to the root of your bundle and the
   185  	// location of the manifest file.
   186  	BundlePath string `json:"bundle_path" yaml:"bundle_path"`
   187  
   188  	// BundleHash is the 64-bit FNV-1a hash of the webapp bundle, computed when the plugin is loaded
   189  	BundleHash []byte `json:"-"`
   190  }
   191  
   192  func (m *Manifest) ToJson() string {
   193  	b, _ := json.Marshal(m)
   194  	return string(b)
   195  }
   196  
   197  func ManifestListToJson(m []*Manifest) string {
   198  	b, _ := json.Marshal(m)
   199  	return string(b)
   200  }
   201  
   202  func ManifestFromJson(data io.Reader) *Manifest {
   203  	var m *Manifest
   204  	json.NewDecoder(data).Decode(&m)
   205  	return m
   206  }
   207  
   208  func ManifestListFromJson(data io.Reader) []*Manifest {
   209  	var manifests []*Manifest
   210  	json.NewDecoder(data).Decode(&manifests)
   211  	return manifests
   212  }
   213  
   214  func (m *Manifest) HasClient() bool {
   215  	return m.Webapp != nil
   216  }
   217  
   218  func (m *Manifest) ClientManifest() *Manifest {
   219  	cm := new(Manifest)
   220  	*cm = *m
   221  	cm.Name = ""
   222  	cm.Description = ""
   223  	cm.Server = nil
   224  	if cm.Webapp != nil {
   225  		cm.Webapp = new(ManifestWebapp)
   226  		*cm.Webapp = *m.Webapp
   227  		cm.Webapp.BundlePath = "/static/" + m.Id + "/" + fmt.Sprintf("%s_%x_bundle.js", m.Id, m.Webapp.BundleHash)
   228  	}
   229  	return cm
   230  }
   231  
   232  // GetExecutableForRuntime returns the path to the executable for the given runtime architecture.
   233  //
   234  // If the manifest defines multiple executables, but none match, or if only a single executable
   235  // is defined, the Executable field will be returned. This method does not guarantee that the
   236  // resulting binary can actually execute on the given platform.
   237  func (m *Manifest) GetExecutableForRuntime(goOs, goArch string) string {
   238  	server := m.Server
   239  
   240  	// Support the deprecated backend parameter.
   241  	if server == nil {
   242  		server = m.Backend
   243  	}
   244  
   245  	if server == nil {
   246  		return ""
   247  	}
   248  
   249  	var executable string
   250  	if server.Executables != nil {
   251  		if goOs == "linux" && goArch == "amd64" {
   252  			executable = server.Executables.LinuxAmd64
   253  		} else if goOs == "darwin" && goArch == "amd64" {
   254  			executable = server.Executables.DarwinAmd64
   255  		} else if goOs == "windows" && goArch == "amd64" {
   256  			executable = server.Executables.WindowsAmd64
   257  		}
   258  	}
   259  
   260  	if executable == "" {
   261  		executable = server.Executable
   262  	}
   263  
   264  	return executable
   265  }
   266  
   267  func (m *Manifest) HasServer() bool {
   268  	return m.Server != nil || m.Backend != nil
   269  }
   270  
   271  func (m *Manifest) HasWebapp() bool {
   272  	return m.Webapp != nil
   273  }
   274  
   275  func (m *Manifest) MeetMinServerVersion(serverVersion string) (bool, error) {
   276  	minServerVersion, err := semver.Parse(m.MinServerVersion)
   277  	if err != nil {
   278  		return false, errors.New("failed to parse MinServerVersion")
   279  	}
   280  	sv := semver.MustParse(serverVersion)
   281  	if sv.LT(minServerVersion) {
   282  		return false, nil
   283  	}
   284  	return true, nil
   285  }
   286  
   287  // FindManifest will find and parse the manifest in a given directory.
   288  //
   289  // In all cases other than a does-not-exist error, path is set to the path of the manifest file that was
   290  // found.
   291  //
   292  // Manifests are JSON or YAML files named plugin.json, plugin.yaml, or plugin.yml.
   293  func FindManifest(dir string) (manifest *Manifest, path string, err error) {
   294  	for _, name := range []string{"plugin.yml", "plugin.yaml"} {
   295  		path = filepath.Join(dir, name)
   296  		f, ferr := os.Open(path)
   297  		if ferr != nil {
   298  			if !os.IsNotExist(ferr) {
   299  				return nil, "", ferr
   300  			}
   301  			continue
   302  		}
   303  		b, ioerr := ioutil.ReadAll(f)
   304  		f.Close()
   305  		if ioerr != nil {
   306  			return nil, path, ioerr
   307  		}
   308  		var parsed Manifest
   309  		err = yaml.Unmarshal(b, &parsed)
   310  		if err != nil {
   311  			return nil, path, err
   312  		}
   313  		manifest = &parsed
   314  		manifest.Id = strings.ToLower(manifest.Id)
   315  		return manifest, path, nil
   316  	}
   317  
   318  	path = filepath.Join(dir, "plugin.json")
   319  	f, ferr := os.Open(path)
   320  	if ferr != nil {
   321  		if os.IsNotExist(ferr) {
   322  			path = ""
   323  		}
   324  		return nil, path, ferr
   325  	}
   326  	defer f.Close()
   327  	var parsed Manifest
   328  	err = json.NewDecoder(f).Decode(&parsed)
   329  	if err != nil {
   330  		return nil, path, err
   331  	}
   332  	manifest = &parsed
   333  	manifest.Id = strings.ToLower(manifest.Id)
   334  	return manifest, path, nil
   335  }