Require goplugin build flag to enable go plugin support (#6393)

This commit is contained in:
Daniel Nelson
2019-09-20 16:50:19 -07:00
committed by GitHub
parent 46b89b379a
commit 776e92ffab
3 changed files with 53 additions and 34 deletions

View File

@@ -0,0 +1,9 @@
// +build !goplugin
package goplugin
import "errors"
func LoadExternalPlugins(rootDir string) error {
return errors.New("go plugin support is not enabled")
}

View File

@@ -0,0 +1,42 @@
// +build goplugin
package goplugin
import (
"fmt"
"os"
"path"
"path/filepath"
"plugin"
"strings"
)
// loadExternalPlugins loads external plugins from shared libraries (.so, .dll, etc.)
// in the specified directory.
func LoadExternalPlugins(rootDir string) error {
return filepath.Walk(rootDir, func(pth string, info os.FileInfo, err error) error {
// Stop if there was an error.
if err != nil {
return err
}
// Ignore directories.
if info.IsDir() {
return nil
}
// Ignore files that aren't shared libraries.
ext := strings.ToLower(path.Ext(pth))
if ext != ".so" && ext != ".dll" {
return nil
}
// Load plugin.
_, err = plugin.Open(pth)
if err != nil {
return fmt.Errorf("error loading %s: %s", pth, err)
}
return nil
})
}