generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} |