mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
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()
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
package plugin
|
||||
|
||||
type BundleInfo struct {
|
||||
Path string
|
||||
|
||||
Manifest *Manifest
|
||||
ManifestPath string
|
||||
ManifestError error
|
||||
}
|
||||
|
||||
// Returns bundle info for the given path. The return value is never nil.
|
||||
func BundleInfoForPath(path string) *BundleInfo {
|
||||
m, mpath, err := FindManifest(path)
|
||||
return &BundleInfo{
|
||||
Path: path,
|
||||
Manifest: m,
|
||||
ManifestPath: mpath,
|
||||
ManifestError: err,
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBundleInfoForPath(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "mm-plugin-test")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
path := filepath.Join(dir, "plugin.json")
|
||||
f, err := os.Create(path)
|
||||
require.NoError(t, err)
|
||||
_, err = f.WriteString(`{"id": "foo"}`)
|
||||
f.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
info := BundleInfoForPath(dir)
|
||||
assert.Equal(t, info.Path, dir)
|
||||
assert.NotNil(t, info.Manifest)
|
||||
assert.Equal(t, info.ManifestPath, path)
|
||||
assert.Nil(t, info.ManifestError)
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Manifest struct {
|
||||
Id string `json:"id" yaml:"id"`
|
||||
Backend *ManifestBackend `json:"backend,omitempty" yaml:"backend,omitempty"`
|
||||
}
|
||||
|
||||
type ManifestBackend struct {
|
||||
Executable string `json:"executable" yaml:"executable"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFindManifest(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
Filename string
|
||||
Contents string
|
||||
ExpectError bool
|
||||
ExpectNotExist bool
|
||||
}{
|
||||
{"foo", "bar", true, true},
|
||||
{"plugin.json", "bar", true, false},
|
||||
{"plugin.json", `{"id": "foo"}`, false, false},
|
||||
{"plugin.yaml", `id: foo`, false, false},
|
||||
{"plugin.yaml", "bar", true, false},
|
||||
{"plugin.yml", `id: foo`, false, false},
|
||||
{"plugin.yml", "bar", true, false},
|
||||
} {
|
||||
dir, err := ioutil.TempDir("", "mm-plugin-test")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
path := filepath.Join(dir, tc.Filename)
|
||||
f, err := os.Create(path)
|
||||
require.NoError(t, err)
|
||||
_, err = f.WriteString(tc.Contents)
|
||||
f.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
m, mpath, err := FindManifest(dir)
|
||||
assert.True(t, (err != nil) == tc.ExpectError, tc.Filename)
|
||||
assert.True(t, (err != nil && os.IsNotExist(err)) == tc.ExpectNotExist, tc.Filename)
|
||||
if !tc.ExpectNotExist {
|
||||
assert.Equal(t, path, mpath, tc.Filename)
|
||||
} else {
|
||||
assert.Empty(t, mpath, tc.Filename)
|
||||
}
|
||||
if !tc.ExpectError {
|
||||
require.NotNil(t, m, tc.Filename)
|
||||
assert.NotEmpty(t, m.Id, tc.Filename)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManifestUnmarshal(t *testing.T) {
|
||||
expected := Manifest{
|
||||
Id: "theid",
|
||||
Backend: &ManifestBackend{
|
||||
Executable: "theexecutable",
|
||||
},
|
||||
}
|
||||
|
||||
var yamlResult Manifest
|
||||
require.NoError(t, yaml.Unmarshal([]byte(`
|
||||
id: theid
|
||||
backend:
|
||||
executable: theexecutable
|
||||
`), &yamlResult))
|
||||
assert.Equal(t, expected, yamlResult)
|
||||
|
||||
var jsonResult Manifest
|
||||
require.NoError(t, json.Unmarshal([]byte(`{
|
||||
"id": "theid",
|
||||
"backend": {
|
||||
"executable": "theexecutable"
|
||||
}
|
||||
}`), &jsonResult))
|
||||
assert.Equal(t, expected, jsonResult)
|
||||
}
|
||||
|
||||
func TestFindManifest_FileErrors(t *testing.T) {
|
||||
for _, tc := range []string{"plugin.yaml", "plugin.json"} {
|
||||
dir, err := ioutil.TempDir("", "mm-plugin-test")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
path := filepath.Join(dir, tc)
|
||||
require.NoError(t, os.Mkdir(path, 0700))
|
||||
|
||||
m, mpath, err := FindManifest(dir)
|
||||
assert.Nil(t, m)
|
||||
assert.Equal(t, path, mpath)
|
||||
assert.Error(t, err, tc)
|
||||
assert.False(t, os.IsNotExist(err), tc)
|
||||
}
|
||||
}
|
||||
@@ -3,21 +3,31 @@ package pluginenv
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/plugin"
|
||||
)
|
||||
|
||||
type APIProviderFunc func(*plugin.Manifest) (plugin.API, error)
|
||||
type SupervisorProviderFunc func(*plugin.BundleInfo) (plugin.Supervisor, error)
|
||||
type APIProviderFunc func(*model.Manifest) (plugin.API, error)
|
||||
type SupervisorProviderFunc func(*model.BundleInfo) (plugin.Supervisor, error)
|
||||
|
||||
type ActivePlugin struct {
|
||||
BundleInfo *model.BundleInfo
|
||||
Supervisor plugin.Supervisor
|
||||
}
|
||||
|
||||
// Environment represents an environment that plugins are discovered and launched in.
|
||||
type Environment struct {
|
||||
searchPath string
|
||||
webappPath string
|
||||
apiProvider APIProviderFunc
|
||||
supervisorProvider SupervisorProviderFunc
|
||||
activePlugins map[string]plugin.Supervisor
|
||||
activePlugins map[string]ActivePlugin
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
type Option func(*Environment)
|
||||
@@ -25,7 +35,7 @@ type Option func(*Environment)
|
||||
// Creates a new environment. At a minimum, the APIProvider and SearchPath options are required.
|
||||
func New(options ...Option) (*Environment, error) {
|
||||
env := &Environment{
|
||||
activePlugins: make(map[string]plugin.Supervisor),
|
||||
activePlugins: make(map[string]ActivePlugin),
|
||||
}
|
||||
for _, opt := range options {
|
||||
opt(env)
|
||||
@@ -35,19 +45,45 @@ func New(options ...Option) (*Environment, error) {
|
||||
}
|
||||
if env.searchPath == "" {
|
||||
return nil, fmt.Errorf("a search path must be provided")
|
||||
} else if env.apiProvider == nil {
|
||||
return nil, fmt.Errorf("an api provider must be provided")
|
||||
}
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// Returns the configured webapp path.
|
||||
func (env *Environment) WebappPath() string {
|
||||
return env.webappPath
|
||||
}
|
||||
|
||||
// Returns the configured search path.
|
||||
func (env *Environment) SearchPath() string {
|
||||
return env.searchPath
|
||||
}
|
||||
|
||||
// Returns a list of all plugins found within the environment.
|
||||
func (env *Environment) Plugins() ([]*plugin.BundleInfo, error) {
|
||||
func (env *Environment) Plugins() ([]*model.BundleInfo, error) {
|
||||
env.mutex.Lock()
|
||||
defer env.mutex.Unlock()
|
||||
return ScanSearchPath(env.searchPath)
|
||||
}
|
||||
|
||||
// Returns a list of all currently active plugins within the environment.
|
||||
func (env *Environment) ActivePlugins() ([]*model.BundleInfo, error) {
|
||||
env.mutex.Lock()
|
||||
defer env.mutex.Unlock()
|
||||
|
||||
activePlugins := []*model.BundleInfo{}
|
||||
for _, p := range env.activePlugins {
|
||||
activePlugins = append(activePlugins, p.BundleInfo)
|
||||
}
|
||||
|
||||
return activePlugins, nil
|
||||
}
|
||||
|
||||
// Returns the ids of the currently active plugins.
|
||||
func (env *Environment) ActivePluginIds() (ids []string) {
|
||||
env.mutex.Lock()
|
||||
defer env.mutex.Unlock()
|
||||
|
||||
for id := range env.activePlugins {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
@@ -56,6 +92,9 @@ func (env *Environment) ActivePluginIds() (ids []string) {
|
||||
|
||||
// Activates the plugin with the given id.
|
||||
func (env *Environment) ActivatePlugin(id string) error {
|
||||
env.mutex.Lock()
|
||||
defer env.mutex.Unlock()
|
||||
|
||||
if _, ok := env.activePlugins[id]; ok {
|
||||
return fmt.Errorf("plugin already active: %v", id)
|
||||
}
|
||||
@@ -63,46 +102,91 @@ func (env *Environment) ActivatePlugin(id string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var plugin *plugin.BundleInfo
|
||||
var bundle *model.BundleInfo
|
||||
for _, p := range plugins {
|
||||
if p.Manifest != nil && p.Manifest.Id == id {
|
||||
if plugin != nil {
|
||||
if bundle != nil {
|
||||
return fmt.Errorf("multiple plugins found: %v", id)
|
||||
}
|
||||
plugin = p
|
||||
bundle = p
|
||||
}
|
||||
}
|
||||
if plugin == nil {
|
||||
if bundle == nil {
|
||||
return fmt.Errorf("plugin not found: %v", id)
|
||||
}
|
||||
supervisor, err := env.supervisorProvider(plugin)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to create supervisor for plugin: %v", id)
|
||||
|
||||
activePlugin := ActivePlugin{BundleInfo: bundle}
|
||||
|
||||
var supervisor plugin.Supervisor
|
||||
|
||||
if bundle.Manifest.Backend != nil {
|
||||
if env.apiProvider == nil {
|
||||
return fmt.Errorf("env missing api provider, cannot activate plugin: %v", id)
|
||||
}
|
||||
|
||||
supervisor, err = env.supervisorProvider(bundle)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to create supervisor for plugin: %v", id)
|
||||
}
|
||||
api, err := env.apiProvider(bundle.Manifest)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get api for plugin: %v", id)
|
||||
}
|
||||
if err := supervisor.Start(); err != nil {
|
||||
return errors.Wrapf(err, "unable to start plugin: %v", id)
|
||||
}
|
||||
if err := supervisor.Hooks().OnActivate(api); err != nil {
|
||||
supervisor.Stop()
|
||||
return errors.Wrapf(err, "unable to activate plugin: %v", id)
|
||||
}
|
||||
|
||||
activePlugin.Supervisor = supervisor
|
||||
}
|
||||
api, err := env.apiProvider(plugin.Manifest)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get api for plugin: %v", id)
|
||||
|
||||
if bundle.Manifest.Webapp != nil {
|
||||
if env.webappPath == "" {
|
||||
if supervisor != nil {
|
||||
supervisor.Stop()
|
||||
}
|
||||
return fmt.Errorf("env missing webapp path, cannot activate plugin: %v", id)
|
||||
}
|
||||
|
||||
webappBundle, err := ioutil.ReadFile(fmt.Sprintf("%s/%s/webapp/%s_bundle.js", env.searchPath, id, id))
|
||||
if err != nil {
|
||||
if supervisor != nil {
|
||||
supervisor.Stop()
|
||||
}
|
||||
return errors.Wrapf(err, "unable to read webapp bundle: %v", id)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(fmt.Sprintf("%s/%s_bundle.js", env.webappPath, id), webappBundle, 0644)
|
||||
if err != nil {
|
||||
if supervisor != nil {
|
||||
supervisor.Stop()
|
||||
}
|
||||
return errors.Wrapf(err, "unable to write webapp bundle: %v", id)
|
||||
}
|
||||
}
|
||||
if err := supervisor.Start(); err != nil {
|
||||
return errors.Wrapf(err, "unable to start plugin: %v", id)
|
||||
}
|
||||
if err := supervisor.Hooks().OnActivate(api); err != nil {
|
||||
supervisor.Stop()
|
||||
return errors.Wrapf(err, "unable to activate plugin: %v", id)
|
||||
}
|
||||
env.activePlugins[id] = supervisor
|
||||
|
||||
env.activePlugins[id] = activePlugin
|
||||
return nil
|
||||
}
|
||||
|
||||
// Deactivates the plugin with the given id.
|
||||
func (env *Environment) DeactivatePlugin(id string) error {
|
||||
if supervisor, ok := env.activePlugins[id]; !ok {
|
||||
env.mutex.Lock()
|
||||
defer env.mutex.Unlock()
|
||||
|
||||
if activePlugin, ok := env.activePlugins[id]; !ok {
|
||||
return fmt.Errorf("plugin not active: %v", id)
|
||||
} else {
|
||||
delete(env.activePlugins, id)
|
||||
err := supervisor.Hooks().OnDeactivate()
|
||||
if serr := supervisor.Stop(); err == nil {
|
||||
err = serr
|
||||
var err error
|
||||
if activePlugin.Supervisor != nil {
|
||||
err = activePlugin.Supervisor.Hooks().OnDeactivate()
|
||||
if serr := activePlugin.Supervisor.Stop(); err == nil {
|
||||
err = serr
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -110,14 +194,19 @@ func (env *Environment) DeactivatePlugin(id string) error {
|
||||
|
||||
// Deactivates all plugins and gracefully shuts down the environment.
|
||||
func (env *Environment) Shutdown() (errs []error) {
|
||||
for _, supervisor := range env.activePlugins {
|
||||
if err := supervisor.Hooks().OnDeactivate(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if err := supervisor.Stop(); err != nil {
|
||||
errs = append(errs, err)
|
||||
env.mutex.Lock()
|
||||
defer env.mutex.Unlock()
|
||||
|
||||
for _, activePlugin := range env.activePlugins {
|
||||
if activePlugin.Supervisor != nil {
|
||||
if err := activePlugin.Supervisor.Hooks().OnDeactivate(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if err := activePlugin.Supervisor.Stop(); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
env.activePlugins = make(map[string]plugin.Supervisor)
|
||||
env.activePlugins = make(map[string]ActivePlugin)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/plugin"
|
||||
"github.com/mattermost/platform/plugin/plugintest"
|
||||
)
|
||||
@@ -19,7 +20,7 @@ type MockProvider struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockProvider) API(manifest *plugin.Manifest) (plugin.API, error) {
|
||||
func (m *MockProvider) API(manifest *model.Manifest) (plugin.API, error) {
|
||||
ret := m.Called()
|
||||
if ret.Get(0) == nil {
|
||||
return nil, ret.Error(1)
|
||||
@@ -27,7 +28,7 @@ func (m *MockProvider) API(manifest *plugin.Manifest) (plugin.API, error) {
|
||||
return ret.Get(0).(plugin.API), ret.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockProvider) Supervisor(bundle *plugin.BundleInfo) (plugin.Supervisor, error) {
|
||||
func (m *MockProvider) Supervisor(bundle *model.BundleInfo) (plugin.Supervisor, error) {
|
||||
ret := m.Called()
|
||||
if ret.Get(0) == nil {
|
||||
return nil, ret.Error(1)
|
||||
@@ -90,19 +91,13 @@ func TestNew_MissingOptions(t *testing.T) {
|
||||
)
|
||||
assert.Nil(t, env)
|
||||
assert.Error(t, err)
|
||||
|
||||
env, err = New(
|
||||
SearchPath(dir),
|
||||
)
|
||||
assert.Nil(t, env)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestEnvironment(t *testing.T) {
|
||||
dir := initTmpDir(t, map[string]string{
|
||||
".foo/plugin.json": `{"id": "foo"}`,
|
||||
"foo/bar": "asdf",
|
||||
"foo/plugin.json": `{"id": "foo"}`,
|
||||
"foo/plugin.json": `{"id": "foo", "backend": {}}`,
|
||||
"bar/zxc": "qwer",
|
||||
"baz/plugin.yaml": "id: baz",
|
||||
"bad/plugin.json": "asd",
|
||||
@@ -110,11 +105,14 @@ func TestEnvironment(t *testing.T) {
|
||||
})
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
webappDir := "notarealdirectory"
|
||||
|
||||
var provider MockProvider
|
||||
defer provider.AssertExpectations(t)
|
||||
|
||||
env, err := New(
|
||||
SearchPath(dir),
|
||||
WebappPath(webappDir),
|
||||
APIProvider(provider.API),
|
||||
SupervisorProvider(provider.Supervisor),
|
||||
)
|
||||
@@ -125,6 +123,10 @@ func TestEnvironment(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, plugins, 3)
|
||||
|
||||
activePlugins, err := env.ActivePlugins()
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, activePlugins, 0)
|
||||
|
||||
assert.Error(t, env.ActivatePlugin("x"))
|
||||
|
||||
var api struct{ plugin.API }
|
||||
@@ -144,6 +146,9 @@ func TestEnvironment(t *testing.T) {
|
||||
|
||||
assert.NoError(t, env.ActivatePlugin("foo"))
|
||||
assert.Equal(t, env.ActivePluginIds(), []string{"foo"})
|
||||
activePlugins, err = env.ActivePlugins()
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, activePlugins, 1)
|
||||
assert.Error(t, env.ActivatePlugin("foo"))
|
||||
|
||||
hooks.On("OnDeactivate").Return(nil)
|
||||
@@ -152,6 +157,10 @@ func TestEnvironment(t *testing.T) {
|
||||
|
||||
assert.NoError(t, env.ActivatePlugin("foo"))
|
||||
assert.Equal(t, env.ActivePluginIds(), []string{"foo"})
|
||||
|
||||
assert.Equal(t, env.SearchPath(), dir)
|
||||
assert.Equal(t, env.WebappPath(), webappDir)
|
||||
|
||||
assert.Empty(t, env.Shutdown())
|
||||
}
|
||||
|
||||
@@ -195,7 +204,7 @@ func TestEnvironment_BadSearchPathError(t *testing.T) {
|
||||
|
||||
func TestEnvironment_ActivatePluginErrors(t *testing.T) {
|
||||
dir := initTmpDir(t, map[string]string{
|
||||
"foo/plugin.json": `{"id": "foo"}`,
|
||||
"foo/plugin.json": `{"id": "foo", "backend": {}}`,
|
||||
})
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
@@ -254,7 +263,7 @@ func TestEnvironment_ActivatePluginErrors(t *testing.T) {
|
||||
|
||||
func TestEnvironment_ShutdownError(t *testing.T) {
|
||||
dir := initTmpDir(t, map[string]string{
|
||||
"foo/plugin.json": `{"id": "foo"}`,
|
||||
"foo/plugin.json": `{"id": "foo", "backend": {}}`,
|
||||
})
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package pluginenv
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/plugin"
|
||||
"github.com/mattermost/platform/plugin/rpcplugin"
|
||||
)
|
||||
@@ -29,14 +30,21 @@ func SearchPath(path string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WebappPath specifies the static directory serving the webapp.
|
||||
func WebappPath(path string) Option {
|
||||
return func(env *Environment) {
|
||||
env.webappPath = path
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultSupervisorProvider chooses a supervisor based on the plugin's manifest contents. E.g. if
|
||||
// the manifest specifies a backend executable, it will be given an rpcplugin.Supervisor.
|
||||
func DefaultSupervisorProvider(bundle *plugin.BundleInfo) (plugin.Supervisor, error) {
|
||||
func DefaultSupervisorProvider(bundle *model.BundleInfo) (plugin.Supervisor, error) {
|
||||
if bundle.Manifest == nil {
|
||||
return nil, fmt.Errorf("a manifest is required")
|
||||
}
|
||||
if bundle.Manifest.Backend == nil {
|
||||
return nil, fmt.Errorf("invalid manifest: at this time, only backend plugins are supported")
|
||||
return nil, fmt.Errorf("invalid manifest: missing backend plugin")
|
||||
}
|
||||
return rpcplugin.SupervisorProvider(bundle)
|
||||
}
|
||||
|
||||
@@ -6,22 +6,22 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/platform/plugin"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/plugin/rpcplugin"
|
||||
)
|
||||
|
||||
func TestDefaultSupervisorProvider(t *testing.T) {
|
||||
_, err := DefaultSupervisorProvider(&plugin.BundleInfo{})
|
||||
_, err := DefaultSupervisorProvider(&model.BundleInfo{})
|
||||
assert.Error(t, err)
|
||||
|
||||
_, err = DefaultSupervisorProvider(&plugin.BundleInfo{
|
||||
Manifest: &plugin.Manifest{},
|
||||
_, err = DefaultSupervisorProvider(&model.BundleInfo{
|
||||
Manifest: &model.Manifest{},
|
||||
})
|
||||
assert.Error(t, err)
|
||||
|
||||
supervisor, err := DefaultSupervisorProvider(&plugin.BundleInfo{
|
||||
Manifest: &plugin.Manifest{
|
||||
Backend: &plugin.ManifestBackend{
|
||||
supervisor, err := DefaultSupervisorProvider(&model.BundleInfo{
|
||||
Manifest: &model.Manifest{
|
||||
Backend: &model.ManifestBackend{
|
||||
Executable: "foo",
|
||||
},
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mattermost/platform/plugin"
|
||||
"github.com/mattermost/platform/model"
|
||||
)
|
||||
|
||||
// Performs a full scan of the given path.
|
||||
@@ -14,17 +14,17 @@ import (
|
||||
// parsed).
|
||||
//
|
||||
// Plugins are found non-recursively and paths beginning with a dot are always ignored.
|
||||
func ScanSearchPath(path string) ([]*plugin.BundleInfo, error) {
|
||||
func ScanSearchPath(path string) ([]*model.BundleInfo, error) {
|
||||
files, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var ret []*plugin.BundleInfo
|
||||
var ret []*model.BundleInfo
|
||||
for _, file := range files {
|
||||
if !file.IsDir() || file.Name()[0] == '.' {
|
||||
continue
|
||||
}
|
||||
if info := plugin.BundleInfoForPath(filepath.Join(path, file.Name())); info.ManifestPath != "" {
|
||||
if info := model.BundleInfoForPath(filepath.Join(path, file.Name())); info.ManifestPath != "" {
|
||||
ret = append(ret, info)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/platform/plugin"
|
||||
"github.com/mattermost/platform/model"
|
||||
)
|
||||
|
||||
func TestScanSearchPath(t *testing.T) {
|
||||
@@ -27,17 +27,17 @@ func TestScanSearchPath(t *testing.T) {
|
||||
plugins, err := ScanSearchPath(dir)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, plugins, 3)
|
||||
assert.Contains(t, plugins, &plugin.BundleInfo{
|
||||
assert.Contains(t, plugins, &model.BundleInfo{
|
||||
Path: filepath.Join(dir, "foo"),
|
||||
ManifestPath: filepath.Join(dir, "foo", "plugin.json"),
|
||||
Manifest: &plugin.Manifest{
|
||||
Manifest: &model.Manifest{
|
||||
Id: "foo",
|
||||
},
|
||||
})
|
||||
assert.Contains(t, plugins, &plugin.BundleInfo{
|
||||
assert.Contains(t, plugins, &model.BundleInfo{
|
||||
Path: filepath.Join(dir, "baz"),
|
||||
ManifestPath: filepath.Join(dir, "baz", "plugin.yaml"),
|
||||
Manifest: &plugin.Manifest{
|
||||
Manifest: &model.Manifest{
|
||||
Id: "baz",
|
||||
},
|
||||
})
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/plugin"
|
||||
)
|
||||
|
||||
@@ -116,7 +117,7 @@ func (s *Supervisor) runPlugin(ctx context.Context, start chan<- error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SupervisorProvider(bundle *plugin.BundleInfo) (plugin.Supervisor, error) {
|
||||
func SupervisorProvider(bundle *model.BundleInfo) (plugin.Supervisor, error) {
|
||||
if bundle.Manifest == nil {
|
||||
return nil, fmt.Errorf("no manifest available")
|
||||
} else if bundle.Manifest.Backend == nil || bundle.Manifest.Backend.Executable == "" {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/platform/plugin"
|
||||
"github.com/mattermost/platform/model"
|
||||
)
|
||||
|
||||
func TestSupervisor(t *testing.T) {
|
||||
@@ -35,7 +35,7 @@ func TestSupervisor(t *testing.T) {
|
||||
|
||||
ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "backend.exe"}}`), 0600)
|
||||
|
||||
bundle := plugin.BundleInfoForPath(dir)
|
||||
bundle := model.BundleInfoForPath(dir)
|
||||
supervisor, err := SupervisorProvider(bundle)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, supervisor.Start())
|
||||
@@ -61,7 +61,7 @@ func TestSupervisor_StartTimeout(t *testing.T) {
|
||||
|
||||
ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "backend.exe"}}`), 0600)
|
||||
|
||||
bundle := plugin.BundleInfoForPath(dir)
|
||||
bundle := model.BundleInfoForPath(dir)
|
||||
supervisor, err := SupervisorProvider(bundle)
|
||||
require.NoError(t, err)
|
||||
require.Error(t, supervisor.Start())
|
||||
@@ -98,7 +98,7 @@ func TestSupervisor_PluginCrash(t *testing.T) {
|
||||
|
||||
ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "backend.exe"}}`), 0600)
|
||||
|
||||
bundle := plugin.BundleInfoForPath(dir)
|
||||
bundle := model.BundleInfoForPath(dir)
|
||||
supervisor, err := SupervisorProvider(bundle)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, supervisor.Start())
|
||||
|
||||
Reference in New Issue
Block a user