Skip to content
Permalink
Browse files

feat: support custom wrap directory

closes #728
  • Loading branch information...
caarlos0 committed Nov 13, 2018
1 parent ce267ac commit 18b8be68185507aa715e1ed48ea3b23b68e0e46c
Showing with 47 additions and 7 deletions.
  1. +21 −4 internal/pipe/archive/archive.go
  2. +24 −2 internal/pipe/archive/archive_test.go
  3. +1 −1 pkg/config/config.go
  4. +1 −0 www/content/archive.md
@@ -18,6 +18,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/archive"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)

@@ -90,7 +91,7 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
}
archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format)
lock.Lock()
if _, err := os.Stat(archivePath); !os.IsNotExist(err) {
if _, err = os.Stat(archivePath); !os.IsNotExist(err) {
lock.Unlock()
return fmt.Errorf("archive named %s already exists. Check your archive name template", archivePath)
}
@@ -101,12 +102,17 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
}
lock.Unlock()
defer archiveFile.Close() // nolint: errcheck

var log = log.WithField("archive", archivePath)
log.Info("creating")
var wrap string
if ctx.Config.Archive.WrapInDirectory {
wrap = folder

wrap, err := tmpl.New(ctx).
WithArtifact(binaries[0], ctx.Config.Archive.Replacements).
Apply(wrapFolder(ctx.Config.Archive))
if err != nil {
return err
}

var a = NewEnhancedArchive(archive.New(archiveFile), wrap)
defer a.Close() // nolint: errcheck

@@ -135,6 +141,17 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
return nil
}

func wrapFolder(a config.Archive) string {
switch a.WrapInDirectory {
case "true":
return a.NameTemplate
case "false":
return ""
default:
return a.WrapInDirectory
}
}

func skip(ctx *context.Context, binaries []artifact.Artifact) error {
for _, binary := range binaries {
log.WithField("binary", binary.Name).Info("skip archiving")
@@ -282,8 +282,11 @@ func TestRunPipeWrap(t *testing.T) {
Dist: dist,
Archive: config.Archive{
NameTemplate: "foo",
WrapInDirectory: true,
WrapInDirectory: "foo_{{ .Os }}",
Format: "tar.gz",
Replacements: map[string]string{
"darwin": "macOS",
},
Files: []string{
"README.*",
},
@@ -317,7 +320,7 @@ func TestRunPipeWrap(t *testing.T) {
break
}
require.NoError(t, err)
require.Equal(t, filepath.Join("foo", n), h.Name)
require.Equal(t, filepath.Join("foo_macOS", n), h.Name)
}
}

@@ -520,3 +523,22 @@ func TestDuplicateFilesInsideArchive(t *testing.T) {
require.NoError(t, a.Add("foo", ff.Name()))
require.EqualError(t, a.Add("foo", ff.Name()), "file foo already exists in the archive")
}

func TestWrapInDirectory(t *testing.T) {
t.Run("false", func(t *testing.T) {
require.Equal(t, "", wrapFolder(config.Archive{
WrapInDirectory: "false",
}))
})
t.Run("true", func(t *testing.T) {
require.Equal(t, "foo", wrapFolder(config.Archive{
WrapInDirectory: "true",
NameTemplate: "foo",
}))
})
t.Run("custom", func(t *testing.T) {
require.Equal(t, "foobar", wrapFolder(config.Archive{
WrapInDirectory: "foobar",
}))
})
}
@@ -149,7 +149,7 @@ type Archive struct {

Format string `yaml:",omitempty"`
FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty"`
WrapInDirectory bool `yaml:"wrap_in_directory,omitempty"`
WrapInDirectory string `yaml:"wrap_in_directory,omitempty"`
Files []string `yaml:",omitempty"`
}

@@ -36,6 +36,7 @@ archive:
# If set to true and you extract the archive 'goreleaser_Linux_arm64.tar.gz',
# you get a folder 'goreleaser_Linux_arm64'.
# If set to false, all files are extracted separately.
# You can also set it to a custom folder name.
# Default is false.
wrap_in_directory: true

0 comments on commit 18b8be6

Please sign in to comment.
You can’t perform that action at this time.