Skip to content

Commit 80447a0

Browse files
committed
Migrated plugin system from Muxy and improved test coverage
0 parents  commit 80447a0

13 files changed

+942
-0
lines changed

.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
bin
3+
pkg
4+
*.o
5+
*.a
6+
*.so
7+
8+
# Folders
9+
_obj
10+
_test
11+
12+
# Architecture specific extensions/prefixes
13+
*.[568vq]
14+
[568vq].out
15+
16+
*.cgo1.go
17+
*.cgo2.c
18+
_cgo_defun.c
19+
_cgo_gotypes.go
20+
_cgo_export.*
21+
22+
_testmain.go
23+
24+
*.exe
25+
*.test
26+
*.prof

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Matt Fellows
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
TEST?=./...
2+
3+
default: test
4+
5+
bin:
6+
@sh -c "$(CURDIR)/scripts/build.sh"
7+
8+
dev:
9+
@TF_DEV=1 sh -c "$(CURDIR)/scripts/build.sh"
10+
11+
test:
12+
"$(CURDIR)/scripts/test.sh"
13+
14+
testrace:
15+
go test -race $(TEST) $(TESTARGS)
16+
17+
updatedeps:
18+
go get -d -v -p 2 ./...
19+
20+
.PHONY: bin default dev test updatedeps

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Plugo
2+
3+
Simple Plugin abstraction with built-in configuration capability. Plugo provides a simple API to register and lookup plugins,
4+
and then inject configuration into them from a YAML/JSON configuration file.
5+
6+
## Creating a Plugin
7+
8+
First, create your plugin `struct`:
9+
10+
thing.go:
11+
12+
```golang
13+
type Thing struct {
14+
Name string
15+
Age int
16+
}
17+
18+
// Register with plugo with name 'fireThing'
19+
func init() {
20+
plugo.PluginFactories.Register(func() (interface{}, error) {
21+
return &Thing{}, nil
22+
}, "fireThing")
23+
}
24+
25+
func (t *Thing) WhoAmiI() {
26+
log.Printf("My name is %s and I'm %d", t.Name, t.Age)
27+
}
28+
29+
type PluginThing interface {
30+
DoSomething()
31+
}
32+
type MockPluginThing struct {
33+
}
34+
35+
func (m MockPluginThing) DoSomething() {
36+
log.Println("Doing something!")
37+
}
38+
```
39+
40+
Then in your main class, load the configuration from file (or in this case, hardcoded) and unmarshall the plugins,
41+
complete with configuration applied:
42+
43+
main.go:
44+
45+
```
46+
type MyPluginConfig struct {
47+
Port int
48+
Name string
49+
Things []PluginConfig
50+
}
51+
52+
func main() {
53+
var data = []byte(`
54+
port: 8080
55+
name: Foo
56+
things:
57+
- name: fireThing
58+
config:
59+
name: bar
60+
age: 21
61+
`)
62+
63+
// Load Configuration
64+
var confLoader *plugo.ConfigLoader
65+
c := &MyPluginConfig{}
66+
confLoader.Load(data, c)
67+
68+
// Load all plugins
69+
things := make([]*Thing, len(c.Things))
70+
plugins := plugo.LoadPluginsWithConfig(confLoader, c.Things)
71+
for i, p := range plugins {
72+
things[i] = p.(*Thing)
73+
things[i].WhoAmiI()
74+
}
75+
}
76+
77+
```

0 commit comments

Comments
 (0)