Skip to content

Commit

Permalink
feat: deb now fully support config files
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Feb 17, 2018
1 parent fe3c9fe commit b7566dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
52 changes: 30 additions & 22 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,39 +184,47 @@ func createControl(now time.Time, instSize int64, md5sums []byte, info nfpm.Info
}); err != nil {
return nil, err
}
var header = tar.Header{
Name: "control",
Size: int64(body.Len()),
Mode: 0644,
ModTime: now,
Typeflag: tar.TypeReg,

for name, content := range map[string][]byte{
"control": body.Bytes(),
"md5sums": md5sums,
"conffiles": conffiles(info),
} {
if err := newFileInsideTarGz(out, name, content, now); err != nil {
return nil, err
}
}
if err := out.WriteHeader(&header); err != nil {
return nil, errors.Wrap(err, "cannot write header of control file to control.tar.gz")

if err := out.Close(); err != nil {
return nil, errors.Wrap(err, "closing control.tar.gz")
}
if _, err := out.Write(body.Bytes()); err != nil {
return nil, errors.Wrap(err, "cannot write control file to control.tar.gz")
if err := compress.Close(); err != nil {
return nil, errors.Wrap(err, "closing control.tar.gz")
}
return buf.Bytes(), nil
}

header = tar.Header{
Name: "md5sums",
Size: int64(len(md5sums)),
func newFileInsideTarGz(out *tar.Writer, name string, content []byte, now time.Time) error {
var header = tar.Header{
Name: name,
Size: int64(len(content)),
Mode: 0644,
ModTime: now,
Typeflag: tar.TypeReg,
}
if err := out.WriteHeader(&header); err != nil {
return nil, errors.Wrap(err, "cannot write header of md5sums file to control.tar.gz")
return errors.Wrapf(err, "cannot write header of %s file to control.tar.gz", name)
}
if _, err := out.Write(md5sums); err != nil {
return nil, errors.Wrap(err, "cannot write md5sums file to control.tar.gz")
if _, err := out.Write(content); err != nil {
return errors.Wrapf(err, "cannot write %s file to control.tar.gz", name)
}
return nil
}

if err := out.Close(); err != nil {
return nil, errors.Wrap(err, "closing control.tar.gz")
}
if err := compress.Close(); err != nil {
return nil, errors.Wrap(err, "closing control.tar.gz")
func conffiles(info nfpm.Info) []byte {
var confs []string
for _, dst := range info.ConfigFiles {
confs = append(confs, dst)
}
return buf.Bytes(), nil
return []byte(strings.Join(confs, "\n") + "\n")
}
9 changes: 9 additions & 0 deletions deb/deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,12 @@ func TestDebNoInfo(t *testing.T) {
var err = Default.Package(nfpm.WithDefaults(nfpm.Info{}), ioutil.Discard)
assert.NoError(t, err)
}

func TestConffiles(t *testing.T) {
out := conffiles(nfpm.Info{
ConfigFiles: map[string]string{
"fake": "/etc/fake",
},
})
assert.Equal(t, "/etc/fake\n", string(out), "should have a trailing empty line")
}

0 comments on commit b7566dc

Please sign in to comment.