-
Notifications
You must be signed in to change notification settings - Fork 6
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
4 changed files
with
108 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
TARGET := test_plugin | ||
PLUGIN_TARGET := dell7590.so | ||
|
||
build: build-plugin | ||
go build -o ${TARGET} main.go | ||
|
||
|
||
build-plugin: | ||
cd plugin && go build -buildmode=plugin -o ${PLUGIN_TARGET} && cd .. | ||
|
||
clean: | ||
rm -f plugin/${PLUGIN_TARGET} | ||
rm -f ${TARGET} | ||
|
||
rebuild: clean build |
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,6 @@ | ||
package adapter | ||
|
||
type Adapter interface { | ||
Apply() error | ||
Check() error | ||
} |
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"plugin" | ||
|
||
"reflect" | ||
|
||
"./adapter" | ||
) | ||
|
||
func testAdapter(adp adapter.Adapter) { | ||
fmt.Println("\tstart test adapter...") | ||
if err := adp.Apply(); err != nil { | ||
fmt.Println(err) | ||
} | ||
if err := adp.Check(); err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Println("\ttest done") | ||
} | ||
|
||
func main() { | ||
fmt.Println("Start test...") | ||
|
||
plg, err := plugin.Open("./plugin/dell7590.so") | ||
if err != nil { | ||
fmt.Println("Failed to open plugin:", err) | ||
return | ||
} | ||
|
||
obj, err := plg.Lookup("Object") | ||
if err != nil { | ||
fmt.Println("Failed to lookup obj:", err) | ||
return | ||
} | ||
|
||
dell7590, ok := obj.(*adapter.Adapter) | ||
if !ok { | ||
fmt.Printf("Failed to convert to adapter from Object, %#v\n", reflect.TypeOf(obj).String()) | ||
} else { | ||
testAdapter(*dell7590) | ||
} | ||
|
||
fobj, err := plg.Lookup("NewObject") | ||
if err != nil { | ||
fmt.Println("Failed to lookup NewObject:", err) | ||
return | ||
} | ||
|
||
ret := fobj.(func() adapter.Adapter)() | ||
if ret == nil { | ||
fmt.Println("Failed to convert to adapter from NewObject") | ||
} else { | ||
testAdapter(ret) | ||
} | ||
fmt.Println("Done") | ||
} |
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,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "../adapter" | ||
) | ||
|
||
type Dell7590 struct{} | ||
|
||
func (info *Dell7590) Apply() error { | ||
fmt.Println("Apply dell 7590") | ||
return nil | ||
} | ||
|
||
func (info *Dell7590) Check() error { | ||
fmt.Println("Check dell 7590") | ||
return nil | ||
} | ||
|
||
func NewObject() Adapter { | ||
return &Dell7590{} | ||
} | ||
|
||
var Object Adapter | ||
|
||
func init() { | ||
Object = &Dell7590{} | ||
} |