Files
nfpm/rpm/scripts.go
db22372bfb fix: add context to deb and rpm errors (#1088)
* chore: improve nfpm maintenance path

* test: Address error context review feedback

Replace the byte-counting writer with errWriter and buffer archive headers
so the failure occurs during the body write. Assert diagnostic context
alongside wrapped causes in each deb and rpm regression case.

Addresses the outstanding review feedback in #1088.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b0096264-a208-4a57-a1b0-b902e47faddd
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Maya Chen <275405107+otjdiepluong@users.noreply.github.com>
Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b0096264-a208-4a57-a1b0-b902e47faddd
Copilot-Session: ea527b37-94d2-489a-8305-66cef357f4ba
2026-10-12 17:37:30 +00:00

94 lines
2.2 KiB
Go

package rpm
import (
"fmt"
"os"
"github.com/goreleaser/nfpm/v2"
"go.digitalxero.dev/rpm"
)
// scriptBodies holds the resolved (file-read) bodies of every lifecycle script.
type scriptBodies struct {
preTrans string
preIn string
preUn string
postIn string
postUn string
postTrans string
verify string
}
// readScripts reads every configured script file into memory once, so both the
// binary builder and the source-package spec generator can consume them.
func readScripts(info *nfpm.Info) (scriptBodies, error) {
read := func(kind, path string) (string, error) {
if path == "" {
return "", nil
}
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("cannot read %s script %s: %w", kind, path, err)
}
return string(data), nil
}
var (
s scriptBodies
err error
)
if s.preTrans, err = read("pretrans", info.RPM.Scripts.PreTrans); err != nil {
return s, err
}
if s.preIn, err = read("preinstall", info.Scripts.PreInstall); err != nil {
return s, err
}
if s.preUn, err = read("preremove", info.Scripts.PreRemove); err != nil {
return s, err
}
if s.postIn, err = read("postinstall", info.Scripts.PostInstall); err != nil {
return s, err
}
if s.postUn, err = read("postremove", info.Scripts.PostRemove); err != nil {
return s, err
}
if s.postTrans, err = read("posttrans", info.RPM.Scripts.PostTrans); err != nil {
return s, err
}
if s.verify, err = read("verify", info.RPM.Scripts.Verify); err != nil {
return s, err
}
return s, nil
}
// applyScripts attaches the configured lifecycle scripts to the package builder.
func applyScripts(b rpm.PackageBuilder, info *nfpm.Info) error {
s, err := readScripts(info)
if err != nil {
return err
}
if s.preTrans != "" {
b.Pretrans().WithScript(s.preTrans).Done()
}
if s.preIn != "" {
b.Prein().WithScript(s.preIn).Done()
}
if s.preUn != "" {
b.Preun().WithScript(s.preUn).Done()
}
if s.postIn != "" {
b.Postin().WithScript(s.postIn).Done()
}
if s.postUn != "" {
b.Postun().WithScript(s.postUn).Done()
}
if s.postTrans != "" {
b.Posttrans().WithScript(s.postTrans).Done()
}
if s.verify != "" {
b.VerifyScript().WithScript(s.verify).Done()
}
return nil
}