Skip to content

Commit

Permalink
Merge branch 'master' into feat/templates-for-gitlab-urls
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Sep 9, 2021
2 parents 5990348 + 717051d commit 5ce6521
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
7 changes: 6 additions & 1 deletion internal/pipe/nfpm/nfpm.go
Expand Up @@ -130,6 +130,11 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
return err
}

binDir, err := tmpl.Apply(fpm.Bindir)
if err != nil {
return err
}

homepage, err := tmpl.Apply(fpm.Homepage)
if err != nil {
return err
Expand Down Expand Up @@ -179,7 +184,7 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
log := log.WithField("package", name+"."+format).WithField("arch", arch)
for _, binary := range binaries {
src := binary.Path
dst := filepath.Join(fpm.Bindir, filepath.Base(binary.Name))
dst := filepath.Join(binDir, filepath.Base(binary.Name))
log.WithField("src", src).WithField("dst", dst).Debug("adding binary to package")
contents = append(contents, &files.Content{
Source: filepath.ToSlash(src),
Expand Down
71 changes: 71 additions & 0 deletions internal/pipe/nfpm/nfpm_test.go
Expand Up @@ -279,6 +279,12 @@ func TestInvalidTemplate(t *testing.T) {
ctx.Config.NFPMs[0].APK.Signature.KeyFile = "{{ .NOPE_KEY_FILE }}"
require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1:3: executing "tmpl" at <.NOPE_KEY_FILE>: map has no entry for key "NOPE_KEY_FILE"`)
})

t.Run("bindir", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.NFPMs[0].Bindir = "/usr/local/{{ .NOPE }}"
require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1:14: executing "tmpl" at <.NOPE>: map has no entry for key "NOPE"`)
})
}

func TestRunPipeInvalidContentsSourceTemplate(t *testing.T) {
Expand Down Expand Up @@ -1052,6 +1058,71 @@ func TestSkipSign(t *testing.T) {
})
}

func TestBinDirTemplating(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
binPath := filepath.Join(dist, "mybin", "mybin")
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
Env: []string{
"PRO=pro",
"DESC=templates",
},
NFPMs: []config.NFPM{
{
ID: "someid",
// Bindir should pass through the template engine
Bindir: "/usr/lib/{{ .Env.PRO }}/nagios/plugins",
Builds: []string{"default"},
Formats: []string{"rpm"},
Section: "somesection",
Priority: "standard",
Description: "Some description with {{ .Env.DESC }}",
License: "MIT",
Maintainer: "me@me",
Vendor: "asdf",
Homepage: "https://goreleaser.com/{{ .Env.PRO }}",
NFPMOverridables: config.NFPMOverridables{
PackageName: "foo",
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
for _, goos := range []string{"linux"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
Name: "subdir/mybin",
Path: binPath,
Goarch: goarch,
Goos: goos,
Type: artifact.Binary,
Extra: map[string]interface{}{
"ID": "default",
},
})
}
}
require.NoError(t, Pipe{}.Run(ctx))
packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()

for _, pkg := range packages {
format := pkg.ExtraOr("Format", "").(string)
require.NotEmpty(t, format)
// the final binary should contain the evaluated bindir (after template eval)
require.ElementsMatch(t, []string{
"/usr/lib/pro/nagios/plugins/mybin",
}, destinations(pkg.ExtraOr("Files", files.Contents{}).(files.Contents)))
}
}

func sources(contents files.Contents) []string {
result := make([]string, 0, len(contents))
for _, f := range contents {
Expand Down
2 changes: 1 addition & 1 deletion www/docs/customization/blob.md
Expand Up @@ -47,7 +47,7 @@ blobs:

# 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 latest one found will be used.
# another file with the same name exists, the last one found will be used.
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down
4 changes: 2 additions & 2 deletions www/docs/customization/checksum.md
Expand Up @@ -23,7 +23,7 @@ checksum:
# If left empty, all published binaries, archives, linux packages and source archives
# are included in the checksums file.
# Default is an empty list.
ids:
ids:
- foo
- bar

Expand All @@ -33,7 +33,7 @@ checksum:

# 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 latest one found will be used.
# another file with the same name exists, the last one found will be used.
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down
3 changes: 2 additions & 1 deletion www/docs/customization/nfpm.md
Expand Up @@ -90,7 +90,8 @@ nfpms:
replaces:
- fish

# Override default /usr/local/bin destination for binaries
# Template to the path that the binaries should be installed.
# Defaults to `/usr/local/bin`.
bindir: /usr/bin

# Version Epoch.
Expand Down
6 changes: 3 additions & 3 deletions www/docs/customization/release.md
Expand Up @@ -67,7 +67,7 @@ release:

# 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 latest one found will be used.
# another file with the same name exists, the last one found will be used.
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down Expand Up @@ -108,7 +108,7 @@ release:

# 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 latest one found will be used.
# another file with the same name exists, the last one found will be used.
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down Expand Up @@ -153,7 +153,7 @@ release:

# 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 latest one found will be used.
# another file with the same name exists, the last one found will be used.
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
Expand Down

0 comments on commit 5ce6521

Please sign in to comment.