73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func addToArchive(tw *tar.Writer, filename string) error {
|
|
file, err := os.Open(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close() //nolint: errcheck // defered
|
|
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
header, err := tar.FileInfoHeader(info, info.Name())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
header.Name = filepath.Base(file.Name())
|
|
|
|
if err := tw.WriteHeader(header); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = io.Copy(tw, file)
|
|
return err
|
|
}
|
|
|
|
// CreateArchive creates a gzip tar archive in the `destDir` path including `files`.
|
|
func CreateArchive(destDir, name string, files ...string) (string, error) {
|
|
now := time.Now().UTC()
|
|
archivePath := filepath.Join(
|
|
destDir,
|
|
fmt.Sprintf("%s-%s.tar.gz", name, strings.ReplaceAll(now.Format(time.RFC3339), ":", "-")),
|
|
)
|
|
|
|
file, err := os.Create(archivePath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("unable to create archive=%s, err=%v", archivePath, err)
|
|
}
|
|
defer file.Close() //nolint: errcheck // defered
|
|
|
|
gw := gzip.NewWriter(file)
|
|
defer gw.Close() //nolint: errcheck // defered
|
|
|
|
tw := tar.NewWriter(gw)
|
|
defer tw.Close() //nolint: errcheck // defered
|
|
|
|
for _, f := range files {
|
|
if err := addToArchive(tw, f); err != nil {
|
|
return "", fmt.Errorf(
|
|
"unable to add file=%s to archive=%s, err=%v",
|
|
f,
|
|
archivePath,
|
|
err,
|
|
)
|
|
}
|
|
}
|
|
|
|
return archivePath, nil
|
|
}
|