Skip to content

Commit

Permalink
feat: add genimage finalize plugin
Browse files Browse the repository at this point in the history
this allows projects to build more advanced filesystem and flash images
out of the oci images

see https://github.com/pengutronix/genimage
  • Loading branch information
axtloss committed Aug 13, 2024
1 parent 04b7a91 commit 9720e4e
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions finalize-plugins/genimage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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()))
}
}

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() {}

0 comments on commit 9720e4e

Please sign in to comment.