Skip to content

Commit 0af9f68

Browse files
committed
Add plugins manager
1 parent 9d8a8b9 commit 0af9f68

File tree

8 files changed

+128
-8
lines changed

8 files changed

+128
-8
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
fast_finish: true
1515
before_install:
1616
- go get github.com/mattn/goveralls
17+
- go build --buildmode=plugin -o testdata/foo.so testdata/foo.go
1718
script:
1819
- go test -v -covermode=count -coverprofile=coverage.out ./...
1920
- go vet ./...

README.md

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
#
2-
[![Build Status](https://img.shields.io/travis/clevergo/____?style=for-the-badge)](https://travis-ci.org/clevergo/____)
3-
[![Coverage Status](https://img.shields.io/coveralls/github/clevergo/____?style=for-the-badge)](https://coveralls.io/github/clevergo/____)
4-
[![Go.Dev reference](https://img.shields.io/badge/go.dev-reference-blue?logo=go&logoColor=white&style=for-the-badge)](https://pkg.go.dev/clevergo.tech/____?tab=doc)
5-
[![Go Report Card](https://goreportcard.com/badge/github.com/clevergo/____?style=for-the-badge)](https://goreportcard.com/report/github.com/clevergo/____)
6-
[![Release](https://img.shields.io/github/release/clevergo/____.svg?style=for-the-badge)](https://github.com/clevergo/____/releases)
1+
# Go plugins manager
2+
[![Build Status](https://img.shields.io/travis/clevergo/plugins?style=for-the-badge)](https://travis-ci.org/clevergo/plugins)
3+
[![Coverage Status](https://img.shields.io/coveralls/github/clevergo/plugins?style=for-the-badge)](https://coveralls.io/github/clevergo/plugins)
4+
[![Go.Dev reference](https://img.shields.io/badge/go.dev-reference-blue?logo=go&logoColor=white&style=for-the-badge)](https://pkg.go.dev/clevergo.tech/plugins?tab=doc)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/clevergo/plugins?style=for-the-badge)](https://goreportcard.com/report/github.com/clevergo/plugins)
6+
[![Release](https://img.shields.io/github/release/clevergo/plugins.svg?style=for-the-badge)](https://github.com/clevergo/plugins/releases)
7+
8+
The `plugins` is a simple Go plugins manager.
9+
10+
## Installation
11+
12+
```shell
13+
$ go get -u clevergo.tech/plugins
14+
```
15+
16+
## Usage
17+
18+
```go
19+
// Plugins location.
20+
path := "/path/to/plugins"
21+
// Creates a plugins manager.
22+
m := plugins.New(path)
23+
24+
// Opens a Go plugins which located at {path}/foo.so
25+
p, err := m.Open("foo.so")
26+
27+
// Lookup a symbol in a plugin.
28+
sym, err := m.Lookup("foo.so", "Bar")
29+
```

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
module clevergo.tech/____
1+
module clevergo.tech/plugins
22

33
go 1.13
44

5-
require github.com/stretchr/testify v1.5.1 // indirect
5+
require github.com/stretchr/testify v1.5.1

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
66
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
77
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
88
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
910
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1011
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1112
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

manager.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2020 CleverGo. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be found
3+
// in the LICENSE file.
4+
5+
package plugins
6+
7+
import (
8+
"path"
9+
"plugin"
10+
)
11+
12+
// Manager is a manager that manage plugins.
13+
type Manager struct {
14+
// The location of plugins.
15+
Path string
16+
}
17+
18+
// New returns a plugins manager with the given path.
19+
func New(path string) *Manager {
20+
return &Manager{
21+
Path: path,
22+
}
23+
}
24+
25+
// Open opens a Go plugin.
26+
func (pm *Manager) Open(name string) (*plugin.Plugin, error) {
27+
return plugin.Open(path.Join(pm.Path, name))
28+
}
29+
30+
// Lookup searchs for a symbol named symName in a plugin named pluginName.
31+
func (pm *Manager) Lookup(pluginName, symName string) (plugin.Symbol, error) {
32+
p, err := pm.Open(pluginName)
33+
if err != nil {
34+
return nil, err
35+
}
36+
return p.Lookup(symName)
37+
}

manager_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2020 CleverGo. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be found
3+
// in the LICENSE file.
4+
5+
package plugins
6+
7+
import (
8+
"path"
9+
"plugin"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestNew(t *testing.T) {
16+
for _, path := range []string{"foo", "bar"} {
17+
m := New(path)
18+
assert.Equal(t, path, m.Path)
19+
}
20+
}
21+
22+
func TestManagerOpen(t *testing.T) {
23+
dir := "testdata"
24+
m := New(dir)
25+
26+
for _, name := range []string{"foo.so", "bar.so"} {
27+
expectedP, expectedErr := plugin.Open(path.Join(dir, name))
28+
p, err := m.Open(name)
29+
assert.Equal(t, expectedErr, err)
30+
assert.Equal(t, expectedP, p)
31+
}
32+
}
33+
34+
func TestManagerLookup(t *testing.T) {
35+
dir := "testdata"
36+
m := New(dir)
37+
38+
_, err := m.Lookup("nil", "Foo")
39+
assert.NotNil(t, err)
40+
41+
_, err = m.Lookup("foo.so", "Bar")
42+
assert.NotNil(t, err)
43+
44+
sym, err := m.Lookup("foo.so", "Foo")
45+
assert.Nil(t, err)
46+
f := sym.(func() string)
47+
assert.Equal(t, "Bar", f())
48+
}

testdata/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.so

testdata/foo.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
// Foo returns Bar.
4+
func Foo() string {
5+
return "Bar"
6+
}
7+
8+
func mian() {
9+
}

0 commit comments

Comments
 (0)