github.com/marianogappa/goreleaser@v0.26.2-0.20170715090149-96acd0a9fc46/pipeline/archive/archive.go (about) 1 // Package archive implements the pipe interface with the intent of 2 // archiving and compressing the binaries, readme, and other artifacts. It 3 // also provides an Archive interface which represents an archiving format. 4 package archive 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 11 "github.com/apex/log" 12 "github.com/goreleaser/archive" 13 "github.com/goreleaser/goreleaser/context" 14 "github.com/goreleaser/goreleaser/internal/archiveformat" 15 "github.com/mattn/go-zglob" 16 "golang.org/x/sync/errgroup" 17 ) 18 19 // Pipe for archive 20 type Pipe struct{} 21 22 // Description of the pipe 23 func (Pipe) Description() string { 24 return "Creating archives" 25 } 26 27 // Run the pipe 28 func (Pipe) Run(ctx *context.Context) error { 29 var g errgroup.Group 30 for platform, folder := range ctx.Folders { 31 folder := folder 32 platform := platform 33 g.Go(func() error { 34 if ctx.Config.Archive.Format == "binary" { 35 return skip(ctx, platform, folder) 36 } 37 return create(ctx, platform, folder) 38 }) 39 } 40 return g.Wait() 41 } 42 43 func create(ctx *context.Context, platform, name string) error { 44 var folder = filepath.Join(ctx.Config.Dist, name) 45 var format = archiveformat.For(ctx, platform) 46 file, err := os.Create(folder + "." + format) 47 if err != nil { 48 return err 49 } 50 log.WithField("archive", file.Name()).Info("creating") 51 defer func() { _ = file.Close() }() 52 var archive = archive.New(file) 53 defer func() { _ = archive.Close() }() 54 55 files, err := findFiles(ctx) 56 if err != nil { 57 return err 58 } 59 for _, f := range files { 60 if err = archive.Add(f, f); err != nil { 61 return err 62 } 63 } 64 var basepath = filepath.Join(ctx.Config.Dist, name) 65 binaries, err := ioutil.ReadDir(basepath) 66 if err != nil { 67 return err 68 } 69 for _, binary := range binaries { 70 var path = filepath.Join(basepath, binary.Name()) 71 if err := archive.Add(binary.Name(), path); err != nil { 72 return err 73 } 74 } 75 ctx.AddArtifact(file.Name()) 76 return nil 77 } 78 79 func skip(ctx *context.Context, platform, name string) error { 80 var path = filepath.Join(ctx.Config.Dist, name) 81 binaries, err := ioutil.ReadDir(path) 82 if err != nil { 83 return err 84 } 85 log.WithField("platform", platform).Debugf("found %v binaries", len(binaries)) 86 for _, binary := range binaries { 87 log.WithField("binary", binary.Name()).Info("skip archiving") 88 ctx.AddArtifact(filepath.Join(path+"/", binary.Name())) 89 } 90 return nil 91 } 92 93 func findFiles(ctx *context.Context) (result []string, err error) { 94 for _, glob := range ctx.Config.Archive.Files { 95 files, err := zglob.Glob(glob) 96 if err != nil { 97 return result, err 98 } 99 result = append(result, files...) 100 } 101 return 102 }