package pods import ( "bytes" "io" "log" buildahCopyah "github.com/containers/buildah/copier" "github.com/containers/podman/v4/pkg/bindings/containers" "github.com/containers/podman/v4/pkg/copy" "github.com/spf13/viper" "golang.org/x/sync/errgroup" ) // CopySkelToContainer copies an Archive into the containers home directory // (Assumes the use of a Webtop container) // This Function is heavily inspired by https://github.com/containers/podman/blob/main/cmd/podman/containers/cp.go#L337 func CopySkelToContainer(id string) error { log.Println("Copying skel directory to container") var errorGroup errgroup.Group var buf bytes.Buffer var err error _, err = copy.ResolveHostPath(viper.GetString("skel")) if err != nil { log.Println("Could not find skel Directory", err) return err } reader, writer := io.Pipe() // Create Function for copying from host to pipe hostCopy := func() error { getOptions := buildahCopyah.GetOptions{ KeepDirectoryNames: true, } err := buildahCopyah.Get("/", "", getOptions, []string{viper.GetString("skel")}, &buf) if err != nil { log.Print("Error copying from Host: ", err) } //defer closeAndLogWriter(writer) return nil } // create function to copy from pipe to container containerCopy := func() error { _, err := containers.CopyFromArchive(Socket, id, "/config/", &buf) if err != nil { log.Print("Error copying to Container: ", err) } //defer closeAndLogReader(reader) return nil } errorGroup.Go(hostCopy) errorGroup.Go(containerCopy) if err := errorGroup.Wait(); err != nil { log.Println("Could not Copy Skel", err) } closeAndLogReader(reader) closeAndLogWriter(writer) return err } // closeAndLogReader takes a reader and closes it, logging any error func closeAndLogReader(reader *io.PipeReader) { err := reader.Close() if err != nil { log.Println("Could not close reader: ", err) } } // closeAndLogWriter takes a writer and closes it, logging any error func closeAndLogWriter(writer *io.PipeWriter) { err := writer.Close() if err != nil { log.Println("Could not close writer: ", err) } }