Skip to content

Commit

Permalink
feat: template on extra_files (#2677)
Browse files Browse the repository at this point in the history
* feat: template on extra_files

* Update internal/extrafiles/extra_files_test.go

Co-authored-by: Brian Flad <bflad417@gmail.com>

* Update www/docs/customization/blob.md

Co-authored-by: Brian Flad <bflad417@gmail.com>

Co-authored-by: Brian Flad <bflad417@gmail.com>
  • Loading branch information
caarlos0 and bflad committed Nov 22, 2021
1 parent 72434a0 commit 808b603
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 19 deletions.
11 changes: 9 additions & 2 deletions internal/extrafiles/extra_files.go
Expand Up @@ -7,17 +7,24 @@ import (

"github.com/apex/log"
"github.com/goreleaser/fileglob"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)

// Find resolves extra files globs et al into a map of names/paths or an error.
func Find(files []config.ExtraFile) (map[string]string, error) {
func Find(ctx *context.Context, files []config.ExtraFile) (map[string]string, error) {
t := tmpl.New(ctx)
result := map[string]string{}
for _, extra := range files {
if extra.Glob == "" {
continue
}
files, err := fileglob.Glob(extra.Glob)
glob, err := t.Apply(extra.Glob)
if err != nil {
return result, fmt.Errorf("failed to apply template to glob %q: %w", extra.Glob, err)
}
files, err := fileglob.Glob(glob)
if err != nil {
return result, fmt.Errorf("globbing failed for pattern %s: %w", extra.Glob, err)
}
Expand Down
44 changes: 37 additions & 7 deletions internal/extrafiles/extra_files_test.go
Expand Up @@ -4,15 +4,45 @@ import (
"testing"

"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)

func TestTemplate(t *testing.T) {
globs := []config.ExtraFile{
{Glob: "./testdata/file{{ .Env.ONE }}.golden"},
}

ctx := context.New(config.Project{})
ctx.Env["ONE"] = "1"

files, err := Find(ctx, globs)
require.NoError(t, err)
require.Len(t, files, 1)

require.Equal(t, "testdata/file1.golden", files["file1.golden"])
}

func TestBadTemplate(t *testing.T) {
globs := []config.ExtraFile{
{Glob: "./testdata/file{{ .Env.NOPE }}.golden"},
}

ctx := context.New(config.Project{})

files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to apply template to glob "./testdata/file{{ .Env.NOPE }}.golden": template: tmpl:1:22: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`)
}

func TestShouldGetSpecificFile(t *testing.T) {
globs := []config.ExtraFile{
{}, // empty glob, will be ignored
{Glob: "./testdata/sub3"}, // will get a file1.golden as well, but will be overridden
{Glob: "./testdata/file1.golden"},
}

files, err := Find(globs)
files, err := Find(context.New(config.Project{}), globs)
require.NoError(t, err)
require.Len(t, files, 1)

Expand All @@ -24,7 +54,7 @@ func TestFailToGetSpecificFile(t *testing.T) {
{Glob: "./testdata/file453.golden"},
}

files, err := Find(globs)
files, err := Find(context.New(config.Project{}), globs)
require.EqualError(t, err, "globbing failed for pattern ./testdata/file453.golden: matching \"./testdata/file453.golden\": file does not exist")
require.Empty(t, files)
}
Expand All @@ -34,12 +64,12 @@ func TestShouldGetFilesWithSuperStar(t *testing.T) {
{Glob: "./**/file?.golden"},
}

files, err := Find(globs)
files, err := Find(context.New(config.Project{}), globs)
require.NoError(t, err)
require.Len(t, files, 3)

require.Equal(t, "testdata/file2.golden", files["file2.golden"])
require.Equal(t, "testdata/file1.golden", files["file1.golden"])
require.Equal(t, "testdata/sub3/file1.golden", files["file1.golden"])
require.Equal(t, "testdata/sub/file5.golden", files["file5.golden"])
}

Expand All @@ -48,7 +78,7 @@ func TestShouldGetAllFilesWithGoldenExtension(t *testing.T) {
{Glob: "./testdata/*.golden"},
}

files, err := Find(globs)
files, err := Find(context.New(config.Project{}), globs)
require.NoError(t, err)
require.Len(t, files, 2)

Expand All @@ -61,11 +91,11 @@ func TestShouldGetAllFilesInsideTestdata(t *testing.T) {
{Glob: "./testdata/*"},
}

files, err := Find(globs)
files, err := Find(context.New(config.Project{}), globs)
require.NoError(t, err)
require.Len(t, files, 4)

require.Equal(t, "testdata/file1.golden", files["file1.golden"])
require.Equal(t, "testdata/sub3/file1.golden", files["file1.golden"])
require.Equal(t, "testdata/file2.golden", files["file2.golden"])
require.Equal(t, "testdata/file3.gold", files["file3.gold"])
require.Equal(t, "testdata/sub/file5.golden", files["file5.golden"])
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion internal/pipe/blob/upload.go
Expand Up @@ -108,7 +108,7 @@ func doUpload(ctx *context.Context, conf config.Blob) error {
})
}

files, err := extrafiles.Find(conf.ExtraFiles)
files, err := extrafiles.Find(ctx, conf.ExtraFiles)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/checksums/checksums.go
Expand Up @@ -51,7 +51,7 @@ func (Pipe) Run(ctx *context.Context) (err error) {
return nil
}

extraFiles, err := extrafiles.Find(ctx.Config.Checksum.ExtraFiles)
extraFiles, err := extrafiles.Find(ctx, ctx.Config.Checksum.ExtraFiles)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/release/release.go
Expand Up @@ -131,7 +131,7 @@ func doPublish(ctx *context.Context, client client.Client) error {
return err
}

extraFiles, err := extrafiles.Find(ctx.Config.Release.ExtraFiles)
extraFiles, err := extrafiles.Find(ctx, ctx.Config.Release.ExtraFiles)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions www/docs/customization/blob.md
Expand Up @@ -43,9 +43,11 @@ blobs:
# Default is `{{ .ProjectName }}/{{ .Tag }}`
folder: "foo/bar/{{.Version}}"

# You can add extra pre-existing files to the release.
# The filename on the release will be the last part of the path (base). If
# another file with the same name exists, the last one found will be used.
# You can add extra pre-existing files to the bucket.
# The filename on the release will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
# These globs can also include templates.
#
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down
6 changes: 4 additions & 2 deletions www/docs/customization/checksum.md
Expand Up @@ -30,8 +30,10 @@ checksum:
disable: true

# You can add extra pre-existing files to the checksums file.
# The filename on the checksums file will be the last part of the path (base). If
# another file with the same name exists, the last one found will be used.
# The filename on the checksum will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
# This globs can also include templates.
#
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down
6 changes: 4 additions & 2 deletions www/docs/customization/release.md
Expand Up @@ -64,8 +64,10 @@ release:
disable: true

# You can add extra pre-existing files to the release.
# The filename on the release will be the last part of the path (base). If
# another file with the same name exists, the last one found will be used.
# The filename on the release will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
# This globs can also include templates.
#
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down

0 comments on commit 808b603

Please sign in to comment.