Skip to content

Commit

Permalink
fix: reuse keys func
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 7, 2023
1 parent f83d358 commit 45cc894
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 34 deletions.
13 changes: 2 additions & 11 deletions apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ import (
"io"
"net/mail"
"os"
"sort"
"strings"
"sync/atomic"
"text/template"
"time"

"github.com/goreleaser/nfpm/v2"
"github.com/goreleaser/nfpm/v2/files"
"github.com/goreleaser/nfpm/v2/internal/maps"
"github.com/goreleaser/nfpm/v2/internal/sign"
gzip "github.com/klauspost/pgzip"
)
Expand Down Expand Up @@ -352,7 +352,7 @@ func createBuilderControl(info *nfpm.Info, size int64, dataDigest []byte) func(t
".pre-deinstall": info.Scripts.PreRemove,
".post-deinstall": info.Scripts.PostRemove,
}
for _, name := range keys(scripts) {
for _, name := range maps.Keys(scripts) {
path := scripts[name]
if path == "" {
continue
Expand Down Expand Up @@ -512,12 +512,3 @@ func writeControl(w io.Writer, data controlData) error {
})
return template.Must(tmpl.Parse(controlTemplate)).Execute(w, data)
}

func keys(m map[string]string) []string {
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
15 changes: 3 additions & 12 deletions arch/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"time"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/goreleaser/nfpm/v2/files"
"github.com/klauspost/compress/zstd"
"github.com/klauspost/pgzip"
"golang.org/x/exp/maps"

Check failure on line 21 in arch/arch.go

View workflow job for this annotation

GitHub Actions / build-pkgs

no required module provides package golang.org/x/exp/maps; to add it:

Check failure on line 21 in arch/arch.go

View workflow job for this annotation

GitHub Actions / htmltest

no required module provides package golang.org/x/exp/maps; to add it:

Check failure on line 21 in arch/arch.go

View workflow job for this annotation

GitHub Actions / ruleguard / scan

no required module provides package golang.org/x/exp/maps; to add it:

Check failure on line 21 in arch/arch.go

View workflow job for this annotation

GitHub Actions / ruleguard / scan

could not import golang.org/x/exp/maps (invalid package name: "")

Check failure on line 21 in arch/arch.go

View workflow job for this annotation

GitHub Actions / ruleguard / scan

could not import golang.org/x/exp/maps (invalid package name: "")

Check failure on line 21 in arch/arch.go

View workflow job for this annotation

GitHub Actions / govulncheck / scan

no required module provides package golang.org/x/exp/maps; to add it:

Check failure on line 21 in arch/arch.go

View workflow job for this annotation

GitHub Actions / govulncheck / scan

could not import golang.org/x/exp/maps (invalid package name: "")
)

var ErrInvalidPkgName = errors.New("archlinux: package names may only contain alphanumeric characters or one of ., _, +, or -, and may not start with hyphen or dot")
Expand Down Expand Up @@ -402,7 +402,7 @@ func createPkginfo(info *nfpm.Info, tw *tar.Writer, totalSize int64) (*MtreeEntr
}

func writeKVPairs(w io.Writer, pairs map[string]string) error {
for _, key := range keys(pairs) {
for _, key := range maps.Keys(pairs) {
if err := writeKVPair(w, key, pairs[key]); err != nil {
return err
}
Expand Down Expand Up @@ -569,7 +569,7 @@ func createScripts(info *nfpm.Info, tw *tar.Writer) error {
}

func writeScripts(w io.Writer, scripts map[string]string) error {
for _, script := range keys(scripts) {
for _, script := range maps.Keys(scripts) {
fmt.Fprintf(w, "function %s() {\n", script)

fl, err := os.Open(scripts[script])
Expand All @@ -592,12 +592,3 @@ func writeScripts(w io.Writer, scripts map[string]string) error {

return nil
}

func keys(m map[string]string) []string {
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
13 changes: 2 additions & 11 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
Expand All @@ -26,6 +25,7 @@ import (
"github.com/goreleaser/nfpm/v2/internal/sign"
"github.com/klauspost/compress/zstd"
"github.com/ulikunitz/xz"
"golang.org/x/exp/maps"

Check failure on line 28 in deb/deb.go

View workflow job for this annotation

GitHub Actions / ruleguard / scan

could not import golang.org/x/exp/maps (invalid package name: "")

Check failure on line 28 in deb/deb.go

View workflow job for this annotation

GitHub Actions / ruleguard / scan

could not import golang.org/x/exp/maps (invalid package name: "")

Check failure on line 28 in deb/deb.go

View workflow job for this annotation

GitHub Actions / govulncheck / scan

could not import golang.org/x/exp/maps (invalid package name: "")
)

const packagerName = "deb"
Expand Down Expand Up @@ -607,7 +607,7 @@ func createControl(instSize int64, md5sums []byte, info *nfpm.Info) (controlTarG
},
}

for _, filename := range keys(specialFiles) {
for _, filename := range maps.Keys(specialFiles) {
dets := specialFiles[filename]
if dets.fileName == "" {
continue
Expand Down Expand Up @@ -801,12 +801,3 @@ func writeControl(w io.Writer, data controlData) error {
})
return template.Must(tmpl.Parse(controlTemplate)).Execute(w, data)
}

func keys[T any](m map[string]T) []string {
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
12 changes: 12 additions & 0 deletions internal/maps/maps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package maps

import "sort"

func Keys[T any](m map[string]T) []string {
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}

0 comments on commit 45cc894

Please sign in to comment.