From 3491cf83aa381e54612796afd2f911ba615bcdf8 Mon Sep 17 00:00:00 2001 From: axtloss Date: Thu, 22 Aug 2024 23:26:02 +0200 Subject: [PATCH] feat: add shim plugin --- plugins/shim.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 plugins/shim.go diff --git a/plugins/shim.go b/plugins/shim.go new file mode 100644 index 0000000..f5da39d --- /dev/null +++ b/plugins/shim.go @@ -0,0 +1,76 @@ +package main + +import ( + "encoding/json" + "fmt" + + "C" + + "github.com/vanilla-os/vib/api" +) +import ( + "os" + "os/exec" + "path/filepath" +) + +type ShimModule struct { + Name string `json:"name"` + Type string `json:"type"` + ShimType string `json:"shimtype"` +} + +//export PlugInfo +func PlugInfo() *C.char { + plugininfo := &api.PluginInfo{Name: "shim", Type: api.BuildPlugin} + pluginjson, err := json.Marshal(plugininfo) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + return C.CString(string(pluginjson)) +} + +//export BuildModule +func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char { + var module *ShimModule + var recipe *api.Recipe + + err := json.Unmarshal([]byte(C.GoString(moduleInterface)), &module) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + + err = json.Unmarshal([]byte(C.GoString(recipeInterface)), &recipe) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + + fmt.Printf("[SHIM] Starting plugin: %s\n", module.ShimType) + + dataDir, err := os.MkdirTemp("", fmt.Sprintf("*-vibshim-%s", module.ShimType)) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + defer os.RemoveAll(dataDir) + + pluginCommand := fmt.Sprintf("%s/%s", recipe.PluginPath, module.ShimType) + modulePath := filepath.Join(dataDir, "moduleInterface") + recipePath := filepath.Join(dataDir, "recipeInterface") + + err = os.WriteFile(modulePath, []byte(C.GoString(moduleInterface)), 0o777) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + err = os.WriteFile(recipePath, []byte(C.GoString(recipeInterface)), 0o777) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + + out, err := exec.Command(pluginCommand, modulePath, recipePath).Output() + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + return C.CString(string(out)) +} + +func main() {}