diff --git a/pipeline/compress/compress.go b/pipeline/compress/compress.go index fd0a7efa6..f9dfc9336 100644 --- a/pipeline/compress/compress.go +++ b/pipeline/compress/compress.go @@ -1,13 +1,12 @@ package compress import ( - "archive/tar" - "compress/gzip" - "io" "log" "os" "github.com/goreleaser/releaser/config" + "github.com/goreleaser/releaser/pipeline/compress/tar" + "github.com/goreleaser/releaser/pipeline/compress/zip" "github.com/goreleaser/releaser/uname" "golang.org/x/sync/errgroup" ) @@ -35,51 +34,31 @@ func (Pipe) Run(config config.ProjectConfig) error { return g.Wait() } +type Archive interface { + Close() error + Add(name, path string) error +} + func create(system, arch string, config config.ProjectConfig) error { - file, err := os.Create("dist/" + nameFor(system, arch, config.BinaryName) + ".tar.gz") + file, err := os.Create("dist/" + nameFor(system, arch, config.BinaryName) + "." + config.Archive.Format) log.Println("Creating", file.Name(), "...") if err != nil { return err } - gw := gzip.NewWriter(file) - tw := tar.NewWriter(gw) - defer func() { - _ = tw.Close() - _ = gw.Close() - _ = file.Close() - }() + defer func() { _ = file.Close() }() + var archive Archive + if config.Archive.Format == "zip" { + archive = zip.New(file) + } else { + archive = tar.New(file) + } + defer func() { _ = archive.Close() }() for _, f := range config.Files { - if err := addFile(tw, f, f); err != nil { + if err := archive.Add(f, f); err != nil { return err } } - return addFile(tw, config.BinaryName+ext(system), binaryPath(system, arch, config.BinaryName)) -} - -func addFile(tw *tar.Writer, name, path string) (err error) { - file, err := os.Open(path) - if err != nil { - return - } - defer func() { - _ = file.Close() - }() - stat, err := file.Stat() - if err != nil { - return - } - header := new(tar.Header) - header.Name = name - header.Size = stat.Size() - header.Mode = int64(stat.Mode()) - header.ModTime = stat.ModTime() - if err := tw.WriteHeader(header); err != nil { - return err - } - if _, err := io.Copy(tw, file); err != nil { - return err - } - return + return archive.Add(config.BinaryName+ext(system), binaryPath(system, arch, config.BinaryName)) } func nameFor(system, arch, binary string) string { diff --git a/pipeline/compress/tar/tar.go b/pipeline/compress/tar/tar.go new file mode 100644 index 000000000..9d985a274 --- /dev/null +++ b/pipeline/compress/tar/tar.go @@ -0,0 +1,58 @@ +package tar + +import ( + "archive/tar" + "compress/gzip" + "io" + "os" +) + +type Archive struct { + gw *gzip.Writer + tw *tar.Writer +} + +func (a Archive) Close() error { + if err := a.gw.Close(); err != nil { + return err + } + if err := a.tw.Close(); err != nil { + return err + } + return nil +} + +func New(target *os.File) Archive { + gw := gzip.NewWriter(target) + tw := tar.NewWriter(gw) + return Archive{ + gw: gw, + tw: tw, + } +} + +func (a Archive) Add(name, path string) (err error) { + file, err := os.Open(path) + if err != nil { + return + } + defer func() { + _ = file.Close() + }() + stat, err := file.Stat() + if err != nil { + return + } + header := new(tar.Header) + header.Name = name + header.Size = stat.Size() + header.Mode = int64(stat.Mode()) + header.ModTime = stat.ModTime() + if err := a.tw.WriteHeader(header); err != nil { + return err + } + if _, err := io.Copy(a.tw, file); err != nil { + return err + } + return +} diff --git a/pipeline/compress/zip/zip.go b/pipeline/compress/zip/zip.go new file mode 100644 index 000000000..b79bfd95d --- /dev/null +++ b/pipeline/compress/zip/zip.go @@ -0,0 +1,35 @@ +package zip + +import ( + "archive/zip" + "io" + "os" +) + +type Archive struct { + z *zip.Writer +} + +func (a Archive) Close() error { + return a.z.Close() +} + +func New(target *os.File) Archive { + return Archive{ + z: zip.NewWriter(target), + } +} + +func (a Archive) Add(name, path string) (err error) { + file, err := os.Open(path) + if err != nil { + return + } + defer func() { _ = file.Close() }() + f, err := a.z.Create(name) + if err != nil { + return err + } + _, err = io.Copy(f, file) + return err +}