Skip to content

Commit

Permalink
feat: env expand contents (#721)
Browse files Browse the repository at this point in the history
* feat: env expand contents

closes #719

* fix: expand

* docs: update
  • Loading branch information
caarlos0 authored Oct 17, 2023
1 parent 32c34dd commit 61d7e9d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Content struct {
Type string `yaml:"type,omitempty" json:"type,omitempty" jsonschema:"enum=symlink,enum=ghost,enum=config,enum=config|noreplace,enum=dir,enum=tree,enum=,default="`
Packager string `yaml:"packager,omitempty" json:"packager,omitempty"`
FileInfo *ContentFileInfo `yaml:"file_info,omitempty" json:"file_info,omitempty"`
Expand bool `yaml:"expand,omitempty" json:"expand,omitempty"`
}

type ContentFileInfo struct {
Expand Down
14 changes: 14 additions & 0 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ func (c *Config) expandEnvVarsStringSlice(items []string) []string {
return items
}

func (c *Config) expandEnvVarsContents(contents files.Contents) files.Contents {
for i := range contents {
f := contents[i]
if !f.Expand {
continue
}
f.Destination = strings.TrimSpace(os.Expand(f.Destination, c.envMappingFunc))
f.Source = strings.TrimSpace(os.Expand(f.Source, c.envMappingFunc))
}
return contents
}

func (c *Config) expandEnvVars() {
// Version related fields
c.Info.Release = os.Expand(c.Info.Release, c.envMappingFunc)
Expand All @@ -185,13 +197,15 @@ func (c *Config) expandEnvVars() {
c.Overrides[or].Recommends = c.expandEnvVarsStringSlice(c.Overrides[or].Recommends)
c.Overrides[or].Provides = c.expandEnvVarsStringSlice(c.Overrides[or].Provides)
c.Overrides[or].Suggests = c.expandEnvVarsStringSlice(c.Overrides[or].Suggests)
c.Overrides[or].Contents = c.expandEnvVarsContents(c.Overrides[or].Contents)
}
c.Info.Conflicts = c.expandEnvVarsStringSlice(c.Info.Conflicts)
c.Info.Depends = c.expandEnvVarsStringSlice(c.Info.Depends)
c.Info.Replaces = c.expandEnvVarsStringSlice(c.Info.Replaces)
c.Info.Recommends = c.expandEnvVarsStringSlice(c.Info.Recommends)
c.Info.Provides = c.expandEnvVarsStringSlice(c.Info.Provides)
c.Info.Suggests = c.expandEnvVarsStringSlice(c.Info.Suggests)
c.Info.Contents = c.expandEnvVarsContents(c.Info.Contents)

// Basic metadata fields
c.Info.Name = os.Expand(c.Info.Name, c.envMappingFunc)
Expand Down
32 changes: 32 additions & 0 deletions nfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,38 @@ deb:
require.NoError(t, err)
require.Equal(t, vcsBrowser, info.Deb.Fields["Vcs-Browser"])
})

t.Run("contents", func(t *testing.T) {
t.Setenv("ARCH", "amd64")
t.Setenv("NAME", "foo")
info, err := nfpm.Parse(strings.NewReader(`
name: foo
contents:
- src: '${NAME}_${ARCH}'
dst: /usr/bin/${NAME}
expand: true
- src: '${NAME}'
dst: /usr/bin/bar
overrides:
deb:
contents:
- src: '${NAME}_${ARCH}'
dst: /debian/usr/bin/${NAME}
expand: true
`))
require.NoError(t, err)
require.Equal(t, 2, info.Contents.Len())
content1 := info.Contents[0]
require.Equal(t, "/usr/bin/foo", content1.Destination)
require.Equal(t, "foo_amd64", content1.Source)
content2 := info.Contents[1]
require.Equal(t, "/usr/bin/bar", content2.Destination)
require.Equal(t, "${NAME}", content2.Source)
content3 := info.Overrides["deb"].Contents[0]
require.Equal(t, "/debian/usr/bin/foo", content3.Destination)
require.Equal(t, "foo_amd64", content3.Source)
})
}

func TestOverrides(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions www/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ contents:
file_info:
mode: 0700

# Using `expand: true`, environment variables will be expanded in both
# src and dst.
- dst: /usr/local/bin/${NAME}
src: "${NAME}"
expand: true

# Umask to be used on files without explicit mode set.
#
# By default, nFPM will inherit the mode of the original file that's being
Expand Down

0 comments on commit 61d7e9d

Please sign in to comment.