1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

fix: do not allow several archives with the same name

This commit is contained in:
Carlos Alexandro Becker 2018-11-11 12:11:03 -02:00 committed by Carlos Alexandro Becker
parent a6bef50b0f
commit 2dd00ae7a6
2 changed files with 67 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"github.com/apex/log"
"github.com/campoy/unique"
@ -26,7 +27,9 @@ const (
)
// Pipe for archive
type Pipe struct{}
type Pipe struct {
lock sync.Mutex
}
func (Pipe) String() string {
return "archives"
@ -60,8 +63,8 @@ func (Pipe) Default(ctx *context.Context) error {
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
var g errgroup.Group
func (p Pipe) Run(ctx *context.Context) error {
var g errgroup.Group // TODO: use semerrgroup here
var filtered = ctx.Artifacts.Filter(artifact.ByType(artifact.Binary))
for group, artifacts := range filtered.GroupByPlatform() {
log.Debugf("group %s has %d binaries", group, len(artifacts))
@ -70,13 +73,13 @@ func (Pipe) Run(ctx *context.Context) error {
if packageFormat(ctx, artifacts[0].Goos) == "binary" {
return skip(ctx, artifacts)
}
return create(ctx, artifacts)
return p.create(ctx, artifacts)
})
}
return g.Wait()
}
func create(ctx *context.Context, binaries []artifact.Artifact) error {
func (p Pipe) create(ctx *context.Context, binaries []artifact.Artifact) error {
var format = packageFormat(ctx, binaries[0].Goos)
folder, err := tmpl.New(ctx).
WithArtifact(binaries[0], ctx.Config.Archive.Replacements).
@ -85,10 +88,16 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
return err
}
archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format)
log.Info(archivePath)
p.lock.Lock()
if _, err := os.Stat(archivePath); !os.IsNotExist(err) {
return fmt.Errorf("archive named %s already exists. Check your archive name template", archivePath)
}
archiveFile, err := os.Create(archivePath)
if err != nil {
return fmt.Errorf("failed to create directory %s: %s", archivePath, err.Error())
}
p.lock.Unlock()
defer archiveFile.Close() // nolint: errcheck
var log = log.WithField("archive", archivePath)
log.Info("creating")

View File

@ -261,7 +261,6 @@ func TestRunPipeInvalidGlob(t *testing.T) {
})
assert.EqualError(t, Pipe{}.Run(ctx), `failed to find files to archive: globbing failed for pattern [x-]: file does not exist`)
}
func TestRunPipeWrap(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
@ -446,3 +445,56 @@ func TestBinaryOverride(t *testing.T) {
})
}
}
func TestRunPipeSameArchiveFilename(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
assert.NoError(t, os.Mkdir(dist, 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
assert.NoError(t, os.Mkdir(filepath.Join(dist, "windowsamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
assert.NoError(t, err)
_, err = os.Create(filepath.Join(dist, "windowsamd64", "mybin.exe"))
assert.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,
ProjectName: "foobar",
Archive: config.Archive{
NameTemplate: "same-filename",
Files: []string{
"README.*",
"./foo/**/*",
},
Format: "tar.gz",
},
},
)
ctx.Artifacts.Add(artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
Name: "mybin",
Path: filepath.Join(dist, "darwinamd64", "mybin"),
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
},
})
ctx.Artifacts.Add(artifact.Artifact{
Goos: "windows",
Goarch: "amd64",
Name: "mybin.exe",
Path: filepath.Join(dist, "windowsamd64", "mybin.exe"),
Type: artifact.Binary,
Extra: map[string]string{
"Binary": "mybin",
"Extension": ".exe",
},
})
ctx.Version = "0.0.1"
ctx.Git.CurrentTag = "v0.0.1"
err = Pipe{}.Run(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), "same-filename.tar.gz already exists. Check your archive name template")
}