package sdk import ( "context" "log/slog" _ "github.com/tetratelabs/tinymem" "code.icb4dc0.de/buildr/wasi-module-sdk-go/mem" "code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol" rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol/generated/rpc/v1" ) var defaultRegistry = NewTypeRegistry() func Register(cat Category, moduleName string, factory Factory) { defaultRegistry.Add(cat, moduleName, factory) } //export inventory func Inventory() uint64 { var inventory rpcv1.PluginInventory for _, t := range defaultRegistry.List() { inventory.Modules = append(inventory.Modules, &rpcv1.ModuleReference{ ModuleCategory: t.Category, ModuleType: t.Type, }) } data, err := inventory.MarshalVT() if err != nil { panic(err) } return mem.UnifyPtrSize(mem.DataToUnmanagedPtr(data)) } //export run func Run(specPtr, specSize uint32) { var startTask rpcv1.StartTaskRequest 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()) if err := protocol.Unmarshal(startTask.GetSpec(), module); err != nil { executor.logger.Error("Failed to unmarshal spec", slog.String("error", err.Error())) panic(err) } executor.Run(context.Background(), module) } //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() var helpResponse = &rpcv1.HelpResponse{ 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)) }