83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
|
|
"github.com/google/subcommands"
|
|
"github.com/google/uuid"
|
|
shell "github.com/ipfs/go-ipfs-api"
|
|
"github.com/rogpeppe/go-internal/txtar"
|
|
"tulpa.dev/within/wasmcloud/cmd/internal"
|
|
)
|
|
|
|
type testWorkCommand struct {
|
|
wasmCID, abi, ipfsURL string
|
|
}
|
|
|
|
func (testWorkCommand) Name() string { return "test-work" }
|
|
func (testWorkCommand) Synopsis() string { return "sends a test work message over IPFS" }
|
|
func (testWorkCommand) Usage() string { return "TODO(within): this" }
|
|
|
|
func (t *testWorkCommand) SetFlags(fs *flag.FlagSet) {
|
|
fs.StringVar(&t.wasmCID, "wasm-cid", "", "CID for the WASM to run")
|
|
fs.StringVar(&t.abi, "abi", "cwa", "which ABI to use")
|
|
fs.StringVar(&t.ipfsURL, "ipfs-host", "127.0.0.1:5001", "IPFS host")
|
|
}
|
|
|
|
func (t testWorkCommand) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
|
sh := shell.NewShell(t.ipfsURL)
|
|
rdr, err := sh.Cat(t.wasmCID)
|
|
if err != nil {
|
|
log.Fatalf("error getting %s: %v", t.wasmCID, err)
|
|
}
|
|
io.Copy(ioutil.Discard, rdr)
|
|
rdr.Close()
|
|
|
|
er := internal.ExecRequest{
|
|
WASMCID: t.wasmCID,
|
|
ABI: internal.ABI(t.abi),
|
|
Data: []byte("hello, world"),
|
|
Env: map[string]string{
|
|
"MAGIC_CONCH": "yes",
|
|
},
|
|
UUID: uuid.New().String(),
|
|
}
|
|
|
|
data, err := json.Marshal(er)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = sh.PubSubPublish(internal.TopicName, string(data))
|
|
if err != nil {
|
|
log.Printf("can't publish data: %v", err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
|
|
subsc, err := sh.PubSubSubscribe(er.UUID)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
msg, err := subsc.Next()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var execResp internal.ExecResponse
|
|
err = json.Unmarshal(msg.Data, &execResp)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(string(txtar.Format(&execResp.Logs)))
|
|
|
|
return subcommands.ExitSuccess
|
|
}
|