diff --git a/examples/deliver/main.go b/examples/deliver/main.go index 8fd5b67..1485f78 100644 --- a/examples/deliver/main.go +++ b/examples/deliver/main.go @@ -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: "", -// 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) +} diff --git a/examples/deliver/main.wasm b/examples/deliver/main.wasm new file mode 100644 index 0000000..615a841 Binary files /dev/null and b/examples/deliver/main.wasm differ diff --git a/examples/echo/main.wasm b/examples/echo/main.wasm index 566c932..a23a86f 100644 Binary files a/examples/echo/main.wasm and b/examples/echo/main.wasm differ