Skip to content

Commit

Permalink
Allow plugins to be resided in subdirectories
Browse files Browse the repository at this point in the history
- clean output after running tests
- add formatting to make chain
  • Loading branch information
prskr committed Apr 1, 2020
1 parent 02d8b44 commit 0ed9070
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 18 additions & 7 deletions internal/plugins/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/path/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 2 additions & 1 deletion pkg/plugins/http_mock/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugins/tls_interceptor/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0ed9070

Please sign in to comment.