From 45cc894de31df37ee5d949138fd7f1acead3419e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 7 Dec 2023 10:24:31 -0300 Subject: [PATCH] fix: reuse keys func --- apk/apk.go | 13 ++----------- arch/arch.go | 15 +++------------ deb/deb.go | 13 ++----------- internal/maps/maps.go | 12 ++++++++++++ 4 files changed, 19 insertions(+), 34 deletions(-) create mode 100644 internal/maps/maps.go diff --git a/apk/apk.go b/apk/apk.go index c265a497..33304bc4 100644 --- a/apk/apk.go +++ b/apk/apk.go @@ -38,7 +38,6 @@ import ( "io" "net/mail" "os" - "sort" "strings" "sync/atomic" "text/template" @@ -46,6 +45,7 @@ import ( "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" ) @@ -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 @@ -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 -} diff --git a/arch/arch.go b/arch/arch.go index 3e39f63f..5a689381 100644 --- a/arch/arch.go +++ b/arch/arch.go @@ -10,7 +10,6 @@ import ( "fmt" "io" "os" - "sort" "strconv" "strings" "time" @@ -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" ) var ErrInvalidPkgName = errors.New("archlinux: package names may only contain alphanumeric characters or one of ., _, +, or -, and may not start with hyphen or dot") @@ -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 } @@ -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]) @@ -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 -} diff --git a/deb/deb.go b/deb/deb.go index a6a03eeb..7c73b45b 100644 --- a/deb/deb.go +++ b/deb/deb.go @@ -13,7 +13,6 @@ import ( "io" "os" "path/filepath" - "sort" "strings" "text/template" "time" @@ -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" ) const packagerName = "deb" @@ -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 @@ -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 -} diff --git a/internal/maps/maps.go b/internal/maps/maps.go new file mode 100644 index 00000000..7a6065b1 --- /dev/null +++ b/internal/maps/maps.go @@ -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 +}