From c6d2599e731f8926909325c065e1929447c6f03b Mon Sep 17 00:00:00 2001 From: axtloss Date: Tue, 13 Aug 2024 13:55:28 +0200 Subject: [PATCH] feat: add genimage finalize plugin this allows projects to build more advanced filesystem and flash images out of the oci images see https://github.com/pengutronix/genimage --- finalize-plugins/genimage.go | 91 ++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 finalize-plugins/genimage.go diff --git a/finalize-plugins/genimage.go b/finalize-plugins/genimage.go new file mode 100644 index 0000000..eb7fa1b --- /dev/null +++ b/finalize-plugins/genimage.go @@ -0,0 +1,91 @@ +package main + +import ( + "C" + "encoding/json" + "fmt" + "github.com/vanilla-os/vib/api" + "os" + "os/exec" + "strings" +) + +type Genimage struct { + Name string `json:"name"` + Type string `json:"type"` + GenimagePath string `json:"genimagepath"` + Config string `json:"config"` + Rootpath string `json:"rootpath"` + Inputpath string `json:"inputpath"` + Outputpath string `json:"outputpath"` +} + +//export PlugInfo +func PlugInfo() *C.char { + plugininfo := &api.PluginInfo{Name: "genimage", Type: api.FinalizePlugin} + pluginjson, err := json.Marshal(plugininfo) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + return C.CString(string(pluginjson)) +} + +//export PluginScope +func PluginScope() int32 { // int32 is defined as GoInt32 in cgo which is the same as a C int + return api.IMAGENAME | api.FS | api.RECIPE +} + +func ParsePath(path string, data *api.ScopeData) string { + path = strings.Replace(path, "$PROJROOT", data.Recipe.ParentPath, 1) + path = strings.Replace(path, "$FSROOT", data.FS, 1) + return path +} + +//export FinalizeBuild +func FinalizeBuild(moduleInterface *C.char, extraData *C.char) *C.char { + var module *Genimage + var data *api.ScopeData + + 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(extraData)), &data) + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + + genimage := module.GenimagePath + if genimage == "" { + genimage, err = exec.LookPath("genimage") + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + } + + fmt.Println("running genimg") + cmd := exec.Command( + genimage, + "--config", + ParsePath(module.Config, data), + "--rootpath", + ParsePath(module.Rootpath, data), + "--outputpath", + ParsePath(module.Outputpath, data), + "--inputpath", + ParsePath(module.Inputpath, data), + ) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Dir = data.Recipe.ParentPath + + err = cmd.Run() + if err != nil { + return C.CString(fmt.Sprintf("ERROR: %s", err.Error())) + } + + return C.CString("") +} + +func main() {}