diff options
Diffstat (limited to 'cmds/gocli-as-plugin')
| -rw-r--r-- | cmds/gocli-as-plugin/Makefile | 14 | ||||
| -rw-r--r-- | cmds/gocli-as-plugin/log.go | 35 | ||||
| -rw-r--r-- | cmds/gocli-as-plugin/main.go | 59 |
3 files changed, 108 insertions, 0 deletions
diff --git a/cmds/gocli-as-plugin/Makefile b/cmds/gocli-as-plugin/Makefile new file mode 100644 index 0000000..277d737 --- /dev/null +++ b/cmds/gocli-as-plugin/Makefile @@ -0,0 +1,14 @@ +run: build + ./gocli-as-plugin + # ldd ./gocli-as-plugin + +build-release: + go get -v -u -x . + go build + +build: + GO111MODULE="off" go get -v -x . + GO111MODULE="off" go build + +update: + GO111MODULE="off" go get -v -u -x . diff --git a/cmds/gocli-as-plugin/log.go b/cmds/gocli-as-plugin/log.go new file mode 100644 index 0000000..b05beaf --- /dev/null +++ b/cmds/gocli-as-plugin/log.go @@ -0,0 +1,35 @@ +// This creates a simple hello world window +package main + +import ( + "log" + "fmt" + "os" + arg "github.com/alexflint/go-arg" +) + + +var args struct { + Foo string + Bar bool + User string `arg:"env:USER"` + Demo bool `help:"run a demo"` +} + +var f *os.File +var err error + +func init() { + arg.MustParse(&args) + fmt.Println(args.Foo, args.Bar, args.User) + + f, err = os.OpenFile("/tmp/guilogfile", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) + if err != nil { + log.Fatalf("error opening file: %v", err) + } + // hmm. is there a trick here or must this be in main() + // defer f.Close() + + log.SetOutput(f) + log.Println("This is a test log entry") +} diff --git a/cmds/gocli-as-plugin/main.go b/cmds/gocli-as-plugin/main.go new file mode 100644 index 0000000..a9bd632 --- /dev/null +++ b/cmds/gocli-as-plugin/main.go @@ -0,0 +1,59 @@ +// Copyright 2014 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "log" + "os" + + "plugin" +// "github.com/awesome-gocui/gocui" +) + +type Greeter interface { + Greet() +} + +var plugGocli *plugin.Plugin +var plugHello *plugin.Plugin + +func main() { + log.Println("attempt plugin") + + go loadPlugin(plugHello, "../../toolkit/hello.so") + loadPlugin(plugGocli, "../../toolkit/gocli.so") +} + +func loadPlugin(plug *plugin.Plugin, name string) { + // load module + // 1. open the so file to load the symbols + plug, err = plugin.Open(name) + if err != nil { + log.Println(err) + os.Exit(1) + } + + // 2. look up a symbol (an exported function or variable) + // in this case, variable Greeter + symGreeter, err := plug.Lookup("Greeter") + if err != nil { + log.Println(err) + os.Exit(1) + } + + log.Println("symGreater", symGreeter) + + // 3. Assert that loaded symbol is of a desired type + // in this case interface type Greeter (defined above) + // var greeter Greeter + greeter, ok := symGreeter.(Greeter) + if !ok { + log.Println("unexpected type from module symbol") + os.Exit(1) + } + + // 4. use the module + greeter.Greet() +} |
