mirror of
https://github.com/rclone/rclone.git
synced 2026-01-04 17:43:50 +00:00
lib: add plugin support
This enables loading plugins from RCLONE_PLUGIN_PATH if set.
This commit is contained in:
committed by
Nick Craig-Wood
parent
349112df6b
commit
44b603d2bd
16
lib/plugin/package.go
Normal file
16
lib/plugin/package.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Package plugin implements loading out-of-tree storage backends
|
||||
// using https://golang.org/pkg/plugin/ on Linux and macOS.
|
||||
//
|
||||
// If the $RCLONE_PLUGIN_PATH is present, any Go plugins in that dir
|
||||
// named like librcloneplugin_NAME.so will be loaded.
|
||||
//
|
||||
// To create a plugin, write the backend package like it was in-tree
|
||||
// but set the package name to "main". Then, build the plugin with
|
||||
//
|
||||
// go build -buildmode=plugin -o librcloneplugin_NAME.so
|
||||
//
|
||||
// where NAME equals the plugin's fs.RegInfo.Name.
|
||||
package plugin
|
||||
|
||||
// Build for plugin for unsupported platforms to stop go complaining
|
||||
// about "no buildable Go source files".
|
||||
41
lib/plugin/plugin.go
Normal file
41
lib/plugin/plugin.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// +build darwin linux
|
||||
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
dir := os.Getenv("RCLONE_PLUGIN_PATH")
|
||||
if dir == "" {
|
||||
return
|
||||
}
|
||||
// Get file names of plugin dir
|
||||
listing, err := ioutil.ReadDir(dir)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Failed to open plugin directory:", err)
|
||||
}
|
||||
// Enumerate file names, load valid plugins
|
||||
for _, file := range listing {
|
||||
// Match name
|
||||
fileName := file.Name()
|
||||
if !strings.HasPrefix(fileName, "librcloneplugin_") {
|
||||
continue
|
||||
}
|
||||
if !strings.HasSuffix(fileName, ".so") {
|
||||
continue
|
||||
}
|
||||
// Try to load plugin
|
||||
_, err := plugin.Open(filepath.Join(dir, fileName))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to load plugin %s: %s\n",
|
||||
fileName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user