Tinkerbell.Sandbox/cmd/getbinariesfromquay/tar/untar.go
Gianluca Arbezzano 45f593449a CLI to get binaries from a docker image with multi arch support
have a look at the README.md to see how it works.

Bootstrap a CLI program to copy binaries from multi arch images

Signed-off-by: Gianluca Arbezzano <gianarb92@gmail.com>
2020-11-16 17:50:40 +01:00

49 lines
838 B
Go

package tar
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
)
func Untar(dst string, r io.Reader) error {
gzr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
switch {
case err == io.EOF:
return nil
case err != nil:
return err
case header == nil:
continue
}
target := filepath.Join(dst, header.Name)
switch header.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
}
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
if _, err := io.Copy(f, tr); err != nil {
return err
}
f.Close()
}
}
}