github.com/levb/mattermost-server@v5.3.1+incompatible/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 // Starts the serving of a Mattermost plugin over net/rpc. gRPC is not yet supported. 11 // 12 // Call this when your plugin is ready to start. 13 func ClientMain(pluginImplementation interface{}) { 14 if impl, ok := pluginImplementation.(interface { 15 SetAPI(api API) 16 SetSelfRef(ref interface{}) 17 }); !ok { 18 panic("Plugin implementation given must embed plugin.MattermostPlugin") 19 } else { 20 impl.SetAPI(nil) 21 impl.SetSelfRef(pluginImplementation) 22 } 23 24 pluginMap := map[string]plugin.Plugin{ 25 "hooks": &hooksPlugin{hooks: pluginImplementation}, 26 } 27 28 plugin.Serve(&plugin.ServeConfig{ 29 HandshakeConfig: handshake, 30 Plugins: pluginMap, 31 }) 32 } 33 34 type MattermostPlugin struct { 35 // API exposes the plugin api, and becomes available just prior to the OnActive hook. 36 API API 37 38 selfRef interface{} // This is so we can unmarshal into our parent 39 } 40 41 // SetAPI persists the given API interface to the plugin. It is invoked just prior to the 42 // OnActivate hook, exposing the API for use by the plugin. 43 func (p *MattermostPlugin) SetAPI(api API) { 44 p.API = api 45 } 46 47 // SetSelfRef is called by ClientMain to maintain a pointer to the plugin interface originally 48 // registered. This allows for the default implementation of OnConfigurationChange. 49 func (p *MattermostPlugin) SetSelfRef(ref interface{}) { 50 p.selfRef = ref 51 } 52 53 // OnConfigurationChange provides a default implementation of this hook event that unmarshals the 54 // plugin configuration directly onto the plugin struct. 55 // 56 // Feel free to implement your own version of OnConfigurationChange if you need more advanced 57 // configuration handling. 58 func (p *MattermostPlugin) OnConfigurationChange() error { 59 if p.selfRef != nil { 60 return p.API.LoadPluginConfiguration(p.selfRef) 61 } 62 return nil 63 }