feat(cmd) Add CLI command and option to use as a plugin info tool

- -dir <dir>  option sets the starting plugin directory
- -dump <name>  command returns the same data as the `plugin.GetPluginInfo()`
   method.  The result is dumped as a MessagePack object on stdout.
This commit is contained in:
Javier Guerra
2019-12-18 23:32:21 -05:00
committed by Guilherme Salazar
parent da8ce4fa7e
commit 0d2cf25cab
2 changed files with 53 additions and 6 deletions

39
main.go
View File

@@ -13,11 +13,38 @@ import (
)
var kongPrefix = flag.String("kong-prefix", "/usr/local/kong", "Kong prefix path (specified by the `-p` argument commonly used in the `kong` cli)")
var dump = flag.String("dump-plugin-info", "", "Dump info about `plugin` as a MessagePack object")
var pluginsDir = flag.String("plugins-directory", "", "Set directory `path` where to search plugins")
var socket string
func init() {
flag.Parse()
socket = *kongPrefix + "/" + "go_pluginserver.sock"
if *kongPrefix == "" && *dump == "" {
flag.Usage()
os.Exit(2)
}
}
func dumpInfo() {
s := newServer()
info := PluginInfo{}
err := s.GetPluginInfo(*dump, &info)
if err != nil {
log.Printf("%s", err)
}
var handle codec.MsgpackHandle
handle.ReaderBufferSize = 4096
handle.WriterBufferSize = 4096
handle.RawToString = true
handle.MapType = reflect.TypeOf(map[string]interface{}(nil))
enc := codec.NewEncoder(os.Stdout, &handle)
_ = enc.Encode(info)
}
func runServer(listener net.Listener) {
@@ -42,7 +69,7 @@ func runServer(listener net.Listener) {
}
}
func main() {
func startServer() {
err := os.Remove(socket)
if err != nil && !os.IsNotExist(err) {
log.Printf(`removing "%s": %s`, kongPrefix, err)
@@ -59,3 +86,13 @@ func main() {
runServer(listener)
}
func main() {
if *dump != "" {
dumpInfo()
}
if socket != "" {
startServer()
}
}

View File

@@ -26,11 +26,21 @@ type PluginServer struct {
// Create a new server context.
func newServer() *PluginServer {
return &PluginServer{
s := PluginServer{
plugins: map[string]*pluginData{},
instances: map[int]*instanceData{},
events: map[int]*eventData{},
}
if *pluginsDir == "" {
if dir, err := os.Getwd(); err == nil {
s.pluginsDir = dir
}
} else {
s.pluginsDir = *pluginsDir
}
return &s
}
// SetPluginDir tells the server where to find the plugins.
@@ -213,8 +223,8 @@ func getSchemaDict(t reflect.Type) schemaDict {
// Information obtained from a plugin's compiled code.
type PluginInfo struct {
Name string // plugin name
ModTime time.Time // plugin file modification time
LoadTime time.Time // plugin load time
ModTime time.Time `codec:",omitempty"` // plugin file modification time
LoadTime time.Time `codec:",omitempty"` // plugin load time
Phases []string // events it can handle
Version string // version number
Priority int // priority info