Skip to content

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:

1
2
3
4
Module parameter(s): Jane Doe
MESSAGE: πŸ‘‹ Hello Capsule
πŸ“: πŸ‘‹ Hello World! 🌍
πŸ‘‹ Hello Jane Doe

You can find more samples on /capsule/capsule-cli