Remove plugin commands feature

- remove option to expose commands from a plugins
- remove dynamic command loading to make startup process easier
This commit is contained in:
Peter 2020-04-28 19:47:58 +02:00 committed by baez90
parent 91f0cf6963
commit 3466037421
4 changed files with 5 additions and 90 deletions

View file

@ -1,17 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var (
pluginsCmd = &cobra.Command{
Use: "plugins",
Short: "Use the plugins prefix to interact with commands that are provided by plugins",
Long: `
The plugin prefix can be used to interact with commands that are provided by plugins.
The easiest way to explore what commands are available is to start with 'inetmock plugins' - like you did!
This help page contains a list of available sub-commands starting with the name of the plugin as a prefix.
`,
}
)

View file

@ -36,7 +36,6 @@ func init() {
rootCmd.AddCommand( rootCmd.AddCommand(
serveCmd, serveCmd,
generateCaCmd, generateCaCmd,
pluginsCmd,
) )
} }
@ -81,9 +80,6 @@ func onInit() {
"loading plugins completed", "loading plugins completed",
zap.Duration("pluginLoadDuration", pluginLoadDuration), zap.Duration("pluginLoadDuration", pluginLoadDuration),
) )
pluginsCmd.AddCommand(registry.PluginCommands()...)
} }
func ExecuteRootCommand() error { func ExecuteRootCommand() error {

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/baez90/inetmock/pkg/api" "github.com/baez90/inetmock/pkg/api"
"github.com/baez90/inetmock/pkg/path" "github.com/baez90/inetmock/pkg/path"
"github.com/spf13/cobra"
"os" "os"
"path/filepath" "path/filepath"
"plugin" "plugin"
@ -21,14 +20,12 @@ var (
type HandlerRegistry interface { type HandlerRegistry interface {
LoadPlugins(pluginsPath string) error LoadPlugins(pluginsPath string) error
AvailableHandlers() []string AvailableHandlers() []string
RegisterHandler(handlerName string, handlerProvider api.PluginInstanceFactory, subCommands ...*cobra.Command) RegisterHandler(handlerName string, handlerProvider api.PluginInstanceFactory)
HandlerForName(handlerName string) (api.ProtocolHandler, bool) HandlerForName(handlerName string) (api.ProtocolHandler, bool)
PluginCommands() []*cobra.Command
} }
type handlerRegistry struct { type handlerRegistry struct {
handlers map[string]api.PluginInstanceFactory handlers map[string]api.PluginInstanceFactory
pluginCommands []*cobra.Command
} }
func (h handlerRegistry) AvailableHandlers() (availableHandlers []string) { func (h handlerRegistry) AvailableHandlers() (availableHandlers []string) {
@ -38,10 +35,6 @@ func (h handlerRegistry) AvailableHandlers() (availableHandlers []string) {
return return
} }
func (h handlerRegistry) PluginCommands() []*cobra.Command {
return h.pluginCommands
}
func (h *handlerRegistry) HandlerForName(handlerName string) (instance api.ProtocolHandler, ok bool) { func (h *handlerRegistry) HandlerForName(handlerName string) (instance api.ProtocolHandler, ok bool) {
handlerName = strings.ToLower(handlerName) handlerName = strings.ToLower(handlerName)
var provider api.PluginInstanceFactory var provider api.PluginInstanceFactory
@ -51,20 +44,12 @@ func (h *handlerRegistry) HandlerForName(handlerName string) (instance api.Proto
return return
} }
func (h *handlerRegistry) RegisterHandler(handlerName string, handlerProvider api.PluginInstanceFactory, subCommands ...*cobra.Command) { func (h *handlerRegistry) RegisterHandler(handlerName string, handlerProvider api.PluginInstanceFactory) {
handlerName = strings.ToLower(handlerName) handlerName = strings.ToLower(handlerName)
if _, exists := h.handlers[handlerName]; exists { if _, exists := h.handlers[handlerName]; exists {
panic(fmt.Sprintf("handler with name %s is already registered - there's something strange...in the neighborhood", handlerName)) panic(fmt.Sprintf("handler with name %s is already registered - there's something strange...in the neighborhood", handlerName))
} }
h.handlers[handlerName] = handlerProvider h.handlers[handlerName] = handlerProvider
if len(subCommands) > 0 {
pluginCmds := &cobra.Command{
Use: handlerName,
}
pluginCmds.AddCommand(subCommands...)
h.pluginCommands = append(h.pluginCommands, pluginCmds)
}
} }
func (h *handlerRegistry) LoadPlugins(pluginsPath string) (err error) { func (h *handlerRegistry) LoadPlugins(pluginsPath string) (err error) {

View file

@ -2,61 +2,13 @@ package plugins
import ( import (
"github.com/baez90/inetmock/pkg/api" "github.com/baez90/inetmock/pkg/api"
"github.com/spf13/cobra"
"reflect" "reflect"
"testing" "testing"
) )
func Test_handlerRegistry_PluginCommands(t *testing.T) {
type fields struct {
handlers map[string]api.PluginInstanceFactory
pluginCommands []*cobra.Command
}
tests := []struct {
name string
fields fields
want []*cobra.Command
}{
{
name: "Default is an nil array of commands",
fields: fields{},
want: nil,
},
{
name: "Returns a copy of the given array of commands",
fields: fields{
pluginCommands: []*cobra.Command{
{
Use: "my-super-command",
Short: "bla bla bla, description",
},
},
},
want: []*cobra.Command{
{
Use: "my-super-command",
Short: "bla bla bla, description",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := handlerRegistry{
handlers: tt.fields.handlers,
pluginCommands: tt.fields.pluginCommands,
}
if got := h.PluginCommands(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PluginCommands() = %v, want %v", got, tt.want)
}
})
}
}
func Test_handlerRegistry_HandlerForName(t *testing.T) { func Test_handlerRegistry_HandlerForName(t *testing.T) {
type fields struct { type fields struct {
handlers map[string]api.PluginInstanceFactory handlers map[string]api.PluginInstanceFactory
pluginCommands []*cobra.Command
} }
type args struct { type args struct {
handlerName string handlerName string
@ -94,8 +46,7 @@ func Test_handlerRegistry_HandlerForName(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &handlerRegistry{ h := &handlerRegistry{
handlers: tt.fields.handlers, handlers: tt.fields.handlers,
pluginCommands: tt.fields.pluginCommands,
} }
gotInstance, gotOk := h.HandlerForName(tt.args.handlerName) gotInstance, gotOk := h.HandlerForName(tt.args.handlerName)
if !reflect.DeepEqual(gotInstance, tt.wantInstance) { if !reflect.DeepEqual(gotInstance, tt.wantInstance) {