mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-04 03:11:55 +02:00
tar and zip
This commit is contained in:
parent
759bb2918e
commit
715b77e221
@ -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 {
|
||||
|
58
pipeline/compress/tar/tar.go
Normal file
58
pipeline/compress/tar/tar.go
Normal file
@ -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
|
||||
}
|
35
pipeline/compress/zip/zip.go
Normal file
35
pipeline/compress/zip/zip.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user