This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
132 lines (121 loc) · 3.22 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Package plugin provides the ability to load plugins
package plugin
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"plugin"
"strings"
"text/template"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/cmd"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/transport"
mp "github.com/micro/micro/v2/plugin"
)
// Plugin is a plugin loaded from a file
type Plugin struct {
// Name of the plugin e.g rabbitmq
Name string
// Type of the plugin e.g broker
Type string
// Path specifies the import path
Path string
// NewFunc creates an instance of the plugin
NewFunc interface{}
}
// Init sets up the plugin
func Init(p *Plugin) error {
switch p.Type {
case "micro":
pg, ok := p.NewFunc.(func() mp.Plugin)
if !ok {
return fmt.Errorf("Invalid plugin %s", p.Name)
}
mp.Register(pg())
case "broker":
pg, ok := p.NewFunc.(func(...broker.Option) broker.Broker)
if !ok {
return fmt.Errorf("Invalid plugin %s", p.Name)
}
cmd.DefaultBrokers[p.Name] = pg
case "client":
pg, ok := p.NewFunc.(func(...client.Option) client.Client)
if !ok {
return fmt.Errorf("Invalid plugin %s", p.Name)
}
cmd.DefaultClients[p.Name] = pg
case "registry":
pg, ok := p.NewFunc.(func(...registry.Option) registry.Registry)
if !ok {
return fmt.Errorf("Invalid plugin %s", p.Name)
}
cmd.DefaultRegistries[p.Name] = pg
case "server":
pg, ok := p.NewFunc.(func(...server.Option) server.Server)
if !ok {
return fmt.Errorf("Invalid plugin %s", p.Name)
}
cmd.DefaultServers[p.Name] = pg
case "transport":
pg, ok := p.NewFunc.(func(...transport.Option) transport.Transport)
if !ok {
return fmt.Errorf("Invalid plugin %s", p.Name)
}
cmd.DefaultTransports[p.Name] = pg
}
return fmt.Errorf("Unknown plugin type: %s for %s", p.Type, p.Name)
}
// Load loads a plugin created with `go build -buildmode=plugin`
func Load(path string) (*Plugin, error) {
p, err := plugin.Open(path)
if err != nil {
return nil, err
}
s, err := p.Lookup("Plugin")
if err != nil {
return nil, err
}
pl, ok := s.(*Plugin)
if !ok {
return nil, errors.New("could not cast Plugin object")
}
return pl, nil
}
// Generate creates a go file at the specified path.
// You must use `go build -buildmode=plugin`to build it.
func Generate(path string, p *Plugin) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
t, err := template.New(p.Name).Parse(tmpl)
if err != nil {
return err
}
return t.Execute(f, p)
}
// Build generates a dso plugin using the go command `go build -buildmode=plugin`
func Build(path string, p *Plugin) error {
path = strings.TrimSuffix(path, ".so")
// create go file in tmp path
temp := os.TempDir()
base := filepath.Base(path)
goFile := filepath.Join(temp, base+".go")
// generate .go file
if err := Generate(goFile, p); err != nil {
return err
}
// remove .go file
defer os.Remove(goFile)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil && !os.IsExist(err) {
return fmt.Errorf("Failed to create dir %s: %v", filepath.Dir(path), err)
}
c := exec.Command("go", "build", "-buildmode=plugin", "-o", path+".so", goFile)
return c.Run()
}