Skip to content

Instantly share code, notes, and snippets.

@kaz

kaz/iso-patching.go

Created Aug 26, 2020
Embed
What would you like to do?
package main
import (
"log"
"net/http"
"os/exec"
"github.com/kaz/patchwork"
"github.com/labstack/echo/v4"
)
func existing1(c echo.Context) error {
exec.Command("sh", "-c", `
mount ./image/centos.iso /mnt
cp -Tr /mnt ./disk
umount /mnt
echo "Hello, World!" > ./disk/hello.txt
mkisofs -R -o ./out.iso ./disk
`).Run()
return c.File("./out.iso")
}
func existing2(c echo.Context) error {
cmd := exec.Command("sh", "-c", `
echo "Hello, World!" > ./disk/hello.txt
mkisofs -R -o - ./disk
`)
out, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
return c.Stream(http.StatusOK, "application/x-iso9660-image", out)
}
func proposed(c echo.Context) error {
img, ol, err := patchwork.NewOverlayedImageFromFile("./image/centos.iso")
if err != nil {
return err
}
if err := img.UpdateFile("/TRANS.TBL", "/HELLO.TXT", "/hello.txt", []byte("Hello, World!")); err != nil {
return err
}
return c.Stream(http.StatusOK, "application/x-iso9660-image", ol)
}
func main() {
e := echo.New()
e.GET("/existing1", existing1)
e.GET("/existing2", existing2)
e.GET("/proposed", proposed)
log.Fatal(e.Start(":8080"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment