Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow env expansion in conflicts, suggests, recommends, depends… #548

Merged
merged 5 commits into from Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 27 additions & 4 deletions nfpm.go
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/fs"
"os"
"strings"
"sync"

"github.com/AlekSi/pointer"
Expand Down Expand Up @@ -148,16 +149,38 @@ func (c *Config) Validate() error {
return nil
}

func remove(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}

func (c *Config) expandEnvVarsStringSlice(items []string) []string {
for i, dep := range items {
val := strings.TrimSpace(os.Expand(dep, c.envMappingFunc))
items[i] = val
}
for i, val := range items {
if val == "" {
items = remove(items, i)
}
}

return items
}

func (c *Config) expandEnvVars() {
// Version related fields
c.Info.Release = os.Expand(c.Info.Release, c.envMappingFunc)
c.Info.Version = os.Expand(c.Info.Version, c.envMappingFunc)
c.Info.Prerelease = os.Expand(c.Info.Prerelease, c.envMappingFunc)
c.Info.Arch = os.Expand(c.Info.Arch, c.envMappingFunc)
for _, override := range c.Overrides {
for i, dep := range override.Depends {
override.Depends[i] = os.Expand(dep, c.envMappingFunc)
}
for or, override := range c.Overrides {
override.Conflicts = c.expandEnvVarsStringSlice(override.Conflicts)
override.Depends = c.expandEnvVarsStringSlice(override.Depends)
override.Replaces = c.expandEnvVarsStringSlice(override.Replaces)
override.Recommends = c.expandEnvVarsStringSlice(override.Recommends)
override.Provides = c.expandEnvVarsStringSlice(override.Provides)
override.Suggests = c.expandEnvVarsStringSlice(override.Suggests)
c.Overrides[or] = override
}

// Maintainer and vendor fields
Expand Down
22 changes: 22 additions & 0 deletions nfpm_test.go
Expand Up @@ -340,6 +340,28 @@ overrides:
require.Len(t, info.Overrides["rpm"].Depends, 1)
require.Equal(t, "package = 1.0.0", info.Overrides["rpm"].Depends[0])
})

t.Run("depends-strips-empty", func(t *testing.T) {
os.Clearenv()
os.Setenv("VERSION", version)
os.Setenv("PKG", "")
info, err := nfpm.Parse(strings.NewReader(`---
name: foo
overrides:
deb:
depends:
- ${PKG}
- package (= ${VERSION})
rpm:
depends:
- package = ${VERSION}
- ${PKG}`))
require.NoError(t, err)
require.Len(t, info.Overrides["deb"].Depends, 1)
require.Equal(t, "package (= 1.0.0)", info.Overrides["deb"].Depends[0])
require.Len(t, info.Overrides["rpm"].Depends, 1)
require.Equal(t, "package = 1.0.0", info.Overrides["rpm"].Depends[0])
})
}

func TestOverrides(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions testdata/acceptance/core.overrides.yaml
Expand Up @@ -27,14 +27,20 @@ contents:
type: config
overrides:
rpm:
depends:
- (bash >= 4.4 or fish)
scripts:
preinstall: ./testdata/acceptance/scripts/preinstall.sh
postremove: ./testdata/acceptance/scripts/postremove.sh
deb:
depends:
- bash (>= 4.4) | fish
scripts:
postinstall: ./testdata/acceptance/scripts/postinstall.sh
preremove: ./testdata/acceptance/scripts/preremove.sh
apk:
depends:
- bash (>= 4.4) | fish
scripts:
postinstall: ./testdata/acceptance/scripts/postinstall.sh
preremove: ./testdata/acceptance/scripts/preremove.sh
18 changes: 18 additions & 0 deletions www/docs/configuration.md
Expand Up @@ -81,28 +81,46 @@ changelog: "changelog.yaml"
disable_globbing: false

# Packages it replaces. (overridable)
# This will expand any env var you set in the field, eg ${REPLACE_BLA}
# the env var approach can be used to account for differences in platforms
replaces:
- foobar
- ${REPLACE_BLA}

# Packages it provides. (overridable)
# This will expand any env var you set in the field, eg ${PROVIDES_BLA}
# the env var approach can be used to account for differences in platforms
provides:
- bar
- ${PROVIDES_BLA}

# Dependencies. (overridable)
# This will expand any env var you set in the field, eg ${DEPENDS_NGINX}
# the env var approach can be used to account for differences in platforms
# eg rhel needs nginx >= 1:1.18 and deb needs nginx (>= 1.18.0)
depends:
- git
- ${DEPENDS_NGINX}

# Recommended packages. (overridable)
# This will expand any env var you set in the field, eg ${RECOMMENDS_BLA}
# the env var approach can be used to account for differences in platforms
recommends:
- golang
- ${RECOMMENDS_BLA}

# Suggested packages. (overridable)
# This will expand any env var you set in the field, eg ${SUGGESTS_BLA}
# the env var approach can be used to account for differences in platforms
suggests:
- bzr

# Packages it conflicts with. (overridable)
# This will expand any env var you set in the field, eg ${CONFLICTS_BLA}
# the env var approach can be used to account for differences in platforms
conflicts:
- mercurial
- ${CONFLICTS_BLA}

# Contents to add to the package
# This can be binaries or any other files.
Expand Down