Repository: goreleaser/nfpm
Pull request #848 (2024-08-14 16:22:14)

feat: env var substitution in description
Hello!

I noticed you support the environment variable interpolation in many fields in a configuration but not in the description.
This merge request fills this gap.

I added the unit tests for the new code and altered the docs. Please notify me if I missed something or if my code doesn't fit your coding guidelines - I will fix it.

Regards!


Patch:
diff --git a/flake.lock b/flake.lock
index 491e53f..29505fc 100644
--- a/flake.lock
+++ b/flake.lock
@@ -20,11 +20,11 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1711703276,
-        "narHash": "sha256-iMUFArF0WCatKK6RzfUJknjem0H9m4KgorO/p3Dopkk=",
+        "lastModified": 1723362943,
+        "narHash": "sha256-dFZRVSgmJkyM0bkPpaYRtG/kRMRTorUIDj8BxoOt1T4=",
         "owner": "nixos",
         "repo": "nixpkgs",
-        "rev": "d8fe5e6c92d0d190646fb9f1056741a229980089",
+        "rev": "a58bc8ad779655e790115244571758e8de055e3d",
         "type": "github"
       },
       "original": {
diff --git a/flake.nix b/flake.nix
index bb62192..7c0eac7 100644
--- a/flake.nix
+++ b/flake.nix
@@ -15,17 +15,19 @@
           src = ./.;
           ldflags = [ "-s" "-w" "-X main.version=dev" "-X main.builtBy=flake" ];
           doCheck = false;
-          vendorHash = "sha256-g57tLk2+WWcdG0COqkQD7eLYG0TdC0RnlhLF6Qt4woY=";
+          vendorHash = "";
         };
 
         devShells.default = pkgs.mkShell {
+          shellHook = "go mod tidy";
+        };
+
+        # nix develop .#dev
+        devShells.dev = pkgs.mkShell {
           packages = with pkgs; [
-            go
             go-task
             gofumpt
-            nix-prefetch
           ];
-          shellHook = "go mod tidy";
         };
 
         # nix develop .#packagers
diff --git a/nfpm.go b/nfpm.go
index 970dd0d..180c2e1 100644
--- a/nfpm.go
+++ b/nfpm.go
@@ -231,6 +231,7 @@ func (c *Config) expandEnvVars() {
 	c.Info.Homepage = os.Expand(c.Info.Homepage, c.envMappingFunc)
 	c.Info.Maintainer = os.Expand(c.Info.Maintainer, c.envMappingFunc)
 	c.Info.Vendor = os.Expand(c.Info.Vendor, c.envMappingFunc)
+	c.Info.Description = os.Expand(c.Info.Description, c.envMappingFunc)
 
 	// Package signing related fields
 	c.Info.Deb.Signature.KeyFile = os.Expand(c.Deb.Signature.KeyFile, c.envMappingFunc)
diff --git a/www/docs/configuration.md b/www/docs/configuration.md
index c441bf3..3d0a9e9 100644
--- a/www/docs/configuration.md
+++ b/www/docs/configuration.md
@@ -83,6 +83,7 @@ priority: extra
 maintainer: Carlos Alexandro Becker <root@carlosbecker.com>
 
 # Description.
+# This will expand any env var you set in the field, e.g. description: ${DESCRIPTION}
 # Defaults to `no description given`.
 # Most packagers call for a one-line synopsis of the package. Some (like deb)
 # also call for a multi-line description starting on the second line.


Test patch:
diff --git a/nfpm_test.go b/nfpm_test.go
index 4124766..44371ee 100644
--- a/nfpm_test.go
+++ b/nfpm_test.go
@@ -313,11 +313,13 @@ func TestParseFile(t *testing.T) {
 	t.Setenv("RPM_KEY_FILE", "my/rpm/key/file")
 	t.Setenv("TEST_RELEASE_ENV_VAR", "1234")
 	t.Setenv("TEST_PRERELEASE_ENV_VAR", "beta1")
+	t.Setenv("TEST_DESCRIPTION_ENV_VAR", "description")
 	config, err := parseAndValidate("./testdata/env-fields.yaml")
 	require.NoError(t, err)
 	require.Equal(t, fmt.Sprintf("v%s", os.Getenv("GOROOT")), config.Version)
 	require.Equal(t, "1234", config.Release)
 	require.Equal(t, "beta1", config.Prerelease)
+	require.Equal(t, "My description", config.Description)
 	require.Equal(t, "my/rpm/key/file", config.RPM.Signature.KeyFile)
 	require.Equal(t, "hard/coded/file", config.Deb.Signature.KeyFile)
 	require.Equal(t, "", config.APK.Signature.KeyFile)
@@ -381,6 +383,7 @@ func TestOptionsFromEnvironment(t *testing.T) {
 		maintainerEmail = "nope@example.com"
 		homepage        = "https://nfpm.goreleaser.com"
 		vcsBrowser      = "https://github.com/goreleaser/nfpm"
+		description     = "barfoo"
 	)
 
 	t.Run("platform", func(t *testing.T) {
@@ -440,6 +443,13 @@ maintainer: '"$GIT_COMMITTER_NAME" <$GIT_COMMITTER_EMAIL>'
 		require.Equal(t, homepage, info.Homepage)
 	})
 
+	t.Run("description", func(t *testing.T) {
+		t.Setenv("DESCRIPTION", description)
+		info, err := nfpm.Parse(strings.NewReader("name: foo\ndescription: $DESCRIPTION"))
+		require.NoError(t, err)
+		require.Equal(t, description, info.Description)
+	})
+
 	t.Run("global passphrase", func(t *testing.T) {
 		t.Setenv("NFPM_PASSPHRASE", globalPass)
 		info, err := nfpm.Parse(strings.NewReader("name: foo"))
diff --git a/testdata/env-fields.yaml b/testdata/env-fields.yaml
index 46ee6ed..b86d305 100644
--- a/testdata/env-fields.yaml
+++ b/testdata/env-fields.yaml
@@ -4,6 +4,7 @@ arch: "amd64"
 version: "v$GOROOT"
 release: ${TEST_RELEASE_ENV_VAR}
 prerelease: ${TEST_PRERELEASE_ENV_VAR}
+description: My ${TEST_DESCRIPTION_ENV_VAR}
 contents:
 - src: ./testdata/whatever.conf
   dst: /etc/foo/regular.conf