Skip to content

Commit

Permalink
Add plugins manager
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Jun 22, 2020
1 parent 9d8a8b9 commit 0af9f68
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
fast_finish: true
before_install:
- go get github.com/mattn/goveralls
- go build --buildmode=plugin -o testdata/foo.so testdata/foo.go
script:
- go test -v -covermode=count -coverprofile=coverage.out ./...
- go vet ./...
Expand Down
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
#
[![Build Status](https://img.shields.io/travis/clevergo/____?style=for-the-badge)](https://travis-ci.org/clevergo/____)
[![Coverage Status](https://img.shields.io/coveralls/github/clevergo/____?style=for-the-badge)](https://coveralls.io/github/clevergo/____)
[![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)
[![Go Report Card](https://goreportcard.com/badge/github.com/clevergo/____?style=for-the-badge)](https://goreportcard.com/report/github.com/clevergo/____)
[![Release](https://img.shields.io/github/release/clevergo/____.svg?style=for-the-badge)](https://github.com/clevergo/____/releases)
# Go plugins manager
[![Build Status](https://img.shields.io/travis/clevergo/plugins?style=for-the-badge)](https://travis-ci.org/clevergo/plugins)
[![Coverage Status](https://img.shields.io/coveralls/github/clevergo/plugins?style=for-the-badge)](https://coveralls.io/github/clevergo/plugins)
[![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)
[![Go Report Card](https://goreportcard.com/badge/github.com/clevergo/plugins?style=for-the-badge)](https://goreportcard.com/report/github.com/clevergo/plugins)
[![Release](https://img.shields.io/github/release/clevergo/plugins.svg?style=for-the-badge)](https://github.com/clevergo/plugins/releases)

The `plugins` is a simple Go plugins manager.

## Installation

```shell
$ go get -u clevergo.tech/plugins
```

## Usage

```go
// Plugins location.
path := "/path/to/plugins"
// Creates a plugins manager.
m := plugins.New(path)

// Opens a Go plugins which located at {path}/foo.so
p, err := m.Open("foo.so")

// Lookup a symbol in a plugin.
sym, err := m.Lookup("foo.so", "Bar")
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module clevergo.tech/____
module clevergo.tech/plugins

go 1.13

require github.com/stretchr/testify v1.5.1 // indirect
require github.com/stretchr/testify v1.5.1
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
37 changes: 37 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

package plugins

import (
"path"
"plugin"
)

// Manager is a manager that manage plugins.
type Manager struct {
// The location of plugins.
Path string
}

// New returns a plugins manager with the given path.
func New(path string) *Manager {
return &Manager{
Path: path,
}
}

// Open opens a Go plugin.
func (pm *Manager) Open(name string) (*plugin.Plugin, error) {
return plugin.Open(path.Join(pm.Path, name))
}

// Lookup searchs for a symbol named symName in a plugin named pluginName.
func (pm *Manager) Lookup(pluginName, symName string) (plugin.Symbol, error) {
p, err := pm.Open(pluginName)
if err != nil {
return nil, err
}
return p.Lookup(symName)
}
48 changes: 48 additions & 0 deletions manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

package plugins

import (
"path"
"plugin"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
for _, path := range []string{"foo", "bar"} {
m := New(path)
assert.Equal(t, path, m.Path)
}
}

func TestManagerOpen(t *testing.T) {
dir := "testdata"
m := New(dir)

for _, name := range []string{"foo.so", "bar.so"} {
expectedP, expectedErr := plugin.Open(path.Join(dir, name))
p, err := m.Open(name)
assert.Equal(t, expectedErr, err)
assert.Equal(t, expectedP, p)
}
}

func TestManagerLookup(t *testing.T) {
dir := "testdata"
m := New(dir)

_, err := m.Lookup("nil", "Foo")
assert.NotNil(t, err)

_, err = m.Lookup("foo.so", "Bar")
assert.NotNil(t, err)

sym, err := m.Lookup("foo.so", "Foo")
assert.Nil(t, err)
f := sym.(func() string)
assert.Equal(t, "Bar", f())
}
1 change: 1 addition & 0 deletions testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.so
9 changes: 9 additions & 0 deletions testdata/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

// Foo returns Bar.
func Foo() string {
return "Bar"
}

func mian() {
}

0 comments on commit 0af9f68

Please sign in to comment.