Developer Guide
First CLI Module
Create a directory cli-say-hello
| mkdir cli-say-hello
cd cli-say-hello
|
Initialize a new project in cli-say-hello
:
| go mod init cli-say-hello
|
Install the Capsule MDK dependencies:
| go get github.com/bots-garden/capsule-module-sdk
|
Create a new file main.go
in cli-say-hello
:
| package main
import (
capsule "github.com/bots-garden/capsule-module-sdk"
)
func main() {
capsule.SetHandle(Handle)
}
// Handle function
func Handle(params []byte) ([]byte, error) {
// Display the content of `params`
capsule.Print("Module parameter(s): " + string(params))
// Read an display an environment variable
capsule.Print("MESSAGE: " + capsule.GetEnv("MESSAGE"))
// Write content to a file
err := capsule.WriteFile("./hello.txt", []byte("π Hello World! π"))
if err != nil {
capsule.Print(err.Error())
}
// Read content from a file
data, err := capsule.ReadFile("./hello.txt")
if err != nil {
capsule.Print(err.Error())
}
capsule.Print("π: " + string(data))
return []byte("π Hello " + string(params)), nil
}
|
Build the wasm module:
| tinygo build -o cli-say-hello.wasm -scheduler=none --no-debug -target wasi ./main.go
|
Run the module:
You need to download the last capsule CLI: https://github.com/bots-garden/capsule/releases
| export MESSAGE="π Hello Capsule"
./capsule --wasm=cli-say-hello.wasm --params="Jane Doe"
|
- --wasm
flag: the path to the wasm file
- --mode
flag: the parameter to pass to the wasm module
output:
| Module parameter(s): Jane Doe
MESSAGE: π Hello Capsule
π: π Hello World! π
π Hello Jane Doe
|
You can find more samples on /capsule/capsule-cli