mirror of
https://github.com/goreleaser/nfpm
synced 2024-05-18 20:46:19 +02:00
feat: maintainer scripts for deb
This commit is contained in:
parent
fe1364fd98
commit
992e1aac41
50
deb/deb.go
50
deb/deb.go
|
@ -5,6 +5,7 @@ import (
|
|||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
// #nosec
|
||||
"crypto/md5"
|
||||
|
@ -189,6 +190,19 @@ func createControl(instSize int64, md5sums []byte, info nfpm.Info) (controlTarGz
|
|||
}
|
||||
}
|
||||
|
||||
for script, dest := range map[string]string{
|
||||
info.Scripts.PreInstall: "preinst",
|
||||
info.Scripts.PostInstall: "postinst",
|
||||
info.Scripts.PreRemove: "prerm",
|
||||
info.Scripts.PostRemove: "postrm",
|
||||
} {
|
||||
if script != "" {
|
||||
if err := newScriptInsideTarGz(out, script, dest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := out.Close(); err != nil {
|
||||
return nil, errors.Wrap(err, "closing control.tar.gz")
|
||||
}
|
||||
|
@ -198,22 +212,44 @@ func createControl(instSize int64, md5sums []byte, info nfpm.Info) (controlTarGz
|
|||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func newItemInsideTarGz(out *tar.Writer, content []byte, header tar.Header) error {
|
||||
if err := out.WriteHeader(&header); err != nil {
|
||||
return errors.Wrapf(err, "cannot write header of %s file to control.tar.gz", header.Name)
|
||||
}
|
||||
if _, err := out.Write(content); err != nil {
|
||||
return errors.Wrapf(err, "cannot write %s file to control.tar.gz", header.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newFileInsideTarGz(out *tar.Writer, name string, content []byte) error {
|
||||
var header = tar.Header{
|
||||
return newItemInsideTarGz(out, content, tar.Header{
|
||||
Name: name,
|
||||
Size: int64(len(content)),
|
||||
Mode: 0644,
|
||||
ModTime: time.Now(),
|
||||
Typeflag: tar.TypeReg,
|
||||
Format: tar.FormatGNU,
|
||||
})
|
||||
}
|
||||
|
||||
func newScriptInsideTarGz(out *tar.Writer, path string, dest string) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := out.WriteHeader(&header); err != nil {
|
||||
return errors.Wrapf(err, "cannot write header of %s file to control.tar.gz", name)
|
||||
content, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := out.Write(content); err != nil {
|
||||
return errors.Wrapf(err, "cannot write %s file to control.tar.gz", name)
|
||||
}
|
||||
return nil
|
||||
return newItemInsideTarGz(out, content, tar.Header{
|
||||
Name: dest,
|
||||
Size: int64(len(content)),
|
||||
Mode: 0655,
|
||||
ModTime: time.Now(),
|
||||
Typeflag: tar.TypeReg,
|
||||
Format: tar.FormatGNU,
|
||||
})
|
||||
}
|
||||
|
||||
// this is needed because the data.tar.gz file should have the empty folders
|
||||
|
|
9
nfpm.go
9
nfpm.go
|
@ -57,6 +57,15 @@ type Info struct {
|
|||
Bindir string `yaml:"bindir,omitempty"`
|
||||
Files map[string]string `yaml:"files,omitempty"`
|
||||
ConfigFiles map[string]string `yaml:"config_files,omitempty"`
|
||||
Scripts Scripts `yaml:"scripts,omitempty"`
|
||||
}
|
||||
|
||||
// Scripts contains information about maintainer scripts for packages
|
||||
type Scripts struct {
|
||||
PreInstall string `yaml:"preinstall,omitempty"`
|
||||
PostInstall string `yaml:"postinstall,omitempty"`
|
||||
PreRemove string `yaml:"preremove,omitempty"`
|
||||
PostRemove string `yaml:"postremove,omitempty"`
|
||||
}
|
||||
|
||||
// Validate the given Info and returns an error if it is invalid.
|
||||
|
|
Loading…
Reference in New Issue