Skip to content

Commit

Permalink
feat: Add support for templating NFPM bindir (#2466)
Browse files Browse the repository at this point in the history
* feat: Add support for templating NFPM bindir

Example: Allow software to be deployed in '/usr/lib64' directory on x86_64 arch
and in '/usr/lib' directory in i386

* test: invalid bindir template

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

* docs: bindir template

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

Co-authored-by: Rémi Ferrand <remi.ferrand@cc.in2p3.fr>
  • Loading branch information
caarlos0 and riton committed Sep 7, 2021
1 parent 7d22a32 commit 8afb5ea
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 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
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

1 comment on commit 8afb5ea

@vercel
Copy link

@vercel vercel bot commented on 8afb5ea Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.