1
1
Fork 0
mirror of https://github.com/goreleaser/nfpm synced 2024-05-24 10:06:16 +02:00

feat: add support for deb templates (#240)

* add support for deb templates

* merge scripts and templates, make mode settable and move Templates to DebScripts

* add templates support to docs
This commit is contained in:
Ivan Vandot 2020-11-03 13:44:59 +01:00 committed by GitHub
parent 0e2c2844bb
commit 7a70791bb0
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 26 deletions

View File

@ -410,15 +410,40 @@ func createControl(instSize int64, md5sums []byte, info *nfpm.Info) (controlTarG
}
}
for script, dest := range map[string]string{
info.Scripts.PreInstall: "preinst",
info.Scripts.PostInstall: "postinst",
info.Scripts.PreRemove: "prerm",
info.Scripts.PostRemove: "postrm",
info.Overridables.Deb.Scripts.Rules: "rules",
} {
if script != "" {
if err := newScriptInsideTarGz(out, script, dest); err != nil {
type fileAndMode struct {
fileName string
mode int64
}
var specialFiles = map[string]*fileAndMode{}
specialFiles[info.Scripts.PreInstall] = &fileAndMode{
fileName: "preinst",
mode: 0755,
}
specialFiles[info.Scripts.PostInstall] = &fileAndMode{
fileName: "postinst",
mode: 0755,
}
specialFiles[info.Scripts.PreRemove] = &fileAndMode{
fileName: "prerm",
mode: 0755,
}
specialFiles[info.Scripts.PostRemove] = &fileAndMode{
fileName: "postrm",
mode: 0755,
}
specialFiles[info.Overridables.Deb.Scripts.Rules] = &fileAndMode{
fileName: "rules",
mode: 0755,
}
specialFiles[info.Overridables.Deb.Scripts.Templates] = &fileAndMode{
fileName: "templates",
mode: 0644,
}
for path, destMode := range specialFiles {
if path != "" {
if err := newFilePathInsideTarGz(out, path, destMode.fileName, destMode.mode); err != nil {
return nil, err
}
}
@ -454,13 +479,7 @@ func newFileInsideTarGz(out *tar.Writer, name string, content []byte) error {
})
}
// normalizePath returns a path separated by slashes, all relative path items
// resolved and relative to the current directory (so it starts with "./").
func normalizePath(src string) string {
return "." + filepath.ToSlash(filepath.Clean(filepath.Join("/", src)))
}
func newScriptInsideTarGz(out *tar.Writer, path, dest string) error {
func newFilePathInsideTarGz(out *tar.Writer, path, dest string, mode int64) error {
file, err := os.Open(path) //nolint:gosec
if err != nil {
return err
@ -472,13 +491,19 @@ func newScriptInsideTarGz(out *tar.Writer, path, dest string) error {
return newItemInsideTarGz(out, content, &tar.Header{
Name: normalizePath(dest),
Size: int64(len(content)),
Mode: 0755,
Mode: mode,
ModTime: time.Now(),
Typeflag: tar.TypeReg,
Format: tar.FormatGNU,
})
}
// normalizePath returns a path separated by slashes, all relative path items
// resolved and relative to the current directory (so it starts with "./").
func normalizePath(src string) string {
return "." + filepath.ToSlash(filepath.Clean(filepath.Join("/", src)))
}
// this is needed because the data.tar.gz file should have the empty folders
// as well, so we walk through the dst and create all subfolders.
func createTree(tarw *tar.Writer, dst string, created map[string]bool) error {

View File

@ -197,17 +197,17 @@ func TestControl(t *testing.T) {
assert.Equal(t, string(bts), w.String())
}
func TestScripts(t *testing.T) {
func TestSpecialFiles(t *testing.T) {
var w bytes.Buffer
var out = tar.NewWriter(&w)
filePath := "../testdata/scripts/preinstall.sh"
assert.Error(t, newScriptInsideTarGz(out, "doesnotexit", "preinst"))
require.NoError(t, newScriptInsideTarGz(out, filePath, "preinst"))
filePath := "testdata/templates.golden"
assert.Error(t, newFilePathInsideTarGz(out, "doesnotexit", "templates", 0644))
require.NoError(t, newFilePathInsideTarGz(out, filePath, "templates", 0644))
var in = tar.NewReader(&w)
header, err := in.Next()
require.NoError(t, err)
assert.Equal(t, "preinst", header.FileInfo().Name())
mode, err := strconv.ParseInt("0755", 8, 64)
assert.Equal(t, "templates", header.FileInfo().Name())
mode, err := strconv.ParseInt("0644", 8, 64)
require.NoError(t, err)
assert.Equal(t, int64(header.FileInfo().Mode()), mode)
data, err := ioutil.ReadAll(in)

4
deb/testdata/templates.golden vendored Normal file
View File

@ -0,0 +1,4 @@
Template: templates/lala
Type: string
Description: Set lala for templates.
For the test purpose this templates.golden is created.

View File

@ -232,7 +232,8 @@ type DebTriggers struct {
// DebScripts is scripts only available on deb packages.
type DebScripts struct {
Rules string `yaml:"rules,omitempty"`
Rules string `yaml:"rules,omitempty"`
Templates string `yaml:"templates,omitempty"`
}
// Scripts contains information about maintainer scripts for packages.

View File

@ -152,11 +152,14 @@ rpm:
# to #NFPM_PASSPHRASE.
key_file: key.gpg
# Custon configuration applied only to the Deb packager.
# Custom configuration applied only to the Deb packager.
deb:
# Custom deb rules script.
# Custom deb special files.
scripts:
# Deb rules script.
rules: foo.sh
# Deb templates file, when using debconf.
templates: templates
# Custom deb triggers
triggers: