wasi-module-sdk-go/entrypoint.go

108 lines
2.6 KiB
Go
Raw Normal View History

2023-05-08 13:21:31 +00:00
package sdk
import (
"context"
2023-08-15 19:46:02 +00:00
"log/slog"
2023-05-08 13:21:31 +00:00
_ "github.com/tetratelabs/tinymem"
2023-05-18 16:21:44 +00:00
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
2023-08-15 19:46:02 +00:00
"code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol"
2023-05-18 16:21:44 +00:00
rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol/generated/rpc/v1"
2023-05-08 13:21:31 +00:00
)
var defaultRegistry = NewTypeRegistry()
func Register(cat Category, moduleName string, factory Factory) {
defaultRegistry.Add(cat, moduleName, factory)
}
2023-06-29 19:55:40 +00:00
//export inventory
2023-05-13 15:46:09 +00:00
func Inventory() uint64 {
var inventory rpcv1.PluginInventory
for _, t := range defaultRegistry.List() {
inventory.Modules = append(inventory.Modules, &rpcv1.ModuleReference{
2023-08-15 19:46:02 +00:00
ModuleCategory: t.Category,
2023-05-13 15:46:09 +00:00
ModuleType: t.Type,
})
}
data, err := inventory.MarshalVT()
if err != nil {
panic(err)
}
return mem.UnifyPtrSize(mem.DataToUnmanagedPtr(data))
}
2023-06-29 19:55:40 +00:00
//export run
2023-05-08 13:21:31 +00:00
func Run(specPtr, specSize uint32) {
var startTask rpcv1.StartTaskRequest
if err := startTask.UnmarshalVT(mem.DataFromPtr(specPtr, specSize)); err != nil {
panic(err)
}
2023-05-24 20:11:05 +00:00
executor := NewExecutor(startTask.Buildr.Repo.Root, startTask.Buildr.OutDir, startTask.Buildr.BinDir)
2023-05-13 15:46:09 +00:00
reference := startTask.GetReference().GetModule()
2023-08-15 19:46:02 +00:00
module := defaultRegistry.Get(reference.GetModuleCategory(), reference.GetModuleType())
2023-05-08 13:21:31 +00:00
2023-08-15 19:46:02 +00:00
if err := protocol.Unmarshal(startTask.GetSpec(), module); err != nil {
executor.logger.Error("Failed to unmarshal spec", slog.String("error", err.Error()))
2023-05-08 13:21:31 +00:00
panic(err)
}
2023-05-24 20:11:05 +00:00
executor.Run(context.Background(), module)
2023-05-08 13:21:31 +00:00
}
2023-08-15 19:46:02 +00:00
//export help
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)
if module == nil {
return 0
}
helper, ok := module.(Helper)
if !ok {
return 0
}
modHelp := helper.Help()
2023-08-15 20:10:30 +00:00
helpResponse := &rpcv1.HelpResponse{
2023-08-15 19:46:02 +00:00
Name: modHelp.Name,
Description: modHelp.Description,
Examples: make([]*rpcv1.TaskExample, 0, len(modHelp.Examples)),
}
for _, e := range modHelp.Examples {
modSpec, err := protocol.Marshal(e.Spec.Module)
if err != nil {
panic(err)
}
helpResponse.Examples = append(helpResponse.Examples, &rpcv1.TaskExample{
Name: e.Name,
Description: e.Description,
TaskSpec: &rpcv1.TaskSpec{
ModuleName: e.Spec.ModuleName,
Container: e.Spec.Container,
OutputDir: e.Spec.OutputDir,
ModuleSpec: modSpec,
},
})
}
data, err := helpResponse.MarshalVT()
if err != nil {
panic(err)
}
return mem.UnifyPtrSize(mem.DataToUnmanagedPtr(data))
}