github.com/influxdata/telegraf@v1.30.3/internal/goplugin/plugin.go (about)

     1  //go:build goplugin
     2  
     3  package goplugin
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  	"plugin"
    11  	"strings"
    12  )
    13  
    14  // loadExternalPlugins loads external plugins from shared libraries (.so, .dll, etc.)
    15  // in the specified directory.
    16  func LoadExternalPlugins(rootDir string) error {
    17  	return filepath.Walk(rootDir, func(pth string, info os.FileInfo, err error) error {
    18  		// Stop if there was an error.
    19  		if err != nil {
    20  			return err
    21  		}
    22  
    23  		// Ignore directories.
    24  		if info.IsDir() {
    25  			return nil
    26  		}
    27  
    28  		// Ignore files that aren't shared libraries.
    29  		ext := strings.ToLower(path.Ext(pth))
    30  		if ext != ".so" && ext != ".dll" {
    31  			return nil
    32  		}
    33  
    34  		// Load plugin.
    35  		_, err = plugin.Open(pth)
    36  		if err != nil {
    37  			return fmt.Errorf("error loading %s: %s", pth, err)
    38  		}
    39  
    40  		return nil
    41  	})
    42  }