podterminal/pods/skel.go

55 lines
1.3 KiB
Go

package pods
import (
"bytes"
"log"
"os"
"github.com/containers/podman/v4/pkg/bindings/containers"
"github.com/spf13/viper"
)
// CopySkelToContainer copies an Archive into the containers home directory
// (Assumes the use of a Webtop container)
func CopySkelToContainer(id string) error {
trueVar := true
log.Println("Copying skel Archive to container")
var buf bytes.Buffer
var err error
file, err := os.Open(viper.GetString("skel"))
if err != nil {
log.Println("Could not open skel archive", err)
return err
}
defer file.Close()
// Use 'FileInfo' to get the size of the file
fileInfo, err := file.Stat()
if err != nil {
log.Println("Could not get size of skel archive", err)
return err
}
// Create a buffer with the size of the file
buffer := bytes.NewBuffer(make([]byte, 0, fileInfo.Size()))
// Copy the file contents to the buffer
_, err = buffer.ReadFrom(file)
if err != nil {
log.Println("Could not get read in skel archive", err)
return err
}
log.Println("Writing file from buffer to container")
copyOptions := &containers.CopyOptions{
Chown: &trueVar,
}
_, err = containers.CopyFromArchiveWithOptions(Socket, id, "/config/", &buf, copyOptions)
if err != nil {
log.Print("Error copying to Container: ", err)
}
return err
}