Skip to content

Commit

Permalink
feat: templateable archive.files (#1373)
Browse files Browse the repository at this point in the history
* feat: support variable substitution in archive.files

* Update internal/pipe/archive/archive.go

* feat: templateable archive.files

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* docs: templateable archive.files

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

Co-authored-by: Robbie Ostrow <ostrowr@users.noreply.github.com>
  • Loading branch information
caarlos0 and ostrowr committed Mar 4, 2020
1 parent 139d13c commit 969fb4b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
18 changes: 12 additions & 6 deletions internal/pipe/archive/archive.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/apex/log"
"github.com/campoy/unique"
"github.com/mattn/go-zglob"
"github.com/pkg/errors"

"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/ids"
Expand Down Expand Up @@ -131,8 +132,9 @@ func create(ctx *context.Context, archive config.Archive, binaries []*artifact.A
var log = log.WithField("archive", archivePath)
log.Info("creating")

wrap, err := tmpl.New(ctx).
WithArtifact(binaries[0], archive.Replacements).
template := tmpl.New(ctx).
WithArtifact(binaries[0], archive.Replacements)
wrap, err := template.
Apply(wrapFolder(archive))
if err != nil {
return err
Expand All @@ -141,7 +143,7 @@ func create(ctx *context.Context, archive config.Archive, binaries []*artifact.A
var a = NewEnhancedArchive(archivelib.New(archiveFile), wrap)
defer a.Close() // nolint: errcheck

files, err := findFiles(archive)
files, err := findFiles(template, archive)
if err != nil {
return fmt.Errorf("failed to find files to archive: %s", err.Error())
}
Expand Down Expand Up @@ -210,11 +212,15 @@ func skip(ctx *context.Context, archive config.Archive, binaries []*artifact.Art
return nil
}

func findFiles(archive config.Archive) (result []string, err error) {
func findFiles(template *tmpl.Template, archive config.Archive) (result []string, err error) {
for _, glob := range archive.Files {
files, err := zglob.Glob(glob)
replaced, err := template.Apply(glob)
if err != nil {
return result, errors.Wrapf(err, "failed to apply template %s", glob)
}
files, err := zglob.Glob(replaced)
if err != nil {
return result, fmt.Errorf("globbing failed for pattern %s: %s", glob, err.Error())
return result, errors.Wrapf(err, "globbing failed for pattern %s", glob)
}
result = append(result, files...)
}
Expand Down
63 changes: 52 additions & 11 deletions internal/pipe/archive/archive_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"archive/zip"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -39,10 +40,12 @@ func TestRunPipe(t *testing.T) {
createFakeBinary(t, dist, arch, "mybin")
}
createFakeBinary(t, dist, "windowsamd64", "mybin.exe")
_, err := os.Create(filepath.Join(folder, "README.md"))
require.NoError(t, err)
for _, tt := range []string{"darwin", "linux", "windows"} {
_, err := os.Create(filepath.Join(folder, fmt.Sprintf("README.%s.md", tt)))
require.NoError(t, err)
}
require.NoError(t, os.MkdirAll(filepath.Join(folder, "foo", "bar", "foobar"), 0755))
_, err = os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
_, err := os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
require.NoError(t, err)
var ctx = context.New(
config.Project{
Expand All @@ -54,7 +57,7 @@ func TestRunPipe(t *testing.T) {
Builds: []string{"default"},
NameTemplate: defaultNameTemplate,
Files: []string{
"README.*",
"README.{{.Os}}.*",
"./foo/**/*",
},
FormatOverrides: []config.FormatOverride{
Expand Down Expand Up @@ -143,16 +146,16 @@ func TestRunPipe(t *testing.T) {

if format == "tar.gz" {
// Check archive contents
for _, name := range []string{
"foobar_0.0.1_darwin_amd64.tar.gz",
"foobar_0.0.1_linux_386.tar.gz",
"foobar_0.0.1_linux_armv7.tar.gz",
"foobar_0.0.1_linux_mips_softfloat.tar.gz",
for name, os := range map[string]string{
"foobar_0.0.1_darwin_amd64.tar.gz": "darwin",
"foobar_0.0.1_linux_386.tar.gz": "linux",
"foobar_0.0.1_linux_armv7.tar.gz": "linux",
"foobar_0.0.1_linux_mips_softfloat.tar.gz": "linux",
} {
require.Equal(
t,
[]string{
"README.md",
fmt.Sprintf("README.%s.md", os),
"foo/bar",
"foo/bar/foobar",
"foo/bar/foobar/blah.txt",
Expand All @@ -166,7 +169,7 @@ func TestRunPipe(t *testing.T) {
require.Equal(
t,
[]string{
"README.md",
"README.windows.md",
"foo/bar/foobar/blah.txt",
"mybin.exe",
},
Expand Down Expand Up @@ -372,6 +375,44 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
}

func TestRunPipeInvalidFilesNameTemplate(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
require.NoError(t, err)
var ctx = context.New(
config.Project{
Dist: dist,
Archives: []config.Archive{
{
Builds: []string{"default"},
NameTemplate: "foo",
Format: "zip",
Files: []string{
"{{.asdsd}",
},
},
},
},
)
ctx.Git.CurrentTag = "v0.0.1"
ctx.Artifacts.Add(&artifact.Artifact{
Goos: "darwin",
Goarch: "amd64",
Name: "mybin",
Path: filepath.Join("dist", "darwinamd64", "mybin"),
Type: artifact.Binary,
Extra: map[string]interface{}{
"Binary": "mybin",
"ID": "default",
},
})
require.EqualError(t, Pipe{}.Run(ctx), `failed to find files to archive: failed to apply template {{.asdsd}: template: tmpl:1: unexpected "}" in operand`)
}

func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
folder, back := testlib.Mktmp(t)
defer back()
Expand Down
4 changes: 2 additions & 2 deletions www/content/archive.md
Expand Up @@ -62,12 +62,12 @@ archives:
- goos: windows
format: zip

# Additional files/globs you want to add to the archive.
# Additional files/template/globs you want to add to the archive.
# Defaults are any files matching `LICENCE*`, `LICENSE*`,
# `README*` and `CHANGELOG*` (case-insensitive).
files:
- LICENSE.txt
- README.md
- README_{{.Os}}.md
- CHANGELOG.md
- docs/*
- design/*.png
Expand Down

0 comments on commit 969fb4b

Please sign in to comment.