Refactor Package structure
This commit cleans up the package structure, to make two new top-level packages `internal` (for internal-use packages) and `pkg` (for general shared utility code).
This commit is contained in:
parent
487ce09979
commit
a360324f62
124 changed files with 97 additions and 101 deletions
80
pkg/targz/targz.go
Normal file
80
pkg/targz/targz.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
// Package targz provides facilities for packaging tar.gz files
|
||||
package targz
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Package packages the source directory into a 'tar.gz' file into destination.
|
||||
// If the destination already exists, it is truncated.
|
||||
//
|
||||
// onCopy, when not nil, is called for each file being copied into the archive.
|
||||
func Package(dst, src string, onCopy func(rel string, src string)) (count int64, err error) {
|
||||
// create the target archive
|
||||
archive, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer archive.Close()
|
||||
|
||||
// create a gzip writer
|
||||
zipHandle := gzip.NewWriter(archive)
|
||||
defer zipHandle.Close()
|
||||
|
||||
// create a tar writer
|
||||
tarHandle := tar.NewWriter(zipHandle)
|
||||
defer tarHandle.Close()
|
||||
|
||||
// and walk through it!
|
||||
err = filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// determine the relative path
|
||||
var relpath string
|
||||
relpath, err = filepath.Rel(src, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if onCopy != nil {
|
||||
onCopy(relpath, path)
|
||||
}
|
||||
|
||||
// create a file info header!
|
||||
tInfo, err := tar.FileInfoHeader(info, relpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tInfo.Name = filepath.ToSlash(relpath)
|
||||
|
||||
// write it!
|
||||
if err := tarHandle.WriteHeader(tInfo); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// a directory => no more writing required
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// open the file
|
||||
handle, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer handle.Close()
|
||||
|
||||
// and copy it into the archive
|
||||
ccount, err := io.Copy(tarHandle, handle)
|
||||
count += ccount
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue