Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export CGO_ENABLED=0
export GOPATH?=$(PWD)/../../../../
export GOPATH?=$(shell go env GOPATH)
export DESTDIR?=$(GOPATH)/bin
export GOBIN?=$(DESTDIR)

Expand Down
57 changes: 51 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package debpkg

import (
"fmt"
"github.com/xor-gate/debpkg/internal/config"
"io/ioutil"
"strings"

"github.com/xor-gate/debpkg/internal/config"
)

// Config loads settings from a depkg.yml specfile
Expand Down Expand Up @@ -38,17 +40,29 @@ func (deb *DebPkg) Config(filename string) error {
deb.SetShortDescription(cfg.Description.Short)
deb.SetDescription(cfg.Description.Long)
deb.SetBuiltUsing(cfg.BuiltUsing)
deb.SetDepends(cfg.Depends)
deb.SetRecommends(cfg.Recommends)
deb.SetSuggests(cfg.Suggests)
deb.SetConflicts(cfg.Conflicts)
deb.SetProvides(cfg.Provides)
deb.SetReplaces(cfg.Replaces)

for _, file := range cfg.Files {
err := deb.AddFile(file.Src, file.Dest)
if err != nil {
return fmt.Errorf("error adding file %s: %v", file.Src, err)
if len(file.Src) > 0 {
if err := deb.AddFile(file.Src, file.Dest); err != nil {
return fmt.Errorf("error adding file %s: %v", file.Src, err)
}
} else if len(file.Content) > 0 {
if err := deb.AddFileString(file.Content, file.Dest); err != nil {
return fmt.Errorf("error adding file by string: %v", err)
}
} else {
return fmt.Errorf("need either 'content' or a 'src' to add a file")
}
}

for _, dir := range cfg.Directories {
err := deb.AddDirectory(dir)
if err != nil {
if err := deb.AddDirectory(dir); err != nil {
return fmt.Errorf("error adding directory %s: %v", dir, err)
}
}
Expand All @@ -60,5 +74,36 @@ func (deb *DebPkg) Config(filename string) error {
}
}

if len(cfg.ControlExtra.Preinst) > 0 {
if strings.ContainsAny(cfg.ControlExtra.Preinst, "\n") {
deb.AddControlExtraString("preinst", cfg.ControlExtra.Preinst)
} else {
deb.AddControlExtra("preinst", cfg.ControlExtra.Preinst)
}
}

if len(cfg.ControlExtra.Postinst) > 0 {
if strings.ContainsAny(cfg.ControlExtra.Postinst, "\n") {
deb.AddControlExtraString("postinst", cfg.ControlExtra.Postinst)
} else {
deb.AddControlExtra("postinst", cfg.ControlExtra.Postinst)
}
}

if len(cfg.ControlExtra.Prerm) > 0 {
if strings.ContainsAny(cfg.ControlExtra.Prerm, "\n") {
deb.AddControlExtraString("prerm", cfg.ControlExtra.Prerm)
} else {
deb.AddControlExtra("prerm", cfg.ControlExtra.Prerm)
}
}

if len(cfg.ControlExtra.Postrm) > 0 {
if strings.ContainsAny(cfg.ControlExtra.Postrm, "\n") {
deb.AddControlExtraString("postrm", cfg.ControlExtra.Postrm)
} else {
deb.AddControlExtra("postrm", cfg.ControlExtra.Postrm)
}
}
return nil
}
90 changes: 88 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package debpkg

import (
"github.com/stretchr/testify/assert"
"github.com/xor-gate/debpkg/internal/test"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/xor-gate/debpkg/internal/test"
)

// TestExampleConfig verifies if the config example in the root is correctly loaded
Expand All @@ -21,6 +22,12 @@ maintainer_email: deb@pkg.com
homepage: https://github.com/xor-gate/debpkg
section: devel
priority: standard
depends: lsb-release
recommends: nano
suggests: curl
conflicts: pico
provides: editor
replaces: vim
built_using: golang
description:
short: This is a short description
Expand All @@ -36,10 +43,19 @@ files:
- file: debpkg_test.go
- file: README.md
dest: {{.DATAROOTDIR}}/foobar/README.md
- dest: /bin/hello
content: >
#!/bin/bash
echo "hello"
directories:
- ./internal
emptydirs:
- /var/cache/foobar
control_extra:
postrm: Makefile
prerm: Makefile
postinst: Makefile
preinst: Makefile
`
filepath, err := test.WriteTempFile("debpkg.yml", configFile)
assert.Nil(t, err)
Expand Down Expand Up @@ -68,6 +84,52 @@ emptydirs:
assert.Nil(t, testWrite(t, deb))
}

func TestExampleConfig2(t *testing.T) {
const configFile = `name: foo-bar
version: 1.2.3
architecture: amd64
maintainer: Mr. Foo Bar
maintainer_email: foo@bar.org
homepage: https://www.debian.org
section: net
priority: important
control_extra:
postrm: >
#!/bin/bash
echo "post rm!!"
prerm: >
#!/bin/bash
echo "pre rm!!"
postinst: >
#!/bin/bash
echo "post inst!!"
preinst: >
#!/bin/bash
echo "pre inst!!"
`
filepath, err := test.WriteTempFile("debpkg.yml", configFile)
assert.Nil(t, err)

deb := New()
defer deb.Close()

assert.Nil(t, deb.Config(filepath))
assert.Equal(t, "1.2.3", deb.control.info.version.full,
"Unexpected deb.control.info.version.full")
assert.Equal(t, "Mr. Foo Bar", deb.control.info.maintainer,
"Unexpected deb.control.info.maintainer")
assert.Equal(t, "foo@bar.org", deb.control.info.maintainerEmail,
"Unexpected deb.control.info.maintainerEmail")
assert.Equal(t, "https://www.debian.org", deb.control.info.homepage,
"Unexpected deb.control.info.homepage")
assert.Equal(t, "net", deb.control.info.section,
"unexpected section")
assert.Equal(t, PriorityImportant, deb.control.info.priority,
"unexpected priority")

assert.Nil(t, testWrite(t, deb))
}

func TestDefaultConfig(t *testing.T) {
filepath, err := test.WriteTempFile("emptyfile.yml", "")
assert.Nil(t, err)
Expand Down Expand Up @@ -107,3 +169,27 @@ func TestNonExistingConfig(t *testing.T) {

assert.NotNil(t, deb.Config("/non/existent/config/file"))
}

func TestInvalidYAML(t *testing.T) {
deb := New()
defer deb.Close()

const configFile = `name: debpkg
foo: bar
`
filepath, err := test.WriteTempFile("debpkg2.yml", configFile)
assert.Nil(t, err)
assert.NotNil(t, deb.Config(filepath))
}

func TestInvalidTemplateVar(t *testing.T) {
deb := New()
defer deb.Close()

const configFile = `name: debpkg
foo: {{.bar}}
`
filepath, err := test.WriteTempFile("debpkg3.yml", configFile)
assert.Nil(t, err)
assert.NotNil(t, deb.Config(filepath))
}
55 changes: 47 additions & 8 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package debpkg
import (
"fmt"
"io"
"io/ioutil"
"math"
"strings"

Expand Down Expand Up @@ -34,10 +35,12 @@ type controlInfo struct {
maintainer string
maintainerEmail string
homepage string
depends string
recommends string
suggests string
conflicts string
replaces string
provides string
replaces string
section string
priority Priority
descrShort string // Short package description
Expand Down Expand Up @@ -106,6 +109,18 @@ func (deb *DebPkg) SetMaintainerEmail(email string) {
deb.control.info.maintainerEmail = email
}

// SetDepends sets the package dependencies. E.g: "lsb-release"
// See: https://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps
func (deb *DebPkg) SetDepends(depends string) {
deb.control.info.depends = depends
}

// SetRecommends sets the package recommendations. E.g: "aptitude"
// See: https://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps
func (deb *DebPkg) SetRecommends(recommends string) {
deb.control.info.recommends = recommends
}

// SetSuggests sets the package suggestions. E.g: "aptitude"
// See: https://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps
func (deb *DebPkg) SetSuggests(suggests string) {
Expand All @@ -124,6 +139,12 @@ func (deb *DebPkg) SetProvides(provides string) {
deb.control.info.provides = provides
}

// SetReplaces sets the names of packages which will be replaced. E.g: "pico"
// See: https://www.debian.org/doc/debian-policy/ch-relationships.html
func (deb *DebPkg) SetReplaces(replaces string) {
deb.control.info.replaces = replaces
}

// SetPriority (recommended). Default set to debpkg.PriorityUnset
// See: https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Priority
// And: https://www.debian.org/doc/debian-policy/ch-archive.html#s-priorities
Expand All @@ -138,12 +159,6 @@ func (deb *DebPkg) SetSection(section string) {
deb.control.info.section = section
}

// SetReplaces sets the names of packages which will be replaced. E.g: "pico"
// See:
func (deb *DebPkg) SetReplaces(replaces string) {
deb.control.info.replaces = replaces
}

// SetHomepage sets the homepage URL of the package. E.g: "https://github.com/foo/bar"
func (deb *DebPkg) SetHomepage(url string) {
deb.control.info.homepage = url
Expand Down Expand Up @@ -193,14 +208,19 @@ func (deb *DebPkg) SetBuiltUsing(info string) {

// AddControlExtraString is the same as AddControlExtra except it uses a string input
func (deb *DebPkg) AddControlExtraString(name, s string) error {
s = strings.Replace(s, "\r\n", "\n", -1)
return deb.control.tgz.AddFileFromBuffer(name, []byte(s))
}

// AddControlExtra allows the advanced user to add custom script to the control.tar.gz Typical usage is
// for preinst, postinst, postrm, prerm: https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
// And: https://www.debian.org/doc/manuals/maint-guide/dother.en.html#maintscripts
func (deb *DebPkg) AddControlExtra(name, filename string) error {
return deb.control.tgz.AddFile(filename, name)
b, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return deb.AddControlExtraString(name, string(b))
}

// AddConffile adds a file to the conffiles so it is treated as configuration files. Configuration files are not
Expand Down Expand Up @@ -276,6 +296,25 @@ func (c *control) String(installedSize uint64) string {
o += fmt.Sprintf("Built-Using: %s\n", c.info.builtUsing)
}

if c.info.depends != "" {
o += fmt.Sprintf("Depends: %s\n", c.info.depends)
}
if c.info.recommends != "" {
o += fmt.Sprintf("Recommends: %s\n", c.info.recommends)
}
if c.info.suggests != "" {
o += fmt.Sprintf("Suggests: %s\n", c.info.suggests)
}
if c.info.conflicts != "" {
o += fmt.Sprintf("Conflicts: %s\n", c.info.conflicts)
}
if c.info.provides != "" {
o += fmt.Sprintf("Provides: %s\n", c.info.provides)
}
if c.info.replaces != "" {
o += fmt.Sprintf("Replaces: %s\n", c.info.replaces)
}

o += fmt.Sprintf("Description: %s\n", c.info.descrShort)
o += fmt.Sprintf("%s", c.info.descr)

Expand Down
Loading