github.com/vnforks/kid@v5.11.1+incompatible/cmd/mattermost/commands/plugin.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var PluginCmd = &cobra.Command{
    14  	Use:   "plugin",
    15  	Short: "Management of plugins",
    16  }
    17  
    18  var PluginAddCmd = &cobra.Command{
    19  	Use:     "add [plugins]",
    20  	Short:   "Add plugins",
    21  	Long:    "Add plugins to your Mattermost server.",
    22  	Example: `  plugin add hovercardexample.tar.gz pluginexample.tar.gz`,
    23  	RunE:    pluginAddCmdF,
    24  }
    25  
    26  var PluginDeleteCmd = &cobra.Command{
    27  	Use:     "delete [plugins]",
    28  	Short:   "Delete plugins",
    29  	Long:    "Delete previously uploaded plugins from your Mattermost server.",
    30  	Example: `  plugin delete hovercardexample pluginexample`,
    31  	RunE:    pluginDeleteCmdF,
    32  }
    33  
    34  var PluginEnableCmd = &cobra.Command{
    35  	Use:     "enable [plugins]",
    36  	Short:   "Enable plugins",
    37  	Long:    "Enable plugins for use on your Mattermost server.",
    38  	Example: `  plugin enable hovercardexample pluginexample`,
    39  	RunE:    pluginEnableCmdF,
    40  }
    41  
    42  var PluginDisableCmd = &cobra.Command{
    43  	Use:     "disable [plugins]",
    44  	Short:   "Disable plugins",
    45  	Long:    "Disable plugins. Disabled plugins are immediately removed from the user interface and logged out of all sessions.",
    46  	Example: `  plugin disable hovercardexample pluginexample`,
    47  	RunE:    pluginDisableCmdF,
    48  }
    49  
    50  var PluginListCmd = &cobra.Command{
    51  	Use:     "list",
    52  	Short:   "List plugins",
    53  	Long:    "List all active and inactive plugins installed on your Mattermost server.",
    54  	Example: `  plugin list`,
    55  	RunE:    pluginListCmdF,
    56  }
    57  
    58  func init() {
    59  	PluginCmd.AddCommand(
    60  		PluginAddCmd,
    61  		PluginDeleteCmd,
    62  		PluginEnableCmd,
    63  		PluginDisableCmd,
    64  		PluginListCmd,
    65  	)
    66  	RootCmd.AddCommand(PluginCmd)
    67  }
    68  
    69  func pluginAddCmdF(command *cobra.Command, args []string) error {
    70  	a, err := InitDBCommandContextCobra(command)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	defer a.Shutdown()
    75  
    76  	if len(args) < 1 {
    77  		return errors.New("Expected at least one argument. See help text for details.")
    78  	}
    79  
    80  	for i, plugin := range args {
    81  		fileReader, err := os.Open(plugin)
    82  		if err != nil {
    83  			return err
    84  		}
    85  
    86  		if _, err := a.InstallPlugin(fileReader, false); err != nil {
    87  			CommandPrintErrorln("Unable to add plugin: " + args[i] + ". Error: " + err.Error())
    88  		} else {
    89  			CommandPrettyPrintln("Added plugin: " + plugin)
    90  		}
    91  		fileReader.Close()
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func pluginDeleteCmdF(command *cobra.Command, args []string) error {
    98  	a, err := InitDBCommandContextCobra(command)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	defer a.Shutdown()
   103  
   104  	if len(args) < 1 {
   105  		return errors.New("Expected at least one argument. See help text for details.")
   106  	}
   107  
   108  	for _, plugin := range args {
   109  		if err := a.RemovePlugin(plugin); err != nil {
   110  			CommandPrintErrorln("Unable to delete plugin: " + plugin + ". Error: " + err.Error())
   111  		} else {
   112  			CommandPrettyPrintln("Deleted plugin: " + plugin)
   113  		}
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func pluginEnableCmdF(command *cobra.Command, args []string) error {
   120  	a, err := InitDBCommandContextCobra(command)
   121  	if err != nil {
   122  		return err
   123  	}
   124  	defer a.Shutdown()
   125  
   126  	if len(args) < 1 {
   127  		return errors.New("Expected at least one argument. See help text for details.")
   128  	}
   129  
   130  	for _, plugin := range args {
   131  		if err := a.EnablePlugin(plugin); err != nil {
   132  			CommandPrintErrorln("Unable to enable plugin: " + plugin + ". Error: " + err.Error())
   133  		} else {
   134  			CommandPrettyPrintln("Enabled plugin: " + plugin)
   135  		}
   136  	}
   137  
   138  	return nil
   139  }
   140  
   141  func pluginDisableCmdF(command *cobra.Command, args []string) error {
   142  	a, err := InitDBCommandContextCobra(command)
   143  	if err != nil {
   144  		return err
   145  	}
   146  	defer a.Shutdown()
   147  
   148  	if len(args) < 1 {
   149  		return errors.New("Expected at least one argument. See help text for details.")
   150  	}
   151  
   152  	for _, plugin := range args {
   153  		if err := a.DisablePlugin(plugin); err != nil {
   154  			CommandPrintErrorln("Unable to disable plugin: " + plugin + ". Error: " + err.Error())
   155  		} else {
   156  			CommandPrettyPrintln("Disabled plugin: " + plugin)
   157  		}
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  func pluginListCmdF(command *cobra.Command, args []string) error {
   164  	a, err := InitDBCommandContextCobra(command)
   165  	if err != nil {
   166  		return err
   167  	}
   168  	defer a.Shutdown()
   169  
   170  	pluginsResp, appErr := a.GetPlugins()
   171  	if appErr != nil {
   172  		return errors.New("Unable to list plugins. Error: " + appErr.Error())
   173  	}
   174  
   175  	CommandPrettyPrintln("Listing active plugins")
   176  	for _, plugin := range pluginsResp.Active {
   177  		CommandPrettyPrintln(plugin.Manifest.Name + ", Version: " + plugin.Manifest.Version)
   178  	}
   179  
   180  	CommandPrettyPrintln("Listing inactive plugins")
   181  	for _, plugin := range pluginsResp.Inactive {
   182  		CommandPrettyPrintln(plugin.Manifest.Name + ", Version: " + plugin.Manifest.Version)
   183  	}
   184  
   185  	return nil
   186  }