github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/resource/plugins/goplugin/plugin.go (about)

     1  package goplugin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	goplugin "plugin"
     9  
    10  	"github.com/pkg/errors"
    11  	"github.com/projecteru2/core/resource/plugins"
    12  	coretypes "github.com/projecteru2/core/types"
    13  )
    14  
    15  // NewPlugin .
    16  func NewPlugin(ctx context.Context, path string, config coretypes.Config) (plugins.Plugin, error) {
    17  	pFname, err := filepath.Abs(path)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	gp, err := goplugin.Open(pFname)
    22  	if err != nil {
    23  		return nil, errors.Wrapf(err, "failed to open plugin %s", pFname)
    24  	}
    25  	sym, err := gp.Lookup("NewPlugin")
    26  	if err != nil {
    27  		return nil, errors.Wrapf(err, "failed to lookup NewPlugin %s", pFname)
    28  	}
    29  	fn, ok := sym.(func(context.Context, coretypes.Config) (plugins.Plugin, error))
    30  	if !ok {
    31  		return nil, fmt.Errorf("NewPlugin is not a function")
    32  	}
    33  	return fn(ctx, config)
    34  }