Add a raw action

This commit is contained in:
Sjoerd Simons
2017-07-11 16:35:39 +02:00
parent 2ed0acfd45
commit b792b4722a
2 changed files with 47 additions and 0 deletions

45
raw_action.go Normal file
View File

@@ -0,0 +1,45 @@
package main
import (
"os"
"log"
"strconv"
"path"
"io/ioutil"
)
type RawAction struct {
*BaseAction
Offset string
Source string
Path string
}
func (raw *RawAction) Verify(context YaibContext) {
if raw.Source != "rootdir" {
log.Fatal("Only suppport sourcing from filesystem")
}
}
func (raw *RawAction) Run(context *YaibContext) {
s := path.Join(context.rootdir, raw.Path)
content, err := ioutil.ReadFile(s)
if err != nil {
log.Fatalf("Failed to read %s\n", s)
}
target, err := os.OpenFile(context.image, os.O_WRONLY, 0)
if err != nil {
log.Fatalf("Failed to open image file %v\n", err)
}
offset, err := strconv.ParseInt(raw.Offset, 0, 64)
if err != nil {
log.Fatalf("Couldn't parse offset %v\n", err)
}
bytes, err := target.WriteAt(content, offset)
if bytes != len(content) {
log.Fatal("Couldn't write complte data")
}
}

View File

@@ -233,6 +233,8 @@ func (y *YamlAction) UnmarshalYAML(unmarshal func(interface{}) error) error {
y.Action = &OverlayAction{}
case "setup-image":
y.Action = &SetupImage{}
case "raw":
y.Action = &RawAction{}
default:
log.Fatalf("Unknown action: %v", aux.Action)
}