diff options
Diffstat (limited to 'cmd/mattermost')
-rw-r--r-- | cmd/mattermost/commands/init.go | 2 | ||||
-rw-r--r-- | cmd/mattermost/commands/plugin.go | 186 | ||||
-rw-r--r-- | cmd/mattermost/commands/plugin_test.go | 42 | ||||
-rw-r--r-- | cmd/mattermost/commands/server.go | 4 |
4 files changed, 232 insertions, 2 deletions
diff --git a/cmd/mattermost/commands/init.go b/cmd/mattermost/commands/init.go index ea7e8ec84..8d8f12c53 100644 --- a/cmd/mattermost/commands/init.go +++ b/cmd/mattermost/commands/init.go @@ -17,6 +17,8 @@ func InitDBCommandContextCobra(command *cobra.Command) (*app.App, error) { } a, err := InitDBCommandContext(config) + a.InitPlugins(*a.Config().PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory) + if err != nil { // Returning an error just prints the usage message, so actually panic panic(err) diff --git a/cmd/mattermost/commands/plugin.go b/cmd/mattermost/commands/plugin.go new file mode 100644 index 000000000..56a57ddf1 --- /dev/null +++ b/cmd/mattermost/commands/plugin.go @@ -0,0 +1,186 @@ +// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package commands + +import ( + "errors" + "os" + + "github.com/spf13/cobra" +) + +var PluginCmd = &cobra.Command{ + Use: "plugin", + Short: "Management of plugins", +} + +var PluginAddCmd = &cobra.Command{ + Use: "add [plugins]", + Short: "Add plugins", + Long: "Add plugins to your Mattermost server.", + Example: ` plugin add hovercardexample.tar.gz pluginexample.tar.gz`, + RunE: pluginAddCmdF, +} + +var PluginDeleteCmd = &cobra.Command{ + Use: "delete [plugins]", + Short: "Delete plugins", + Long: "Delete previously uploaded plugins from your Mattermost server.", + Example: ` plugin delete hovercardexample pluginexample`, + RunE: pluginDeleteCmdF, +} + +var PluginEnableCmd = &cobra.Command{ + Use: "enable [plugins]", + Short: "Enable plugins", + Long: "Enable plugins for use on your Mattermost server.", + Example: ` plugin enable hovercardexample pluginexample`, + RunE: pluginEnableCmdF, +} + +var PluginDisableCmd = &cobra.Command{ + Use: "disable [plugins]", + Short: "Disable plugins", + Long: "Disable plugins. Disabled plugins are immediately removed from the user interface and logged out of all sessions.", + Example: ` plugin disable hovercardexample pluginexample`, + RunE: pluginDisableCmdF, +} + +var PluginListCmd = &cobra.Command{ + Use: "list", + Short: "List plugins", + Long: "List all active and inactive plugins installed on your Mattermost server.", + Example: ` plugin list`, + RunE: pluginListCmdF, +} + +func init() { + PluginCmd.AddCommand( + PluginAddCmd, + PluginDeleteCmd, + PluginEnableCmd, + PluginDisableCmd, + PluginListCmd, + ) + RootCmd.AddCommand(PluginCmd) +} + +func pluginAddCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + if len(args) < 1 { + return errors.New("Expected at least one argument. See help text for details.") + } + + for i, plugin := range args { + fileReader, err := os.Open(plugin) + if err != nil { + return err + } + + if _, err := a.InstallPlugin(fileReader); err != nil { + CommandPrintErrorln("Unable to add plugin: " + args[i] + ". Error: " + err.Error()) + } else { + CommandPrettyPrintln("Added plugin: " + plugin) + } + fileReader.Close() + } + + return nil +} + +func pluginDeleteCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + if len(args) < 1 { + return errors.New("Expected at least one argument. See help text for details.") + } + + for _, plugin := range args { + if err := a.RemovePlugin(plugin); err != nil { + CommandPrintErrorln("Unable to delete plugin: " + plugin + ". Error: " + err.Error()) + } else { + CommandPrettyPrintln("Deleted plugin: " + plugin) + } + } + + return nil +} + +func pluginEnableCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + if len(args) < 1 { + return errors.New("Expected at least one argument. See help text for details.") + } + + for _, plugin := range args { + if err := a.EnablePlugin(plugin); err != nil { + CommandPrintErrorln("Unable to enable plugin: " + plugin + ". Error: " + err.Error()) + } else { + CommandPrettyPrintln("Enabled plugin: " + plugin) + } + } + + return nil +} + +func pluginDisableCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + if len(args) < 1 { + return errors.New("Expected at least one argument. See help text for details.") + } + + for _, plugin := range args { + if err := a.DisablePlugin(plugin); err != nil { + CommandPrintErrorln("Unable to disable plugin: " + plugin + ". Error: " + err.Error()) + } else { + CommandPrettyPrintln("Disabled plugin: " + plugin) + } + } + + return nil +} + +func pluginListCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + pluginsResp, appErr := a.GetPlugins() + if appErr != nil { + return errors.New("Unable to list plugins. Error: " + appErr.Error()) + } + + CommandPrettyPrintln("Listing active plugins") + for _, plugin := range pluginsResp.Active { + CommandPrettyPrintln(plugin.Manifest.Name + ", Version: " + plugin.Manifest.Version) + } + + CommandPrettyPrintln("Listing inactive plugins") + for _, plugin := range pluginsResp.Inactive { + CommandPrettyPrintln(plugin.Manifest.Name + ", Version: " + plugin.Manifest.Version) + } + + return nil +} diff --git a/cmd/mattermost/commands/plugin_test.go b/cmd/mattermost/commands/plugin_test.go new file mode 100644 index 000000000..043823583 --- /dev/null +++ b/cmd/mattermost/commands/plugin_test.go @@ -0,0 +1,42 @@ +package commands + +import ( + "os" + "path/filepath" + "testing" + + "github.com/mattermost/mattermost-server/api4" + "github.com/mattermost/mattermost-server/utils" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPlugin(t *testing.T) { + os.MkdirAll("./test-plugins", os.ModePerm) + os.MkdirAll("./test-client-plugins", os.ModePerm) + + th := api4.Setup().InitBasic().InitSystemAdmin() + defer th.TearDown() + + path, _ := utils.FindDir("tests") + + os.Chdir(filepath.Join("..", "..", "..")) + + CheckCommand(t, "--config", filepath.Join(path, "test-config.json"), "plugin", "add", filepath.Join(path, "testplugin.tar.gz")) + + CheckCommand(t, "--config", filepath.Join(path, "test-config.json"), "plugin", "enable", "testplugin") + cfg, _, _, err := utils.LoadConfig(filepath.Join(path, "test-config.json")) + require.Nil(t, err) + assert.Equal(t, cfg.PluginSettings.PluginStates["testplugin"].Enable, true) + + CheckCommand(t, "--config", filepath.Join(path, "test-config.json"), "plugin", "disable", "testplugin") + cfg, _, _, err = utils.LoadConfig(filepath.Join(path, "test-config.json")) + require.Nil(t, err) + assert.Equal(t, cfg.PluginSettings.PluginStates["testplugin"].Enable, false) + + CheckCommand(t, "--config", filepath.Join(path, "test-config.json"), "plugin", "list") + + CheckCommand(t, "--config", filepath.Join(path, "test-config.json"), "plugin", "delete", "testplugin") + + os.Chdir(filepath.Join("cmd", "mattermost", "commands")) +} diff --git a/cmd/mattermost/commands/server.go b/cmd/mattermost/commands/server.go index 9500d5dcb..1fab5c83a 100644 --- a/cmd/mattermost/commands/server.go +++ b/cmd/mattermost/commands/server.go @@ -98,10 +98,10 @@ func runServer(configFileLocation string, disableConfigWatch bool, usedPlatform a.DoAdvancedPermissionsMigration() a.DoEmojisPermissionsMigration() - a.InitPlugins(*a.Config().PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory, nil) + a.InitPlugins(*a.Config().PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory) a.AddConfigListener(func(prevCfg, cfg *model.Config) { if *cfg.PluginSettings.Enable { - a.InitPlugins(*cfg.PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory, nil) + a.InitPlugins(*cfg.PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory) } else { a.ShutDownPlugins() } |