mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-08 03:31:59 +02:00
refactor: improved artifact filtering
This commit is contained in:
parent
e9b276923a
commit
1982259c29
@ -2,8 +2,9 @@
|
||||
package artifact
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/apex/log"
|
||||
)
|
||||
|
||||
// Type defines the type of an artifact
|
||||
@ -104,25 +105,41 @@ func ByType(t Type) Filter {
|
||||
}
|
||||
}
|
||||
|
||||
// Or performs an OR between all given filters
|
||||
func Or(filters ...Filter) Filter {
|
||||
return func(a Artifact) bool {
|
||||
for _, f := range filters {
|
||||
if f(a) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// And performs an AND between all given filters
|
||||
func And(filters ...Filter) Filter {
|
||||
return func(a Artifact) bool {
|
||||
for _, f := range filters {
|
||||
if !f(a) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Filter filters the artifact list, returning a new instance.
|
||||
// There are some pre-defined filters but anything of the Type Filter
|
||||
// is accepted.
|
||||
func (artifacts *Artifacts) Filter(filters ...Filter) Artifacts {
|
||||
// You can compose filters by using the And and Or filters.
|
||||
func (artifacts *Artifacts) Filter(filter Filter) Artifacts {
|
||||
var result = New()
|
||||
for _, a := range artifacts.items {
|
||||
if apply(a, filters) {
|
||||
fmt.Println("add", a)
|
||||
if filter(a) {
|
||||
log.Infof("adding %v", a)
|
||||
result.Add(a)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func apply(a Artifact, filters []Filter) bool {
|
||||
for _, filter := range filters {
|
||||
if !filter(a) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -41,8 +41,9 @@ func TestAdd(t *testing.T) {
|
||||
func TestFilter(t *testing.T) {
|
||||
var data = []Artifact{
|
||||
{
|
||||
Name: "foo",
|
||||
Goos: "linux",
|
||||
Name: "foo",
|
||||
Goos: "linux",
|
||||
Goarch: "arm",
|
||||
},
|
||||
{
|
||||
Name: "bar",
|
||||
@ -78,9 +79,24 @@ func TestFilter(t *testing.T) {
|
||||
assert.Len(t, artifacts.Filter(ByType(Checksum)).items, 2)
|
||||
assert.Len(t, artifacts.Filter(ByType(Binary)).items, 0)
|
||||
|
||||
assert.Len(t, artifacts.Filter(ByType(Checksum), func(a Artifact) bool {
|
||||
return a.Name == "checkzumm"
|
||||
}).List(), 1)
|
||||
assert.Len(t, artifacts.Filter(
|
||||
And(
|
||||
ByType(Checksum),
|
||||
func(a Artifact) bool {
|
||||
return a.Name == "checkzumm"
|
||||
},
|
||||
),
|
||||
).List(), 1)
|
||||
|
||||
assert.Len(t, artifacts.Filter(
|
||||
Or(
|
||||
ByType(Checksum),
|
||||
And(
|
||||
ByGoos("linux"),
|
||||
ByGoarm("arm"),
|
||||
),
|
||||
),
|
||||
).List(), 2)
|
||||
}
|
||||
|
||||
func TestGroupByPlatform(t *testing.T) {
|
||||
|
@ -103,10 +103,12 @@ func doRun(ctx *context.Context, client client.Client) error {
|
||||
}
|
||||
|
||||
var archives = ctx.Artifacts.Filter(
|
||||
artifact.ByGoos("darwin"),
|
||||
artifact.ByGoarch("amd64"),
|
||||
artifact.ByGoarch(""),
|
||||
artifact.ByType(artifact.UploadableArchive),
|
||||
artifact.And(
|
||||
artifact.ByGoos("darwin"),
|
||||
artifact.ByGoarch("amd64"),
|
||||
artifact.ByGoarch(""),
|
||||
artifact.ByType(artifact.UploadableArchive),
|
||||
),
|
||||
).List()
|
||||
if len(archives) == 0 {
|
||||
return ErrNoDarwin64Build
|
||||
|
@ -56,14 +56,12 @@ func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
}()
|
||||
// TODO: parallelism should be considered here as well.
|
||||
var g errgroup.Group
|
||||
var artifacts []artifact.Artifact
|
||||
for _, t := range []artifact.Type{
|
||||
artifact.UploadableArchive,
|
||||
artifact.UploadableBinary,
|
||||
} {
|
||||
artifacts = append(artifacts, ctx.Artifacts.Filter(artifact.ByType(t)).List()...)
|
||||
}
|
||||
for _, artifact := range artifacts {
|
||||
for _, artifact := range ctx.Artifacts.Filter(
|
||||
artifact.Or(
|
||||
artifact.ByType(artifact.UploadableArchive),
|
||||
artifact.ByType(artifact.UploadableBinary),
|
||||
),
|
||||
).List() {
|
||||
artifact := artifact
|
||||
g.Go(func() error {
|
||||
return checksums(ctx, file, artifact)
|
||||
|
@ -70,12 +70,14 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
func doRun(ctx *context.Context) error {
|
||||
for _, docker := range ctx.Config.Dockers {
|
||||
var binaries = ctx.Artifacts.Filter(
|
||||
artifact.ByGoos(docker.Goos),
|
||||
artifact.ByGoarch(docker.Goarch),
|
||||
artifact.ByGoarm(docker.Goarm),
|
||||
func(a artifact.Artifact) bool {
|
||||
return a.Name == docker.Binary
|
||||
},
|
||||
artifact.And(
|
||||
artifact.ByGoos(docker.Goos),
|
||||
artifact.ByGoarch(docker.Goarch),
|
||||
artifact.ByGoarm(docker.Goarm),
|
||||
func(a artifact.Artifact) bool {
|
||||
return a.Name == docker.Binary
|
||||
},
|
||||
),
|
||||
).List()
|
||||
for _, binary := range binaries {
|
||||
var err = process(ctx, docker, binary)
|
||||
|
@ -4,7 +4,8 @@ package release
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
@ -20,15 +21,6 @@ func (Pipe) String() string {
|
||||
return "releasing to GitHub"
|
||||
}
|
||||
|
||||
// Run the pipe
|
||||
func (Pipe) Run(ctx *context.Context) error {
|
||||
c, err := client.NewGitHub(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return doRun(ctx, c)
|
||||
}
|
||||
|
||||
// Default sets the pipe defaults
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
if ctx.Config.Release.NameTemplate == "" {
|
||||
@ -45,6 +37,15 @@ func (Pipe) Default(ctx *context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run the pipe
|
||||
func (Pipe) Run(ctx *context.Context) error {
|
||||
c, err := client.NewGitHub(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return doRun(ctx, c)
|
||||
}
|
||||
|
||||
func doRun(ctx *context.Context, c client.Client) error {
|
||||
if !ctx.Publish {
|
||||
return pipeline.Skip("--skip-publish is set")
|
||||
@ -62,7 +63,13 @@ func doRun(ctx *context.Context, c client.Client) error {
|
||||
}
|
||||
var g errgroup.Group
|
||||
sem := make(chan bool, ctx.Parallelism)
|
||||
for _, artifact := range ctx.Artifacts {
|
||||
for _, artifact := range ctx.Artifacts.Filter(
|
||||
artifact.Or(
|
||||
artifact.ByType(artifact.UploadableArchive),
|
||||
artifact.ByType(artifact.UploadableBinary),
|
||||
artifact.ByType(artifact.Checksum),
|
||||
),
|
||||
).List() {
|
||||
sem <- true
|
||||
artifact := artifact
|
||||
g.Go(func() error {
|
||||
@ -75,14 +82,12 @@ func doRun(ctx *context.Context, c client.Client) error {
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
func upload(ctx *context.Context, c client.Client, releaseID int, artifact string) error {
|
||||
var path = filepath.Join(ctx.Config.Dist, artifact)
|
||||
file, err := os.Open(path)
|
||||
func upload(ctx *context.Context, c client.Client, releaseID int, artifact artifact.Artifact) error {
|
||||
file, err := os.Open(artifact.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
_, name := filepath.Split(path)
|
||||
log.WithField("file", file.Name()).WithField("name", name).Info("uploading to release")
|
||||
return c.Upload(ctx, releaseID, name, file)
|
||||
log.WithField("file", file.Name()).WithField("name", artifact.Name).Info("uploading to release")
|
||||
return c.Upload(ctx, releaseID, artifact.Name, file)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user