From 3466037421d01c1e9e3234e670317a06359c68fe Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Tue, 28 Apr 2020 19:47:58 +0200 Subject: [PATCH] Remove plugin commands feature - remove option to expose commands from a plugins - remove dynamic command loading to make startup process easier --- internal/cmd/plugins.go | 17 ---------- internal/cmd/root.go | 4 --- internal/plugins/loading.go | 21 ++----------- internal/plugins/loading_test.go | 53 ++------------------------------ 4 files changed, 5 insertions(+), 90 deletions(-) delete mode 100644 internal/cmd/plugins.go diff --git a/internal/cmd/plugins.go b/internal/cmd/plugins.go deleted file mode 100644 index 4091505..0000000 --- a/internal/cmd/plugins.go +++ /dev/null @@ -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. -`, - } -) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index f0c8550..12f954f 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -36,7 +36,6 @@ func init() { rootCmd.AddCommand( serveCmd, generateCaCmd, - pluginsCmd, ) } @@ -81,9 +80,6 @@ func onInit() { "loading plugins completed", zap.Duration("pluginLoadDuration", pluginLoadDuration), ) - - pluginsCmd.AddCommand(registry.PluginCommands()...) - } func ExecuteRootCommand() error { diff --git a/internal/plugins/loading.go b/internal/plugins/loading.go index cb68924..b826c70 100644 --- a/internal/plugins/loading.go +++ b/internal/plugins/loading.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/baez90/inetmock/pkg/api" "github.com/baez90/inetmock/pkg/path" - "github.com/spf13/cobra" "os" "path/filepath" "plugin" @@ -21,14 +20,12 @@ var ( type HandlerRegistry interface { LoadPlugins(pluginsPath string) error AvailableHandlers() []string - RegisterHandler(handlerName string, handlerProvider api.PluginInstanceFactory, subCommands ...*cobra.Command) + RegisterHandler(handlerName string, handlerProvider api.PluginInstanceFactory) HandlerForName(handlerName string) (api.ProtocolHandler, bool) - PluginCommands() []*cobra.Command } type handlerRegistry struct { - handlers map[string]api.PluginInstanceFactory - pluginCommands []*cobra.Command + handlers map[string]api.PluginInstanceFactory } func (h handlerRegistry) AvailableHandlers() (availableHandlers []string) { @@ -38,10 +35,6 @@ func (h handlerRegistry) AvailableHandlers() (availableHandlers []string) { return } -func (h handlerRegistry) PluginCommands() []*cobra.Command { - return h.pluginCommands -} - func (h *handlerRegistry) HandlerForName(handlerName string) (instance api.ProtocolHandler, ok bool) { handlerName = strings.ToLower(handlerName) var provider api.PluginInstanceFactory @@ -51,20 +44,12 @@ func (h *handlerRegistry) HandlerForName(handlerName string) (instance api.Proto 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) if _, exists := h.handlers[handlerName]; exists { panic(fmt.Sprintf("handler with name %s is already registered - there's something strange...in the neighborhood", handlerName)) } 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) { diff --git a/internal/plugins/loading_test.go b/internal/plugins/loading_test.go index 7ad9ae7..21c9ee2 100644 --- a/internal/plugins/loading_test.go +++ b/internal/plugins/loading_test.go @@ -2,61 +2,13 @@ package plugins import ( "github.com/baez90/inetmock/pkg/api" - "github.com/spf13/cobra" "reflect" "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) { type fields struct { - handlers map[string]api.PluginInstanceFactory - pluginCommands []*cobra.Command + handlers map[string]api.PluginInstanceFactory } type args struct { handlerName string @@ -94,8 +46,7 @@ func Test_handlerRegistry_HandlerForName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := &handlerRegistry{ - handlers: tt.fields.handlers, - pluginCommands: tt.fields.pluginCommands, + handlers: tt.fields.handlers, } gotInstance, gotOk := h.HandlerForName(tt.args.handlerName) if !reflect.DeepEqual(gotInstance, tt.wantInstance) {