-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·76 lines (65 loc) · 1.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"plugin"
)
func main() {
plugNum := "1"
if len(os.Args) == 2 {
plugNum = os.Args[1]
}
var mod string
var f string
switch plugNum {
case "1":
mod = "out/plug1.so"
f = "GreetUniverse"
case "2":
mod = "out/plug2.so"
f = "GreetWorld"
default:
log.Println("plugin did not chose")
os.Exit(1)
}
log.Println("try open plugin:", plugNum, f)
cwd, err := os.Getwd()
if err != nil {
log.Println(err)
os.Exit(1)
}
cmd := exec.Command("ls", "-l")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
log.Fatal("cmd:", err)
}
log.Println(out.String())
path := filepath.Join(cwd, mod)
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Println(mod, err)
os.Exit(0)
}
plug, err := plugin.Open(path)
if err != nil {
log.Println(err)
os.Exit(1)
}
sym, err := plug.Lookup(f)
if err != nil {
log.Println(err)
os.Exit(1)
}
greeter, ok := sym.(func() string)
if !ok {
fmt.Println("unexpected type from module symbol")
os.Exit(1)
}
res := greeter()
log.Println("greeter: ", res)
}