Skip to content

Commit

Permalink
Add example/deliver.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Nov 16, 2024
1 parent 8b3c483 commit 3db3915
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 100 deletions.
144 changes: 44 additions & 100 deletions examples/deliver/main.go
Original file line number Diff line number Diff line change
@@ -1,103 +1,47 @@
// //go:generate tinygo build -o main.wasm -target=wasi -scheduler=none main.go
//go:generate tinygo build -o main.wasm -target=wasi -scheduler=none main.go

package main

// import (
// "bytes"
// "context"
// "io"
// "log/slog"
// "os"

// "capnproto.org/go/capnp/v3"
// "github.com/urfave/cli/v2"
// "github.com/wetware/go/proc"
// )

// func main() {
// ctx := context.TODO()

// app := &cli.App{
// Name: "deliver",
// Usage: "read message from stdn and send to `PID`",
// ArgsUsage: "<PID>",
// Flags: []cli.Flag{
// &cli.StringFlag{
// Name: "method",
// Aliases: []string{"m"},
// Usage: "method name",
// },
// &cli.Uint64SliceFlag{
// Name: "push",
// Aliases: []string{"p"},
// Usage: "push u64 onto stack",
// },
// },
// Action: deliver,
// }

// if err := app.RunContext(ctx, os.Args); err != nil {
// slog.ErrorContext(ctx, "application failed",
// "reason", err)
// os.Exit(1)
// }
// }

// func deliver(c *cli.Context) error {
// name := c.Args().First()
// f, err := os.Open(name)
// if err != nil {
// return err
// }
// defer f.Close()

// m, seg := capnp.NewSingleSegmentMessage(nil)
// defer m.Release()

// call, err := proc.NewRootMethodCall(seg)
// if err != nil {
// return err
// }

// err = call.SetName(c.String("method"))
// if err != nil {
// return err
// }

// stack := c.Uint64Slice("stack")
// size := int32(len(stack))

// callStack, err := call.NewStack(size)
// if err != nil {
// return err
// }

// for i, word := range stack {
// callStack.Set(i, word)
// }

// r := io.LimitReader(c.App.Reader, 1<<32-1) // max u32
// data, err := io.ReadAll(r)
// if err != nil {
// return err
// }

// if err = call.SetCallData(data); err != nil {
// return err
// }

// b, err := m.Marshal()
// if err != nil {
// return err
// }

// n, err := io.Copy(f, bytes.NewReader(b))
// if err != nil {
// return err
// }

// slog.DebugContext(c.Context, "delivered message",
// "size", n,
// "dest", name)
// return nil
// }
import (
"fmt"
"io"
"log/slog"
"os"
)

func main() {
if len(os.Args) < 1 {
slog.Error("expected 1 argument, got 0",
"status", 1)
os.Exit(1)
}

f, err := os.Open(os.Args[0])
if err != nil {
slog.Error("failed to open file",
"reason", err,
"status", 2,
"name", os.Args[0])
os.Exit(2)
}
defer f.Close()

var n int64
var status int
if n, err = io.Copy(f, os.Stdin); err != nil {
status = 3
err = fmt.Errorf("request: %w", err)
} else if n, err = io.Copy(os.Stdout, f); err != nil {
status = 4
err = fmt.Errorf("response: %w", err)
}

if err != nil {
slog.Error("failed to read message from stdin",
"reason", err,
"status", status,
"read", n)
}

os.Exit(status)
}
Binary file added examples/deliver/main.wasm
Binary file not shown.
Binary file modified examples/echo/main.wasm
Binary file not shown.

0 comments on commit 3db3915

Please sign in to comment.