From 632979ac059b6e9973858aee6e9c8e93896b172b Mon Sep 17 00:00:00 2001 From: Slawomir Figiel Date: Thu, 15 Aug 2024 03:31:02 +0200 Subject: [PATCH] feat: env var substitution in description (#848) Co-authored-by: Slawek Figiel <4471997+fivitti@users.noreply.github.com> --- nfpm.go | 1 + nfpm_test.go | 10 ++++++++++ testdata/env-fields.yaml | 1 + www/docs/configuration.md | 1 + 4 files changed, 13 insertions(+) diff --git a/nfpm.go b/nfpm.go index 970dd0d2..180c2e16 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/nfpm_test.go b/nfpm_test.go index 4124766a..44371ee4 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 46ee6edc..b86d305d 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 diff --git a/www/docs/configuration.md b/www/docs/configuration.md index c441bf3c..3d0a9e9c 100644 --- a/www/docs/configuration.md +++ b/www/docs/configuration.md @@ -83,6 +83,7 @@ priority: extra maintainer: Carlos Alexandro Becker # 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.