refactor: use new API structure
This commit is contained in:
parent
8ded4bc17b
commit
04644e99d9
13 changed files with 172 additions and 162 deletions
16
api.go
16
api.go
|
@ -6,17 +6,17 @@ import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
|
commonv1 "code.icb4dc0.de/buildr/api/generated/common/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Category = rpcv1.Category
|
type Category = commonv1.Category
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CategoryTool = rpcv1.Category_CategoryTool
|
CategoryTool = commonv1.Category_CategoryTool
|
||||||
CategoryTask = rpcv1.Category_CategoryTask
|
CategoryTask = commonv1.Category_CategoryTask
|
||||||
CategoryBuild = rpcv1.Category_CategoryBuild
|
CategoryBuild = commonv1.Category_CategoryBuild
|
||||||
CategoryPackage = rpcv1.Category_CategoryPackage
|
CategoryPackage = commonv1.Category_CategoryPackage
|
||||||
CategoryRelease = rpcv1.Category_CategoryRelease
|
CategoryRelease = commonv1.Category_CategoryRelease
|
||||||
)
|
)
|
||||||
|
|
||||||
type StateMetadata struct {
|
type StateMetadata struct {
|
||||||
|
@ -39,7 +39,7 @@ type ExecutionContext interface {
|
||||||
type TaskSpec[T Module] struct {
|
type TaskSpec[T Module] struct {
|
||||||
Module T
|
Module T
|
||||||
ModuleName string
|
ModuleName string
|
||||||
Container *rpcv1.ContainerSpec
|
Container *commonv1.ContainerSpec
|
||||||
OutputDir string
|
OutputDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,24 +2,28 @@ package sdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
_ "github.com/tetratelabs/tinymem"
|
commonv1 "code.icb4dc0.de/buildr/api/generated/common/v1"
|
||||||
|
remotev1 "code.icb4dc0.de/buildr/api/generated/remote/v1"
|
||||||
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
|
wasiv1 "code.icb4dc0.de/buildr/api/generated/wasi/v1"
|
||||||
"code.icb4dc0.de/buildr/common/protocol"
|
"code.icb4dc0.de/buildr/common/protocol"
|
||||||
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultRegistry = NewTypeRegistry()
|
var (
|
||||||
|
defaultRegistry = NewTypeRegistry()
|
||||||
|
startTaskWrapper = FuncExportWrapper[*remotev1.StartTaskRequest, *wasiv1.StartTaskResponse](StartTask)
|
||||||
|
inventoryWrapper = FuncExportWrapper[*wasiv1.PluginInventoryRequest, *wasiv1.PluginInventoryResponse](GetInventory)
|
||||||
|
helpForWrapper = FuncExportWrapper[*wasiv1.HelpRequest, *wasiv1.HelpResponse](HelpForModule)
|
||||||
|
)
|
||||||
|
|
||||||
func Register(cat Category, moduleName string, factory Factory) {
|
func Register(cat Category, moduleName string, factory Factory) {
|
||||||
defaultRegistry.Add(cat, moduleName, factory)
|
defaultRegistry.Add(cat, moduleName, factory)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export inventory
|
func GetInventory(*wasiv1.PluginInventoryRequest) (*wasiv1.PluginInventoryResponse, error) {
|
||||||
func Inventory() uint64 {
|
var inventory wasiv1.PluginInventoryResponse
|
||||||
var inventory rpcv1.PluginInventory
|
|
||||||
|
|
||||||
for _, t := range defaultRegistry.List() {
|
for _, t := range defaultRegistry.List() {
|
||||||
m := defaultRegistry.Get(t.Category, t.Type)
|
m := defaultRegistry.Get(t.Category, t.Type)
|
||||||
|
@ -33,8 +37,8 @@ func Inventory() uint64 {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
inventory.Specs = append(inventory.Specs, &rpcv1.PluginInventory_InventorySpec{
|
inventory.Specs = append(inventory.Specs, &wasiv1.PluginInventoryResponse_InventorySpec{
|
||||||
ModuleRef: &rpcv1.ModuleReference{
|
ModuleRef: &commonv1.ModuleReference{
|
||||||
ModuleCategory: t.Category,
|
ModuleCategory: t.Category,
|
||||||
ModuleType: t.Type,
|
ModuleType: t.Type,
|
||||||
},
|
},
|
||||||
|
@ -42,57 +46,44 @@ func Inventory() uint64 {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := inventory.MarshalVT()
|
return &inventory, nil
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return mem.UnifyPtrSize(mem.DataToUnmanagedPtr(data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//export run
|
func StartTask(req *remotev1.StartTaskRequest) (*wasiv1.StartTaskResponse, error) {
|
||||||
func Run(specPtr, specSize uint32) {
|
executor := NewExecutor(req.Buildr.Repo.Root, req.Buildr.OutDir, req.Buildr.BinDir)
|
||||||
var startTask rpcv1.StartTaskRequest
|
reference := req.GetReference().GetModule()
|
||||||
|
|
||||||
if err := startTask.UnmarshalVT(mem.DataFromPtr(specPtr, specSize)); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
executor := NewExecutor(startTask.Buildr.Repo.Root, startTask.Buildr.OutDir, startTask.Buildr.BinDir)
|
|
||||||
reference := startTask.GetReference().GetModule()
|
|
||||||
module := defaultRegistry.Get(reference.GetModuleCategory(), reference.GetModuleType())
|
module := defaultRegistry.Get(reference.GetModuleCategory(), reference.GetModuleType())
|
||||||
|
|
||||||
if err := protocol.Unmarshal(startTask.GetSpec(), module); err != nil {
|
if err := protocol.Unmarshal(req.GetSpec(), module); err != nil {
|
||||||
executor.logger.Error("Failed to unmarshal spec", slog.String("error", err.Error()))
|
executor.logger.Error("Failed to unmarshal spec", slog.String("error", err.Error()))
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
executor.Run(context.Background(), module)
|
var startTaskResponse wasiv1.StartTaskResponse
|
||||||
|
|
||||||
|
if err := executor.Run(context.Background(), module); err != nil {
|
||||||
|
startTaskResponse.Error = err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return &startTaskResponse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//export help
|
func HelpForModule(helpRequest *wasiv1.HelpRequest) (*wasiv1.HelpResponse, error) {
|
||||||
func HelpFor(pecPtr, specSize uint32) uint64 {
|
|
||||||
var helpRequest rpcv1.HelpRequest
|
|
||||||
|
|
||||||
if err := helpRequest.UnmarshalVT(mem.DataFromPtr(pecPtr, specSize)); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
module := defaultRegistry.Get(helpRequest.ModuleReference.ModuleCategory, helpRequest.ModuleReference.ModuleType)
|
module := defaultRegistry.Get(helpRequest.ModuleReference.ModuleCategory, helpRequest.ModuleReference.ModuleType)
|
||||||
if module == nil {
|
if module == nil {
|
||||||
return 0
|
return nil, errors.New("unknown module")
|
||||||
}
|
}
|
||||||
|
|
||||||
helper, ok := module.(Helper)
|
helper, ok := module.(Helper)
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return new(wasiv1.HelpResponse), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
modHelp := helper.Help()
|
modHelp := helper.Help()
|
||||||
helpResponse := &rpcv1.HelpResponse{
|
helpResponse := &wasiv1.HelpResponse{
|
||||||
Name: modHelp.Name,
|
Name: modHelp.Name,
|
||||||
Description: modHelp.Description,
|
Description: modHelp.Description,
|
||||||
Examples: make([]*rpcv1.TaskExample, 0, len(modHelp.Examples)),
|
Examples: make([]*wasiv1.TaskExample, 0, len(modHelp.Examples)),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, e := range modHelp.Examples {
|
for _, e := range modHelp.Examples {
|
||||||
|
@ -100,10 +91,10 @@ func HelpFor(pecPtr, specSize uint32) uint64 {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
helpResponse.Examples = append(helpResponse.Examples, &rpcv1.TaskExample{
|
helpResponse.Examples = append(helpResponse.Examples, &wasiv1.TaskExample{
|
||||||
Name: e.Name,
|
Name: e.Name,
|
||||||
Description: e.Description,
|
Description: e.Description,
|
||||||
TaskSpec: &rpcv1.TaskSpec{
|
TaskSpec: &commonv1.TaskSpec{
|
||||||
ModuleName: e.Spec.ModuleName,
|
ModuleName: e.Spec.ModuleName,
|
||||||
Container: e.Spec.Container,
|
Container: e.Spec.Container,
|
||||||
OutputDir: e.Spec.OutputDir,
|
OutputDir: e.Spec.OutputDir,
|
||||||
|
@ -112,10 +103,20 @@ func HelpFor(pecPtr, specSize uint32) uint64 {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := helpResponse.MarshalVT()
|
return helpResponse, nil
|
||||||
if err != nil {
|
}
|
||||||
panic(err)
|
|
||||||
}
|
//export /buildr.rpc.v1.WasiExecutorService/PluginInventory
|
||||||
|
func Inventory(ptr, size uint32) uint64 {
|
||||||
return mem.UnifyPtrSize(mem.DataToUnmanagedPtr(data))
|
return inventoryWrapper.Call(ptr, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
//export /buildr.rpc.v1.WasiExecutorService/StartTask
|
||||||
|
func Run(ptr, size uint32) uint64 {
|
||||||
|
return startTaskWrapper.Call(ptr, size)
|
||||||
|
}
|
||||||
|
|
||||||
|
//export /buildr.rpc.v1.WasiExecutorService/Help
|
||||||
|
func HelpFor(ptr, size uint32) uint64 {
|
||||||
|
return helpForWrapper.Call(ptr, size)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
|
wasiv1 "code.icb4dc0.de/buildr/api/generated/wasi/v1"
|
||||||
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ func (c *Command) AddEnv(envToAdd map[string]string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) Run() error {
|
func (c *Command) Run() error {
|
||||||
execReq := &rpcv1.ProcessStartRequest{
|
execReq := &wasiv1.ProcessStartRequest{
|
||||||
Command: c.Name,
|
Command: c.Name,
|
||||||
Args: c.Args,
|
Args: c.Args,
|
||||||
WorkingDirectory: c.WorkingDir,
|
WorkingDirectory: c.WorkingDir,
|
||||||
|
@ -70,7 +70,7 @@ func (c *Command) Run() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
execResp := new(rpcv1.ProcessStartResponse)
|
execResp := new(wasiv1.ProcessStartResponse)
|
||||||
if err := execResp.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
|
if err := execResp.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,12 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
|
wasiv1 "code.icb4dc0.de/buildr/api/generated/wasi/v1"
|
||||||
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LookPath(file string) (string, error) {
|
func LookPath(file string) (string, error) {
|
||||||
lookupPathReq := &rpcv1.LookupPathRequest{
|
lookupPathReq := &wasiv1.LookupPathRequest{
|
||||||
Command: file,
|
Command: file,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ func LookPath(file string) (string, error) {
|
||||||
return "", fmt.Errorf("failed to lookup in path: %s", file)
|
return "", fmt.Errorf("failed to lookup in path: %s", file)
|
||||||
}
|
}
|
||||||
|
|
||||||
lookupPathResp := new(rpcv1.LookupPathResponse)
|
lookupPathResp := new(wasiv1.LookupPathResponse)
|
||||||
if err := lookupPathResp.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
|
if err := lookupPathResp.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,9 +21,7 @@ type Executor struct {
|
||||||
binDir string
|
binDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Executor) Run(ctx context.Context, m Module) {
|
func (e Executor) Run(ctx context.Context, m Module) error {
|
||||||
execCtx := newWasiExecutionContext(ctx, e.logger, m, e.repoRoot, e.binDir, e.outDir)
|
execCtx := newWasiExecutionContext(ctx, e.logger, m, e.repoRoot, e.binDir, e.outDir)
|
||||||
if err := m.Execute(execCtx); err != nil {
|
return m.Execute(execCtx)
|
||||||
e.logger.Error("Failed to execute module", slog.String("err", err.Error()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
31
export.go
Normal file
31
export.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package sdk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
bapi "code.icb4dc0.de/buildr/api"
|
||||||
|
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FuncExportWrapper[TRequest bapi.ProtoMessage, TResponse bapi.ProtoMessage] func(req TRequest) (TResponse, error)
|
||||||
|
|
||||||
|
func (w FuncExportWrapper[TRequest, TResponse]) Call(reqPtr, reqSize uint32) uint64 {
|
||||||
|
var req TRequest
|
||||||
|
if reflect.TypeOf(req).Kind() == reflect.Ptr {
|
||||||
|
req = reflect.New(reflect.TypeOf(req).Elem()).Interface().(TRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := req.UnmarshalVT(mem.DataFromPtr(reqPtr, reqSize)); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp, err := w(req); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else if data, err := resp.MarshalVT(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else if len(data) == 0 {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
|
return mem.UnifyPtrSize(mem.DataToUnmanagedPtr(data))
|
||||||
|
}
|
||||||
|
}
|
12
go.mod
12
go.mod
|
@ -3,17 +3,11 @@ module code.icb4dc0.de/buildr/wasi-module-sdk-go
|
||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
require (
|
require (
|
||||||
code.icb4dc0.de/buildr/api v0.0.0-20230824061008-9729e3ce7ec3
|
code.icb4dc0.de/buildr/api v0.0.0-20230905195458-e4d230e5c7fd
|
||||||
code.icb4dc0.de/buildr/common v0.0.0-20230823164419-5a757074eaa3
|
code.icb4dc0.de/buildr/common v0.0.0-20230905195627-a3d5bb4f1ee7
|
||||||
github.com/tetratelabs/tinymem v0.1.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang/protobuf v1.5.3 // indirect
|
github.com/google/go-cmp v0.5.9 // indirect
|
||||||
golang.org/x/net v0.14.0 // indirect
|
|
||||||
golang.org/x/sys v0.11.0 // indirect
|
|
||||||
golang.org/x/text v0.12.0 // indirect
|
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
|
|
||||||
google.golang.org/grpc v1.57.0 // indirect
|
|
||||||
google.golang.org/protobuf v1.31.0 // indirect
|
google.golang.org/protobuf v1.31.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
23
go.sum
23
go.sum
|
@ -1,27 +1,12 @@
|
||||||
code.icb4dc0.de/buildr/api v0.0.0-20230824061008-9729e3ce7ec3 h1:CXt7gnJOMzk3lxXdYokRzwTeX6w7ZeceSBayvz8AfAw=
|
code.icb4dc0.de/buildr/api v0.0.0-20230905195458-e4d230e5c7fd h1:N4WkVoqphjFXfFwfKpLcEtS5lBCfd9NVytUutiszQ90=
|
||||||
code.icb4dc0.de/buildr/api v0.0.0-20230824061008-9729e3ce7ec3/go.mod h1:eCeIvmMgYp/04nar7GnBDORMdO4jHYQl/RMiNJhyGf8=
|
code.icb4dc0.de/buildr/api v0.0.0-20230905195458-e4d230e5c7fd/go.mod h1:eWSjeX25XbbNGKlxVoOf2a8V6xOCIaQh5W65T7nNcL8=
|
||||||
code.icb4dc0.de/buildr/common v0.0.0-20230823164419-5a757074eaa3 h1:jWQCCE56vhfNoOuJj4JlCEcfXUB/40iR8L59j5xeh+s=
|
code.icb4dc0.de/buildr/common v0.0.0-20230905195627-a3d5bb4f1ee7 h1:sqBO2IFI0Gs+FiM4kkQjj7QkC0p+AhJKqhCQqCEwlM4=
|
||||||
code.icb4dc0.de/buildr/common v0.0.0-20230823164419-5a757074eaa3/go.mod h1:u0PEZjq0l3ICCUgARZObM6Hc0ZlqkcjK0PBEozhAdNQ=
|
code.icb4dc0.de/buildr/common v0.0.0-20230905195627-a3d5bb4f1ee7/go.mod h1:biIRy/mBiVPAR/okWN5DFmB3Hnnxt2FP1Qswa4GBMeE=
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
|
||||||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/tetratelabs/tinymem v0.1.0 h1:Qza1JAg9lquPPJ/CIei5qQYx7t18KLie83O2WR6CM58=
|
|
||||||
github.com/tetratelabs/tinymem v0.1.0/go.mod h1:WFFTZFhLod6lTL+UetFAopVbGaB+KFsVcIY+RUv7NeY=
|
|
||||||
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
|
|
||||||
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
|
|
||||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
|
||||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
|
||||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
|
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
|
|
||||||
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
|
|
||||||
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
|
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
|
||||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
package sdk
|
package sdk
|
||||||
|
|
||||||
func _log_msg(ptr, size uint32) {}
|
func _log_msg(ptr, size uint32) (ptrSize uint64) { return 0 }
|
||||||
|
|
||||||
func _get_state(ptr, size uint32) (ptrSize uint64) { return 0 }
|
func _get_state(ptr, size uint32) (ptrSize uint64) { return 0 }
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ package sdk
|
||||||
|
|
||||||
//go:wasm-module buildr
|
//go:wasm-module buildr
|
||||||
//go:wasmimport buildr log_msg
|
//go:wasmimport buildr log_msg
|
||||||
func _log_msg(ptr, size uint32)
|
func _log_msg(ptr, size uint32) (ptrSize uint64)
|
||||||
|
|
||||||
//go:wasm-module buildr
|
//go:wasm-module buildr
|
||||||
//go:wasmimport buildr get_state
|
//go:wasmimport buildr get_state
|
||||||
|
|
30
logger.go
30
logger.go
|
@ -7,7 +7,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
|
remotev1 "code.icb4dc0.de/buildr/api/generated/remote/v1"
|
||||||
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,15 +28,15 @@ func (h WASIHandler) Enabled(_ context.Context, level slog.Level) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h WASIHandler) Handle(_ context.Context, record slog.Record) error {
|
func (h WASIHandler) Handle(_ context.Context, record slog.Record) error {
|
||||||
taskLog := rpcv1.TaskLog{
|
taskLog := remotev1.TaskLog{
|
||||||
Time: record.Time.UnixMicro(),
|
Time: record.Time.UnixMicro(),
|
||||||
Message: record.Message,
|
Message: record.Message,
|
||||||
Level: int32(record.Level),
|
Level: int32(record.Level),
|
||||||
Attributes: make([]*rpcv1.TaskLog_LogAttribute, 0, record.NumAttrs()),
|
Attributes: make([]*remotev1.TaskLog_LogAttribute, 0, record.NumAttrs()),
|
||||||
}
|
}
|
||||||
|
|
||||||
record.Attrs(func(attr slog.Attr) bool {
|
record.Attrs(func(attr slog.Attr) bool {
|
||||||
taskLog.Attributes = append(taskLog.Attributes, &rpcv1.TaskLog_LogAttribute{
|
taskLog.Attributes = append(taskLog.Attributes, &remotev1.TaskLog_LogAttribute{
|
||||||
Key: attr.Key,
|
Key: attr.Key,
|
||||||
Value: attr.Value.String(),
|
Value: attr.Value.String(),
|
||||||
})
|
})
|
||||||
|
@ -44,13 +44,10 @@ func (h WASIHandler) Handle(_ context.Context, record slog.Record) error {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
data, err := taskLog.MarshalVT()
|
if _, err := LogMsg(&taskLog); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_log_msg(mem.DataToManagedPtr(data))
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,3 +74,20 @@ func (h WASIHandler) WithGroup(name string) slog.Handler {
|
||||||
|
|
||||||
return newHandler
|
return newHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LogMsg(taskLog *remotev1.TaskLog) (*remotev1.Result, error) {
|
||||||
|
data, err := taskLog.MarshalVT()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resultPtr := _log_msg(mem.DataToManagedPtr(data))
|
||||||
|
|
||||||
|
data = mem.PtrToData(mem.UnifiedPtrToSizePtr(resultPtr))
|
||||||
|
resp := new(remotev1.Result)
|
||||||
|
|
||||||
|
if err := resp.UnmarshalVT(data); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
|
@ -56,10 +56,3 @@ func Allocate(size uint32) uintptr {
|
||||||
alivePointers[unsafePtr] = buf
|
alivePointers[unsafePtr] = buf
|
||||||
return unsafePtr
|
return unsafePtr
|
||||||
}
|
}
|
||||||
|
|
||||||
//export deallocate
|
|
||||||
func Deallocate(ptr uintptr) {
|
|
||||||
pointersLock.Lock()
|
|
||||||
defer pointersLock.Unlock()
|
|
||||||
delete(alivePointers, ptr)
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,74 +3,68 @@ package sdk
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
|
remotev1 "code.icb4dc0.de/buildr/api/generated/remote/v1"
|
||||||
|
|
||||||
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StateProxy struct{}
|
type StateProxy struct{}
|
||||||
|
|
||||||
func (s StateProxy) Set(key, state []byte) error {
|
func (s StateProxy) Set(key, state []byte) error {
|
||||||
setCmd := &rpcv1.SetState{
|
setCmd := &remotev1.SetState{
|
||||||
Key: key,
|
Key: key,
|
||||||
Data: state,
|
Data: state,
|
||||||
}
|
}
|
||||||
data, err := setCmd.MarshalVT()
|
|
||||||
if err != nil {
|
if resp, err := SetState(setCmd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
} else if resp.Error != "" {
|
||||||
|
return errors.New(resp.Error)
|
||||||
result := _set_state(mem.DataToManagedPtr(data))
|
} else {
|
||||||
if result == 0 {
|
|
||||||
return errors.New("unknown error occurred")
|
|
||||||
}
|
|
||||||
resultPtr := uint32(result >> 32)
|
|
||||||
resultSize := uint32(result)
|
|
||||||
|
|
||||||
if resultSize == 0 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
resultMsg := new(rpcv1.Result)
|
|
||||||
if err := resultMsg.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !resultMsg.Success {
|
|
||||||
if resultMsg.Error != "" {
|
|
||||||
return errors.New(resultMsg.Error)
|
|
||||||
}
|
|
||||||
return errors.New("unknown error occurred")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StateProxy) Get(key []byte) (state []byte, meta StateMetadata, err error) {
|
func (s StateProxy) Get(key []byte) (state []byte, meta StateMetadata, err error) {
|
||||||
getCmd := &rpcv1.GetStateRequest{
|
getCmd := &remotev1.GetStateRequest{
|
||||||
Key: key,
|
Key: key,
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := getCmd.MarshalVT()
|
if resp, err := GetState(getCmd); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, StateMetadata{}, err
|
return nil, StateMetadata{}, err
|
||||||
|
} else {
|
||||||
|
return resp.Data, StateMetadata{}, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
result := _get_state(mem.DataToManagedPtr(data))
|
|
||||||
if result == 0 {
|
func GetState(req *remotev1.GetStateRequest) (*remotev1.GetStateResponse, error) {
|
||||||
return nil, StateMetadata{}, errors.New("error occurred while processing request")
|
data, err := req.MarshalVT()
|
||||||
}
|
if err != nil {
|
||||||
resultPtr := uint32(result >> 32)
|
return nil, err
|
||||||
resultSize := uint32(result)
|
}
|
||||||
|
|
||||||
if resultSize == 0 {
|
resultPtr := _get_state(mem.DataToManagedPtr(data))
|
||||||
return nil, StateMetadata{}, nil
|
data = mem.PtrToData(mem.UnifiedPtrToSizePtr(resultPtr))
|
||||||
}
|
resp := new(remotev1.GetStateResponse)
|
||||||
|
|
||||||
getStateResult := new(rpcv1.GetStateResponse)
|
if err := resp.UnmarshalVT(data); err != nil {
|
||||||
if err := getStateResult.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
|
return nil, err
|
||||||
return nil, StateMetadata{}, err
|
}
|
||||||
}
|
return resp, nil
|
||||||
|
}
|
||||||
return getStateResult.Data, StateMetadata{}, nil
|
|
||||||
|
func SetState(req *remotev1.SetState) (*remotev1.Result, error) {
|
||||||
|
data, err := req.MarshalVT()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resultPtr := _set_state(mem.DataToManagedPtr(data))
|
||||||
|
data = mem.PtrToData(mem.UnifiedPtrToSizePtr(resultPtr))
|
||||||
|
|
||||||
|
resp := new(remotev1.Result)
|
||||||
|
|
||||||
|
if err := resp.UnmarshalVT(data); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue