1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-21 21:07:19 +02:00
Carlos Alexandro Becker 2e5a8e5a54
feat: allow to template archives.files.info ()
this allows to template the owner, group and mtime in file infos inside
archives.

should help towards reproducible builds!

goes well with 

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2022-12-14 12:16:43 -03:00

59 lines
1.2 KiB
Go

// Package gzip implements the Archive interface providing gz archiving
// and compression.
package gzip
import (
"fmt"
"io"
"os"
"github.com/goreleaser/goreleaser/pkg/config"
gzip "github.com/klauspost/pgzip"
)
// Archive as gz.
type Archive struct {
gw *gzip.Writer
}
// New gz archive.
func New(target io.Writer) Archive {
// the error will be nil since the compression level is valid
gw, _ := gzip.NewWriterLevel(target, gzip.BestCompression)
return Archive{
gw: gw,
}
}
// Close all closeables.
func (a Archive) Close() error {
return a.gw.Close()
}
// Add file to the archive.
func (a Archive) Add(f config.File) error {
if a.gw.Header.Name != "" {
return fmt.Errorf("gzip: failed to add %s, only one file can be archived in gz format", f.Destination)
}
file, err := os.Open(f.Source) // #nosec
if err != nil {
return err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return err
}
if info.IsDir() {
return nil
}
a.gw.Header.Name = f.Destination
if f.Info.ParsedMTime.IsZero() {
a.gw.Header.ModTime = info.ModTime()
} else {
a.gw.Header.ModTime = f.Info.ParsedMTime
}
_, err = io.Copy(a.gw, file)
return err
}