github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/plugin/client.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package plugin
     5  
     6  import (
     7  	"github.com/hashicorp/go-plugin"
     8  )
     9  
    10  const (
    11  	INTERNAL_KEY_PREFIX = "mmi_"
    12  	BOT_USER_KEY        = INTERNAL_KEY_PREFIX + "botid"
    13  )
    14  
    15  // Starts the serving of a Mattermost plugin over net/rpc. gRPC is not yet supported.
    16  //
    17  // Call this when your plugin is ready to start.
    18  func ClientMain(pluginImplementation interface{}) {
    19  	if impl, ok := pluginImplementation.(interface {
    20  		SetAPI(api API)
    21  		SetHelpers(helpers Helpers)
    22  	}); !ok {
    23  		panic("Plugin implementation given must embed plugin.MattermostPlugin")
    24  	} else {
    25  		impl.SetAPI(nil)
    26  		impl.SetHelpers(nil)
    27  	}
    28  
    29  	pluginMap := map[string]plugin.Plugin{
    30  		"hooks": &hooksPlugin{hooks: pluginImplementation},
    31  	}
    32  
    33  	plugin.Serve(&plugin.ServeConfig{
    34  		HandshakeConfig: handshake,
    35  		Plugins:         pluginMap,
    36  	})
    37  }
    38  
    39  type MattermostPlugin struct {
    40  	// API exposes the plugin api, and becomes available just prior to the OnActive hook.
    41  	API     API
    42  	Helpers Helpers
    43  }
    44  
    45  // SetAPI persists the given API interface to the plugin. It is invoked just prior to the
    46  // OnActivate hook, exposing the API for use by the plugin.
    47  func (p *MattermostPlugin) SetAPI(api API) {
    48  	p.API = api
    49  }
    50  
    51  // SetHelpers does the same thing as SetAPI except for the plugin helpers.
    52  func (p *MattermostPlugin) SetHelpers(helpers Helpers) {
    53  	p.Helpers = helpers
    54  }