Skip to content

Commit

Permalink
feat: expand variables in platform field (#601)
Browse files Browse the repository at this point in the history
* feat: expand variables in platform field

Surprising that GOARCH was expanded, but GOOS was not.

More consistent to expand both of them the same way ?

Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>

* fix: check platform for all packagers

Implement platform for deb packager, goes into Architecture.

Validate that the platform is "linux", for Alpine and Arch.

Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>

Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Jan 7, 2023
1 parent b547d33 commit c195d93
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 2 deletions.
3 changes: 3 additions & 0 deletions apk/apk.go
Expand Up @@ -108,6 +108,9 @@ func (*Apk) ConventionalExtension() string {

// Package writes a new apk package to the given writer using the given info.
func (*Apk) Package(info *nfpm.Info, apk io.Writer) (err error) {
if info.Platform != "linux" {
return fmt.Errorf("invalid platform: %s", info.Platform)
}
info = ensureValidArch(info)
if err = info.Validate(); err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions apk/apk_test.go
Expand Up @@ -174,6 +174,15 @@ func TestDefaultWithArch(t *testing.T) {
}
}

func TestApkPlatform(t *testing.T) {
f, err := os.CreateTemp("", "test*.apk")
require.NoError(t, err)
info := exampleInfo()
info.Platform = "darwin"
err = Default.Package(info, f)
require.Error(t, err)
}

func TestNoInfo(t *testing.T) {
err := Default.Package(nfpm.WithDefaults(&nfpm.Info{}), io.Discard)
require.Error(t, err)
Expand Down
4 changes: 4 additions & 0 deletions arch/arch.go
Expand Up @@ -115,6 +115,10 @@ func isOneOf(r rune, rr ...rune) bool {

// Package writes a new archlinux package to the given writer using the given info.
func (ArchLinux) Package(info *nfpm.Info, w io.Writer) error {
if info.Platform != "linux" {
return fmt.Errorf("invalid platform: %s", info.Platform)
}
info = ensureValidArch(info)
if err := info.Validate(); err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions arch/arch_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"io"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -99,6 +100,15 @@ func TestArch(t *testing.T) {
}
}

func TestArchPlatform(t *testing.T) {
f, err := os.CreateTemp("", "test*.pkg.tar.zstd")
require.NoError(t, err)
info := exampleInfo()
info.Platform = "darwin"
err = Default.Package(info, f)
require.Error(t, err)
}

func TestArchNoFiles(t *testing.T) {
info := exampleInfo()
info.Contents = nil
Expand Down
2 changes: 1 addition & 1 deletion deb/deb.go
Expand Up @@ -779,7 +779,7 @@ Version: {{ if .Info.Epoch}}{{ .Info.Epoch }}:{{ end }}{{.Info.Version}}
{{- if .Info.Release}}-{{ .Info.Release }}{{- end }}
Section: {{.Info.Section}}
Priority: {{.Info.Priority}}
Architecture: {{.Info.Arch}}
Architecture: {{ if ne .Info.Platform "linux"}}{{ .Info.Platform }}-{{ end }}{{.Info.Arch}}
{{- /* Optional fields */ -}}
{{- if .Info.Maintainer}}
Maintainer: {{.Info.Maintainer}}
Expand Down
45 changes: 45 additions & 0 deletions deb/deb_test.go
Expand Up @@ -107,6 +107,51 @@ func TestDeb(t *testing.T) {
}
}

func TestDebPlatform(t *testing.T) {
f, err := os.CreateTemp("", "test*.deb")
require.NoError(t, err)
info := exampleInfo()
info.Platform = "darwin"
err = Default.Package(info, f)
require.NoError(t, err)
}

func extractDebArchitecture(deb *bytes.Buffer) string {
for _, s := range strings.Split(deb.String(), "\n") {
if strings.Contains(s, "Architecture: ") {
return strings.TrimPrefix(s, "Architecture: ")
}
}
return ""
}

func splitDebArchitecture(deb *bytes.Buffer) (string, string) {
a := extractDebArchitecture(deb)
if strings.Contains(a, "-") {
f := strings.Split(a, "-")
return f[0], f[1]
}
return "linux", a
}

func TestDebOS(t *testing.T) {
info := exampleInfo()
var buf bytes.Buffer
err := writeControl(&buf, controlData{info, 0})
require.NoError(t, err)
o, _ := splitDebArchitecture(&buf)
require.Equal(t, "linux", o)
}

func TestDebArch(t *testing.T) {
info := exampleInfo()
var buf bytes.Buffer
err := writeControl(&buf, controlData{info, 0})
require.NoError(t, err)
_, a := splitDebArchitecture(&buf)
require.Equal(t, "amd64", a)
}

func extractDebVersion(deb *bytes.Buffer) string {
for _, s := range strings.Split(deb.String(), "\n") {
if strings.Contains(s, "Version: ") {
Expand Down
1 change: 1 addition & 0 deletions nfpm.go
Expand Up @@ -177,6 +177,7 @@ func (c *Config) expandEnvVars() {
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.Platform = os.Expand(c.Info.Platform, c.envMappingFunc)
c.Info.Arch = os.Expand(c.Info.Arch, c.envMappingFunc)
for or := range c.Overrides {
c.Overrides[or].Conflicts = c.expandEnvVarsStringSlice(c.Overrides[or].Conflicts)
Expand Down
16 changes: 16 additions & 0 deletions nfpm_test.go
Expand Up @@ -244,13 +244,29 @@ func TestOptionsFromEnvironment(t *testing.T) {
debPass = "password123"
rpmPass = "secret"
apkPass = "foobar"
platform = "linux"
arch = "amd64"
release = "3"
version = "1.0.0"
vendor = "GoReleaser"
packager = "nope"
maintainerEmail = "nope@example.com"
)

t.Run("platform", func(t *testing.T) {
t.Setenv("OS", platform)
info, err := nfpm.Parse(strings.NewReader("name: foo\nplatform: $OS"))
require.NoError(t, err)
require.Equal(t, platform, info.Platform)
})

t.Run("arch", func(t *testing.T) {
t.Setenv("ARCH", arch)
info, err := nfpm.Parse(strings.NewReader("name: foo\narch: $ARCH"))
require.NoError(t, err)
require.Equal(t, arch, info.Arch)
})

t.Run("version", func(t *testing.T) {
os.Clearenv()
os.Setenv("VERSION", version)
Expand Down
16 changes: 16 additions & 0 deletions rpm/rpm_test.go
Expand Up @@ -106,6 +106,14 @@ func TestRPM(t *testing.T) {
rpm, err := rpmutils.ReadRpm(file)
require.NoError(t, err)

os, err := rpm.Header.GetString(rpmutils.OS)
require.NoError(t, err)
require.Equal(t, "linux", os)

arch, err := rpm.Header.GetString(rpmutils.ARCH)
require.NoError(t, err)
require.Equal(t, archToRPM["amd64"], arch)

version, err := rpm.Header.GetString(rpmutils.VERSION)
require.NoError(t, err)
require.Equal(t, "1.0.0", version)
Expand Down Expand Up @@ -134,6 +142,14 @@ func TestRPM(t *testing.T) {
require.Equal(t, "Foo does things", description)
}

func TestRPMPlatform(t *testing.T) {
f, err := os.CreateTemp("", "test*.rpm")
require.NoError(t, err)
info := exampleInfo()
info.Platform = "darwin"
require.NoError(t, Default.Package(info, f))
}

func TestRPMGroup(t *testing.T) {
f, err := os.CreateTemp("", "test.rpm")
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion www/docs/configuration.md
Expand Up @@ -18,7 +18,8 @@ name: foo
arch: amd64

# Platform.
# This is only used by the rpm packager.
# This will expand any env var you set in the field, e.g. version: ${GOOS}
# This is only used by the rpm and deb packagers.
# Examples: `linux` (default), `darwin`
platform: linux

Expand Down

0 comments on commit c195d93

Please sign in to comment.