A virtual jailed shell environment for Go apps backed by an io/fs#FS.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(command): add jq and rg as wasm programs

Embed jq.wasm and rg.wasm and register both with the wasmprog
registry, mirroring the existing qjs implementation.

Signed-off-by: Xe Iaso <me@xeiaso.net>

Xe Iaso 779fbf82 1c3c1471

+134
+65
command/internal/jq/jq.go
··· 1 + package jq 2 + 3 + import ( 4 + "context" 5 + _ "embed" 6 + "errors" 7 + 8 + "github.com/tetratelabs/wazero" 9 + "github.com/tetratelabs/wazero/experimental/sysfs" 10 + "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" 11 + wsys "github.com/tetratelabs/wazero/sys" 12 + "mvdan.cc/sh/v3/interp" 13 + "tangled.org/xeiaso.net/kefka/command" 14 + "tangled.org/xeiaso.net/kefka/wasm/billyfs" 15 + ) 16 + 17 + //go:embed jq.wasm 18 + var jqWASM []byte 19 + 20 + var ( 21 + runtime wazero.Runtime 22 + compiled wazero.CompiledModule 23 + ) 24 + 25 + func init() { 26 + ctx := context.Background() 27 + runtime = wazero.NewRuntime(ctx) 28 + wasi_snapshot_preview1.MustInstantiate(ctx, runtime) 29 + 30 + var err error 31 + compiled, err = runtime.CompileModule(ctx, jqWASM) 32 + if err != nil { 33 + panic(err) 34 + } 35 + } 36 + 37 + type Impl struct{} 38 + 39 + func (Impl) Exec(ctx context.Context, ec *command.ExecContext, args []string) error { 40 + fsConfig := wazero.NewFSConfig().(sysfs.FSConfig). 41 + WithSysFSMount(billyfs.New(ec.FS), "/") 42 + 43 + config := wazero.NewModuleConfig(). 44 + WithStdin(ec.Stdin). 45 + WithStdout(ec.Stdout). 46 + WithStderr(ec.Stderr). 47 + WithArgs(append([]string{"jq"}, args...)...). 48 + WithName("jq"). 49 + WithFSConfig(fsConfig). 50 + WithSysNanosleep(). 51 + WithSysNanotime(). 52 + WithSysWalltime() 53 + 54 + mod, err := runtime.InstantiateModule(ctx, compiled, config) 55 + if err != nil { 56 + if exitErr, ok := errors.AsType[*wsys.ExitError](err); ok { 57 + if code := exitErr.ExitCode(); code != 0 { 58 + return interp.ExitStatus(uint8(code)) 59 + } 60 + return nil 61 + } 62 + return err 63 + } 64 + return mod.Close(ctx) 65 + }
command/internal/jq/jq.wasm

This is a binary file and will not be displayed.

+65
command/internal/rg/rg.go
··· 1 + package rg 2 + 3 + import ( 4 + "context" 5 + _ "embed" 6 + "errors" 7 + 8 + "github.com/tetratelabs/wazero" 9 + "github.com/tetratelabs/wazero/experimental/sysfs" 10 + "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" 11 + wsys "github.com/tetratelabs/wazero/sys" 12 + "mvdan.cc/sh/v3/interp" 13 + "tangled.org/xeiaso.net/kefka/command" 14 + "tangled.org/xeiaso.net/kefka/wasm/billyfs" 15 + ) 16 + 17 + //go:embed rg.wasm 18 + var rgWASM []byte 19 + 20 + var ( 21 + runtime wazero.Runtime 22 + compiled wazero.CompiledModule 23 + ) 24 + 25 + func init() { 26 + ctx := context.Background() 27 + runtime = wazero.NewRuntime(ctx) 28 + wasi_snapshot_preview1.MustInstantiate(ctx, runtime) 29 + 30 + var err error 31 + compiled, err = runtime.CompileModule(ctx, rgWASM) 32 + if err != nil { 33 + panic(err) 34 + } 35 + } 36 + 37 + type Impl struct{} 38 + 39 + func (Impl) Exec(ctx context.Context, ec *command.ExecContext, args []string) error { 40 + fsConfig := wazero.NewFSConfig().(sysfs.FSConfig). 41 + WithSysFSMount(billyfs.New(ec.FS), "/") 42 + 43 + config := wazero.NewModuleConfig(). 44 + WithStdin(ec.Stdin). 45 + WithStdout(ec.Stdout). 46 + WithStderr(ec.Stderr). 47 + WithArgs(append([]string{"rg"}, args...)...). 48 + WithName("rg"). 49 + WithFSConfig(fsConfig). 50 + WithSysNanosleep(). 51 + WithSysNanotime(). 52 + WithSysWalltime() 53 + 54 + mod, err := runtime.InstantiateModule(ctx, compiled, config) 55 + if err != nil { 56 + if exitErr, ok := errors.AsType[*wsys.ExitError](err); ok { 57 + if code := exitErr.ExitCode(); code != 0 { 58 + return interp.ExitStatus(uint8(code)) 59 + } 60 + return nil 61 + } 62 + return err 63 + } 64 + return mod.Close(ctx) 65 + }
command/internal/rg/rg.wasm

This is a binary file and will not be displayed.

+4
command/registry/wasmprog/wasmprog.go
··· 1 1 package wasmprog 2 2 3 3 import ( 4 + "tangled.org/xeiaso.net/kefka/command/internal/jq" 4 5 "tangled.org/xeiaso.net/kefka/command/internal/python3" 5 6 "tangled.org/xeiaso.net/kefka/command/internal/qjs" 7 + "tangled.org/xeiaso.net/kefka/command/internal/rg" 6 8 "tangled.org/xeiaso.net/kefka/command/registry" 7 9 ) 8 10 ··· 12 14 reg.Register("python3", python3.Impl{}) 13 15 14 16 reg.Register("qjs", qjs.Impl{}) 17 + reg.Register("jq", jq.Impl{}) 18 + reg.Register("rg", rg.Impl{}) 15 19 }