-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
52 lines (42 loc) · 1014 Bytes
/
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
package main
import (
"context"
"embed"
_ "embed"
"fmt"
"log"
"os"
"github.com/tetratelabs/wazero"
"github.com/knqyf263/go-plugin/examples/wasi/cat"
)
//go:embed testdata/hello.txt
var f embed.FS
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx := context.Background()
mc := wazero.NewModuleConfig().
WithStdout(os.Stdout). // Attach stdout so that the plugin can write outputs to stdout
WithStderr(os.Stderr). // Attach stderr so that the plugin can write errors to stderr
WithFS(f) // Loaded plugins can access only files that the host allows.
p, err := cat.NewFileCatPlugin(ctx, cat.WazeroModuleConfig(mc))
if err != nil {
return err
}
wasiPlugin, err := p.Load(ctx, "plugin/plugin.wasm")
if err != nil {
return err
}
defer wasiPlugin.Close(ctx)
reply, err := wasiPlugin.Cat(ctx, &cat.FileCatRequest{
FilePath: "testdata/hello.txt",
})
if err != nil {
return err
}
fmt.Println(reply.GetContent())
return nil
}