github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/pkg/archive/targz/targz.go (about)

     1  // Package targz implements the Archive interface providing tar.gz archiving
     2  // and compression.
     3  package targz
     4  
     5  import (
     6  	"compress/gzip"
     7  	"io"
     8  
     9  	"github.com/goreleaser/goreleaser/pkg/archive/tar"
    10  	"github.com/goreleaser/goreleaser/pkg/config"
    11  )
    12  
    13  // Archive as tar.gz.
    14  type Archive struct {
    15  	gw *gzip.Writer
    16  	tw *tar.Archive
    17  }
    18  
    19  // New tar.gz archive.
    20  func New(target io.Writer) Archive {
    21  	// the error will be nil since the compression level is valid
    22  	gw, _ := gzip.NewWriterLevel(target, gzip.BestCompression)
    23  	tw := tar.New(gw)
    24  	return Archive{
    25  		gw: gw,
    26  		tw: &tw,
    27  	}
    28  }
    29  
    30  // Close all closeables.
    31  func (a Archive) Close() error {
    32  	if err := a.tw.Close(); err != nil {
    33  		return err
    34  	}
    35  	return a.gw.Close()
    36  }
    37  
    38  // Add file to the archive.
    39  func (a Archive) Add(f config.File) error {
    40  	return a.tw.Add(f)
    41  }