2017-01-14 19:47:15 -02:00
|
|
|
package archive
|
2016-12-28 22:23:39 -02:00
|
|
|
|
2016-12-28 22:53:56 -02:00
|
|
|
import (
|
2017-01-14 12:51:09 -02:00
|
|
|
"io/ioutil"
|
2016-12-30 09:27:35 -02:00
|
|
|
"log"
|
2016-12-29 18:10:11 -02:00
|
|
|
"os"
|
2017-01-14 14:06:57 -02:00
|
|
|
"path/filepath"
|
2016-12-29 18:10:11 -02:00
|
|
|
|
2017-01-14 20:01:32 -02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2017-01-14 20:09:44 -02:00
|
|
|
"github.com/goreleaser/goreleaser/pipeline/archive/tar"
|
|
|
|
"github.com/goreleaser/goreleaser/pipeline/archive/zip"
|
2016-12-30 09:59:24 -02:00
|
|
|
"golang.org/x/sync/errgroup"
|
2016-12-28 22:53:56 -02:00
|
|
|
)
|
2016-12-28 22:23:39 -02:00
|
|
|
|
2017-01-14 19:47:15 -02:00
|
|
|
// Pipe for archive
|
2016-12-30 09:27:35 -02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-01-14 19:41:32 +01:00
|
|
|
// Description of the pipe
|
2017-01-14 15:14:35 -02:00
|
|
|
func (Pipe) Description() string {
|
2017-01-19 10:04:41 +01:00
|
|
|
return "Creating archives"
|
2016-12-30 09:27:35 -02:00
|
|
|
}
|
|
|
|
|
2016-12-30 12:41:59 -02:00
|
|
|
// Run the pipe
|
2017-01-14 14:06:57 -02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2016-12-30 09:59:24 -02:00
|
|
|
var g errgroup.Group
|
2017-01-14 14:06:57 -02:00
|
|
|
for _, archive := range ctx.Archives {
|
2017-01-14 12:34:22 -02:00
|
|
|
archive := archive
|
|
|
|
g.Go(func() error {
|
2017-01-14 14:06:57 -02:00
|
|
|
return create(archive, ctx)
|
2017-01-14 12:34:22 -02:00
|
|
|
})
|
2016-12-28 22:53:56 -02:00
|
|
|
}
|
2016-12-30 09:59:24 -02:00
|
|
|
return g.Wait()
|
2016-12-28 22:23:39 -02:00
|
|
|
}
|
2016-12-28 22:53:56 -02:00
|
|
|
|
2017-01-14 19:41:32 +01:00
|
|
|
// Archive represents a compression archive files from disk can be written to.
|
2017-01-06 13:23:49 -02:00
|
|
|
type Archive interface {
|
|
|
|
Close() error
|
|
|
|
Add(name, path string) error
|
|
|
|
}
|
|
|
|
|
2017-01-14 14:06:57 -02:00
|
|
|
func create(name string, ctx *context.Context) error {
|
2017-03-25 21:31:16 -03:00
|
|
|
folder := filepath.Join(ctx.Config.Dist, name)
|
2017-01-14 15:30:28 -02:00
|
|
|
file, err := os.Create(folder + "." + ctx.Config.Archive.Format)
|
2017-01-19 10:04:41 +01:00
|
|
|
log.Println("Creating", file.Name())
|
2016-12-28 22:53:56 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-06 13:23:49 -02:00
|
|
|
defer func() { _ = file.Close() }()
|
2017-01-14 14:06:57 -02:00
|
|
|
var archive = archiveFor(file, ctx.Config.Archive.Format)
|
2017-01-06 13:23:49 -02:00
|
|
|
defer func() { _ = archive.Close() }()
|
2017-01-14 19:47:15 -02:00
|
|
|
for _, f := range ctx.Config.Archive.Files {
|
2017-01-14 14:29:01 -02:00
|
|
|
if err = archive.Add(f, f); err != nil {
|
2016-12-28 22:53:56 -02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 14:06:57 -02:00
|
|
|
files, err := ioutil.ReadDir(folder)
|
2017-01-14 12:51:09 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2017-01-14 15:30:28 -02:00
|
|
|
if err := archive.Add(f.Name(), filepath.Join(folder, f.Name())); err != nil {
|
2017-01-14 12:51:09 -02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-12-28 22:53:56 -02:00
|
|
|
}
|
|
|
|
|
2017-01-09 12:46:20 -02:00
|
|
|
func archiveFor(file *os.File, format string) Archive {
|
|
|
|
if format == "zip" {
|
|
|
|
return zip.New(file)
|
2016-12-28 22:53:56 -02:00
|
|
|
}
|
2017-01-09 12:46:20 -02:00
|
|
|
return tar.New(file)
|
2016-12-28 22:53:56 -02:00
|
|
|
}
|