Skip to content

Commit e260423

Browse files
author
Aleksandr Andryashin
committed
Initial commit
1 parent 1750c70 commit e260423

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
plug
2+
13
# Compiled Object files, Static and Dynamic libs (Shared Objects)
24
*.o
35
*.a

Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM debian:jessie-slim
2+
3+
RUN mkdir /plugins
4+
5+
ADD plug /plugins
6+
ADD plugins/one/one.so /plugins
7+
ADD plugins/two/two.so /plugins
8+
9+
CMD ["/plugins/plug", "/plugins/one.so", "/plugins/two.so"]

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
all:
2+
docker run -v$(HOME):/root --rm golang:1.8 /bin/bash -c 'cd /root/go/src/github.com/aandryashin/plug/plugins/one && GOPATH=/root/go go build -buildmode plugin -o one.so'
3+
docker run -v$(HOME):/root --rm golang:1.8 /bin/bash -c 'cd /root/go/src/github.com/aandryashin/plug/plugins/two && GOPATH=/root/go go build -buildmode plugin -o two.so'
4+
docker run -v$(HOME):/root --rm golang:1.8 /bin/bash -c 'cd /root/go/src/github.com/aandryashin/plug && GOPATH=/root/go go build'
5+
docker build -t plug:0.1.0 .
6+
7+
clean:
8+
rm plug
9+
rm plugins/one/one.so
10+
rm plugins/two/two.so
11+
docker rmi -f plug:0.1.0
12+

main.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/aandryashin/plug/so"
6+
"os"
7+
"plugin"
8+
)
9+
10+
func main() {
11+
if len(os.Args) < 2 {
12+
fmt.Printf("Usage:\n\t%s file1.so file2.so...\n", os.Args[0])
13+
}
14+
for _, f := range os.Args[1:] {
15+
p, err := plugin.Open(f)
16+
if err != nil {
17+
fmt.Printf("%s: load %s: %v\n", os.Args[0], f, err)
18+
os.Exit(1)
19+
}
20+
21+
f, err := p.Lookup("New")
22+
if err != nil {
23+
fmt.Printf("%s: lookup interface: %v\n", os.Args[0], err)
24+
os.Exit(1)
25+
}
26+
27+
fn, ok := f.(func() so.SO)
28+
if !ok {
29+
fmt.Printf("%s: verify signature: %v\n", os.Args[0], err)
30+
os.Exit(1)
31+
}
32+
33+
fn().Run()
34+
}
35+
}

plugins/one/plugin.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/aandryashin/plug/so"
6+
)
7+
8+
type Plugin struct {
9+
name string
10+
value string
11+
}
12+
13+
func (p *Plugin) Run() {
14+
fmt.Printf("I am %s plugin [%s]\n", p.name, p.value)
15+
}
16+
17+
func New() so.SO {
18+
return &Plugin{"String", "Hello, world!"}
19+
}

plugins/two/plugin.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/aandryashin/plug/so"
6+
)
7+
8+
type Plugin struct {
9+
name string
10+
value int
11+
}
12+
13+
func (p *Plugin) Run() {
14+
fmt.Printf("I am %s plugin [%d]\n", p.name, p.value)
15+
}
16+
17+
func New() so.SO {
18+
return &Plugin{"Integer", 17}
19+
}

so/so.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package so
2+
3+
type SO interface {
4+
Run()
5+
}

0 commit comments

Comments
 (0)