48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package {{.name}}
|
|
|
|
import (
|
|
"apigo.cloud/git/apigo/plugin"
|
|
"errors"
|
|
"github.com/ssgo/log"
|
|
"github.com/ssgo/u"
|
|
)
|
|
|
|
var prefix = ""
|
|
|
|
func init() {
|
|
plugin.Register(plugin.Plugin{
|
|
Id: "{{.name}}",
|
|
Name: "",
|
|
// set plugin contents
|
|
Objects: map[string]interface{}{
|
|
// *plugin.Context is optional if need to use, ignore it when invoke
|
|
"set": func(key string, value interface{}, ctx *plugin.Context) {
|
|
ctx.SetData(prefix+key, value)
|
|
},
|
|
// return error is optional if need to throw exception, ignore it when invoke
|
|
"get": func(key string, ctx *plugin.Context) (interface{}, error) {
|
|
value := ctx.GetData(prefix + key)
|
|
if value == nil {
|
|
return nil, errors.New("data " + key + " is null")
|
|
}
|
|
return value, nil
|
|
},
|
|
// you can get inject object from *plugin.Context, *log.Logger is default logger with a unique trance id
|
|
"remove": func(key string, ctx *plugin.Context) {
|
|
if logger, ok := ctx.GetInject("*log.Logger").(*log.Logger); ok {
|
|
logger.Warning("context data cannot be remove, will set to null")
|
|
}
|
|
ctx.SetData(prefix+key, nil)
|
|
},
|
|
},
|
|
// set config sample (recommend to use YAML format)
|
|
ConfigSample: `prefix: _`,
|
|
// init plugin config
|
|
Init: func(config map[string]interface{}) {
|
|
if config[prefix] != nil {
|
|
prefix = u.String(config[prefix])
|
|
}
|
|
},
|
|
})
|
|
}
|