Allow plugins to be resided in subdirectories
- clean output after running tests - add formatting to make chain
This commit is contained in:
parent
02d8b444e3
commit
0ed90708d8
5 changed files with 31 additions and 10 deletions
2
Makefile
2
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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue