/
plugin.go
178 lines (144 loc) · 7 KB
/
plugin.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
// Autogenerated by github.com/wendigo/go-bind-plugin on 2016-11-12 19:45:36.989271091 +0100 CET, do not edit!
// Command: go-bind-plugin -plugin-path ./internal/test_fixtures/generated/basic_plugin/plugin.so -plugin-package ./internal/test_fixtures/basic_plugin -output-name TestWrapper -output-path ./internal/test_fixtures/generated/basic_plugin/plugin.go -output-package main -sha256 -rebuild
//
// Plugin ./internal/test_fixtures/generated/basic_plugin/plugin.so info:
// - package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
// - size: 2447824 bytes
// - sha256: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"plugin"
"reflect"
"strings"
)
// TestWrapper wraps symbols (functions and variables) exported by plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
type TestWrapper struct {
// Exported functions
_NonReturningFunction func()
_ReturningInt32 func() int32
_ReturningIntArray func() [3]int32
_ReturningStringSlice func() []string
// Exported variables (public references)
}
// NonReturningFunction function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'NonReturningFunction'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#NonReturningFunction
func (p *TestWrapper) NonReturningFunction() {
p._NonReturningFunction()
}
// ReturningInt32 function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningInt32'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningInt32
func (p *TestWrapper) ReturningInt32() int32 {
return p._ReturningInt32()
}
// ReturningIntArray function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningIntArray'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningIntArray
func (p *TestWrapper) ReturningIntArray() [3]int32 {
return p._ReturningIntArray()
}
// ReturningStringSlice function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningStringSlice'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningStringSlice
func (p *TestWrapper) ReturningStringSlice() []string {
return p._ReturningStringSlice()
}
// String returnes textual representation of the wrapper. It provides info on exported symbols and variables.
func (p *TestWrapper) String() string {
var lines []string
lines = append(lines, "Struct TestWrapper:")
lines = append(lines, "\t- Generated on: 2016-11-12 19:45:36.989271091 +0100 CET")
lines = append(lines, "\t- Command: go-bind-plugin -plugin-path ./internal/test_fixtures/generated/basic_plugin/plugin.so -plugin-package ./internal/test_fixtures/basic_plugin -output-name TestWrapper -output-path ./internal/test_fixtures/generated/basic_plugin/plugin.go -output-package main -sha256 -rebuild")
lines = append(lines, "\nPlugin info:")
lines = append(lines, "\t- package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin")
lines = append(lines, "\t- sha256 sum: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483")
lines = append(lines, "\t- size: 2447824 bytes")
lines = append(lines, "\nExported functions (4):")
lines = append(lines, "\t- NonReturningFunction func()")
lines = append(lines, "\t- ReturningInt32 func() (int32)")
lines = append(lines, "\t- ReturningIntArray func() ([3]int32)")
lines = append(lines, "\t- ReturningStringSlice func() ([]string)")
return strings.Join(lines, "\n")
}
// BindTestWrapper loads plugin from the given path and binds symbols (variables and functions)
// to the TestWrapper struct.
// When plugin is loaded sha256 checksum is computed and checked against precomputed once. On mismatch error is returned.
func BindTestWrapper(path string) (*TestWrapper, error) {
p, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open plugin: %s", err)
}
fileChecksum := func(path string) (string, error) {
hasher := sha256.New()
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
checksum, err := fileChecksum(path)
if err != nil {
return nil, fmt.Errorf("could not calculate file %s checksum", path)
}
if checksum != "e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483" {
return nil, fmt.Errorf("SHA256 checksum mismatch (expected: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483, actual: %s)", checksum)
}
ret := new(TestWrapper)
funcNonReturningFunction, err := p.Lookup("NonReturningFunction")
if err != nil {
return nil, fmt.Errorf("could not import function 'NonReturningFunction', symbol not found: %s", err)
}
if typed, ok := funcNonReturningFunction.(func()); ok {
ret._NonReturningFunction = typed
} else {
return nil, fmt.Errorf("could not import function 'NonReturningFunction', incompatible types 'func()' and '%s'", reflect.TypeOf(funcNonReturningFunction))
}
funcReturningInt32, err := p.Lookup("ReturningInt32")
if err != nil {
return nil, fmt.Errorf("could not import function 'ReturningInt32', symbol not found: %s", err)
}
if typed, ok := funcReturningInt32.(func() int32); ok {
ret._ReturningInt32 = typed
} else {
return nil, fmt.Errorf("could not import function 'ReturningInt32', incompatible types 'func() (int32)' and '%s'", reflect.TypeOf(funcReturningInt32))
}
funcReturningIntArray, err := p.Lookup("ReturningIntArray")
if err != nil {
return nil, fmt.Errorf("could not import function 'ReturningIntArray', symbol not found: %s", err)
}
if typed, ok := funcReturningIntArray.(func() [3]int32); ok {
ret._ReturningIntArray = typed
} else {
return nil, fmt.Errorf("could not import function 'ReturningIntArray', incompatible types 'func() ([3]int32)' and '%s'", reflect.TypeOf(funcReturningIntArray))
}
funcReturningStringSlice, err := p.Lookup("ReturningStringSlice")
if err != nil {
return nil, fmt.Errorf("could not import function 'ReturningStringSlice', symbol not found: %s", err)
}
if typed, ok := funcReturningStringSlice.(func() []string); ok {
ret._ReturningStringSlice = typed
} else {
return nil, fmt.Errorf("could not import function 'ReturningStringSlice', incompatible types 'func() ([]string)' and '%s'", reflect.TypeOf(funcReturningStringSlice))
}
return ret, nil
}
func main() {
pl, err := BindTestWrapper("./internal/test_fixtures/generated/basic_plugin/plugin.so")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(pl.ReturningIntArray())
}