diff --git a/Makefile b/Makefile index 80d4892..13bd439 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ BINARY_NAME = inetmock PLUGINS = $(wildcard $(DIR)pkg/plugins/*/.) DEBUG_PORT = 2345 DEBUG_ARGS?= --development-logs=true -INETMOCK_PLUGINS_DIRECTORY = $(DIR)plugins +INETMOCK_PLUGINS_DIRECTORY = $(DIR) .PHONY: clean all format deps compile debug test cli-cover-report html-cover-report plugins $(PLUGINS) diff --git a/internal/plugins/loading.go b/internal/plugins/loading.go index c933976..73bb46a 100644 --- a/internal/plugins/loading.go +++ b/internal/plugins/loading.go @@ -3,13 +3,17 @@ package plugins import ( "fmt" "github.com/baez90/inetmock/pkg/api" + "github.com/baez90/inetmock/pkg/path" "github.com/spf13/cobra" + "os" "path/filepath" "plugin" + "regexp" ) var ( - registry HandlerRegistry + registry HandlerRegistry + pluginFileNamePattern = regexp.MustCompile(`[\w\-]+\.so$`) ) type HandlerRegistry interface { @@ -52,15 +56,22 @@ func (h *handlerRegistry) RegisterHandler(handlerName string, handlerProvider ap } func (h *handlerRegistry) LoadPlugins(pluginsPath string) (err error) { - var plugins []string - if plugins, err = filepath.Glob(fmt.Sprintf("%s%c*.so", pluginsPath, filepath.Separator)); err != nil { + + if !path.DirExists(pluginsPath) { + err = fmt.Errorf("plugins path %s does not exist or is not accessible", pluginsPath) return } - - for _, pluginSo := range plugins { - if _, err = plugin.Open(pluginSo); err != nil { - return + err = filepath.Walk(pluginsPath, func(path string, info os.FileInfo, err error) error { + if !info.IsDir() && pluginFileNamePattern.MatchString(info.Name()) { + if _, err := plugin.Open(path); err != nil { + return err + } } + return nil + }) + + if err != nil { + return } err = nil diff --git a/pkg/path/helpers.go b/pkg/path/helpers.go index d0e428c..5ca4c62 100644 --- a/pkg/path/helpers.go +++ b/pkg/path/helpers.go @@ -17,3 +17,11 @@ func FileExists(filename string) bool { } return !info.IsDir() } + +func DirExists(dirPath string) bool { + info, err := os.Stat(dirPath) + if os.IsNotExist(err) { + return false + } + return info.IsDir() +} diff --git a/pkg/plugins/http_mock/Makefile b/pkg/plugins/http_mock/Makefile index d03d9ad..47da7bf 100644 --- a/pkg/plugins/http_mock/Makefile +++ b/pkg/plugins/http_mock/Makefile @@ -12,7 +12,7 @@ DEBUG_PORT = 2345 .PHONY: deps format compile test cli-cover-report html-cover-report -all: compile test +all: format compile test deps: @go mod tidy @@ -35,6 +35,7 @@ endif test: @go test -coverprofile=./cov-raw.out -v $(TEST_PKGS) @cat ./cov-raw.out | grep -v "generated" > ./cov.out + @rm -f $(DIR)$(PLUGIN_NAME) cli-cover-report: @go tool cover -func=cov.out diff --git a/pkg/plugins/tls_interceptor/Makefile b/pkg/plugins/tls_interceptor/Makefile index 9e5e4f4..dc01810 100644 --- a/pkg/plugins/tls_interceptor/Makefile +++ b/pkg/plugins/tls_interceptor/Makefile @@ -12,7 +12,7 @@ DEBUG_PORT = 2345 .PHONY: deps format compile test cli-cover-report html-cover-report -all: compile test +all: format compile test deps: @go mod tidy @@ -35,6 +35,7 @@ endif test: @go test -coverprofile=./cov-raw.out -v $(TEST_PKGS) @cat ./cov-raw.out | grep -v "generated" > ./cov.out + @rm -f $(DIR)$(PLUGIN_NAME) cli-cover-report: @go tool cover -func=cov.out