Skip to content

Commit

Permalink
fix: improve deb special files
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Dec 7, 2023
1 parent ab21009 commit f83d358
Showing 1 changed file with 52 additions and 39 deletions.
91 changes: 52 additions & 39 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -575,47 +576,50 @@ func createControl(instSize int64, md5sums []byte, info *nfpm.Info) (controlTarG
mode int64
}

specialFiles := map[string]*fileAndMode{}
specialFiles[info.Scripts.PreInstall] = &fileAndMode{
fileName: "preinst",
mode: 0o755,
}
specialFiles[info.Scripts.PostInstall] = &fileAndMode{
fileName: "postinst",
mode: 0o755,
}
specialFiles[info.Scripts.PreRemove] = &fileAndMode{
fileName: "prerm",
mode: 0o755,
}
specialFiles[info.Scripts.PostRemove] = &fileAndMode{
fileName: "postrm",
mode: 0o755,
}
specialFiles[info.Overridables.Deb.Scripts.Rules] = &fileAndMode{
fileName: "rules",
mode: 0o755,
}
specialFiles[info.Overridables.Deb.Scripts.Templates] = &fileAndMode{
fileName: "templates",
mode: 0o644,
}
specialFiles[info.Overridables.Deb.Scripts.Config] = &fileAndMode{
fileName: "config",
mode: 0o755,
specialFiles := map[string]*fileAndMode{
"preinst": {
fileName: info.Scripts.PreInstall,
mode: 0o755,
},
"postinst": {
fileName: info.Scripts.PostInstall,
mode: 0o755,
},
"prerm": {
fileName: info.Scripts.PreRemove,
mode: 0o755,
},
"postrm": {
fileName: info.Scripts.PostRemove,
mode: 0o755,
},
"rules": {
fileName: info.Overridables.Deb.Scripts.Rules,
mode: 0o755,
},
"templates": {
fileName: info.Overridables.Deb.Scripts.Templates,
mode: 0o644,
},
"config": {
fileName: info.Overridables.Deb.Scripts.Config,
mode: 0o755,
},
}

for path, destMode := range specialFiles {
if path != "" {
if err := newFilePathInsideTar(
out,
path,
destMode.fileName,
destMode.mode,
info.MTime,
); err != nil {
return nil, err
}
for _, filename := range keys(specialFiles) {
dets := specialFiles[filename]
if dets.fileName == "" {
continue
}
if err := newFilePathInsideTar(
out,
dets.fileName,
filename,
dets.mode,
info.MTime,
); err != nil {
return nil, err

Check warning on line 622 in deb/deb.go

View check run for this annotation

Codecov / codecov/patch

deb/deb.go#L615-L622

Added lines #L615 - L622 were not covered by tests
}
}

Expand Down Expand Up @@ -797,3 +801,12 @@ 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
}

0 comments on commit f83d358

Please sign in to comment.