Skip to content

Commit 12a5b06

Browse files
authored
Refactoring and add variable expansion for config file with text/template (#13)
1 parent 732298e commit 12a5b06

18 files changed

+610
-338
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
export CGO_ENABLED=0
12
export GOPATH?=$(PWD)/../../../../
23
export DESTDIR?=$(GOPATH)/bin
34
export GOBIN?=$(DESTDIR)
45

56
all: build
6-
ci: test
7+
ci: env test
8+
9+
env:
10+
go env
11+
echo "---"
712

813
dep:
914
go get -u ./

config.go

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,26 @@ package debpkg
77
import (
88
"fmt"
99
"io/ioutil"
10-
"runtime"
11-
12-
yaml "gopkg.in/yaml.v2"
10+
"github.com/xor-gate/debpkg/internal/config"
1311
)
1412

15-
type debPkgSpecFileCfg struct {
16-
Name string `yaml:"name"`
17-
Version string `yaml:"version"`
18-
Architecture string `yaml:"architecture"`
19-
Maintainer string `yaml:"maintainer"`
20-
MaintainerEmail string `yaml:"maintainer_email"`
21-
Homepage string `yaml:"homepage"`
22-
Section string `yaml:"section"`
23-
Priority string `yaml:"priority"`
24-
BuiltUsing string `yaml:"built_using"`
25-
Description struct {
26-
Short string `yaml:"short"`
27-
Long string `yaml:"long"`
28-
}
29-
Files []struct {
30-
Src string `yaml:"file"`
31-
Dest string `yaml:"dest"`
32-
} `yaml:",flow"`
33-
Directories []string `yaml:",flow"`
34-
EmptyDirectories []string `yaml:"emptydirs,flow"`
35-
}
36-
3713
// Config loads settings from a depkg.yml specfile
3814
func (deb *DebPkg) Config(filename string) error {
39-
cfg := debPkgSpecFileCfg{
40-
Name: "unknown",
41-
Version: "0.1.0+dev",
42-
Architecture: "any",
43-
Maintainer: "anonymous",
44-
MaintainerEmail: "anon@foo.bar",
45-
Homepage: "https://www.google.com",
46-
Section: "misc",
47-
Priority: string(PriorityOptional),
48-
BuiltUsing: runtime.Version(),
15+
data, err := ioutil.ReadFile(filename)
16+
if err != nil {
17+
return fmt.Errorf("problem reading config file: %v", err)
4918
}
50-
cfg.Description.Long = "-"
51-
cfg.Description.Short = "-"
5219

53-
cfgFile, err := ioutil.ReadFile(filename)
20+
dataExpanded, err := ExpandVar(string(data))
5421
if err != nil {
55-
return fmt.Errorf("problem reading config file: %v", err)
22+
return err
5623
}
57-
err = yaml.Unmarshal(cfgFile, &cfg)
24+
25+
cfg, err := config.PkgSpecFileUnmarshal([]byte(dataExpanded))
5826
if err != nil {
59-
return fmt.Errorf("problem unmarshaling config file: %v", err)
27+
return err
6028
}
29+
6130
deb.SetSection(cfg.Section)
6231
deb.SetPriority(Priority(cfg.Priority))
6332
deb.SetName(cfg.Name)

config_test.go

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,54 @@
55
package debpkg
66

77
import (
8-
"io/ioutil"
98
"runtime"
109
"testing"
11-
10+
"github.com/xor-gate/debpkg/internal/test"
1211
"github.com/stretchr/testify/assert"
1312
)
1413

1514
// TestExampleConfig verifies if the config example in the root is correctly loaded
1615
func TestExampleConfig(t *testing.T) {
16+
const configFile = `name: debpkg
17+
version: 7.6.5
18+
architecture: all
19+
maintainer: Deb Pkg
20+
maintainer_email: deb@pkg.com
21+
homepage: https://github.com/xor-gate/debpkg
22+
section: devel
23+
priority: standard
24+
built_using: golang
25+
description:
26+
short: This is a short description
27+
long: >
28+
Bla bla
29+
Bla Bla
30+
.
31+
Dusse
32+
files:
33+
- file: LICENSE
34+
dest: {{.DATAROOTDIR}}/foobar/LICENSE
35+
- file: debpkg.go
36+
- file: debpkg_test.go
37+
- file: README.md
38+
dest: {{.DATAROOTDIR}}/foobar/README.md
39+
directories:
40+
- ./internal
41+
emptydirs:
42+
- /var/cache/foobar
43+
`
44+
filepath, err := test.WriteTempFile("debpkg.yml", configFile)
45+
assert.Nil(t, err)
46+
1747
deb := New()
1848
defer deb.Close()
1949

20-
err := deb.Config("debpkg.yml")
21-
if err != nil {
22-
t.Errorf("debpkg.yml error: %v", err)
23-
}
50+
assert.Nil(t, deb.Config(filepath))
2451
assert.Equal(t, "7.6.5", deb.control.info.version.full,
2552
"Unexpected deb.control.info.version.full")
26-
assert.Equal(t, "Foo Bar", deb.control.info.maintainer,
53+
assert.Equal(t, "Deb Pkg", deb.control.info.maintainer,
2754
"Unexpected deb.control.info.maintainer")
28-
assert.Equal(t, "foo@bar.com", deb.control.info.maintainerEmail,
55+
assert.Equal(t, "deb@pkg.com", deb.control.info.maintainerEmail,
2956
"Unexpected deb.control.info.maintainerEmail")
3057
assert.Equal(t, "https://github.com/xor-gate/debpkg", deb.control.info.homepage,
3158
"Unexpected deb.control.info.homepage")
@@ -37,20 +64,19 @@ func TestExampleConfig(t *testing.T) {
3764
"unexpected section")
3865
assert.Equal(t, PriorityStandard, deb.control.info.priority,
3966
"unexpected priority")
67+
68+
assert.Nil(t, testWrite(t, deb))
4069
}
4170

4271
func TestDefaultConfig(t *testing.T) {
43-
f, err := ioutil.TempFile("", "config")
44-
if err != nil {
45-
t.Errorf("unexpected error creating tempfile: %v", err)
46-
}
47-
f.Close()
72+
filepath, err := test.WriteTempFile("emptyfile.yml", "")
73+
assert.Nil(t, err)
74+
4875
deb := New()
4976
defer deb.Close()
5077

51-
if err := deb.Config(f.Name()); err != nil {
52-
t.Errorf("Unexpected error during load of empty config: %v", err)
53-
}
78+
assert.Nil(t, deb.Config(filepath))
79+
5480
assert.Equal(t, "any", deb.control.info.architecture,
5581
"unexpected architecture")
5682
assert.Equal(t, "anonymous", deb.control.info.maintainer,
@@ -79,6 +105,5 @@ func TestNonExistingConfig(t *testing.T) {
79105
deb := New()
80106
defer deb.Close()
81107

82-
err := deb.Config("/non/existant/config/file")
83-
assert.Error(t, err, "error expected")
108+
assert.NotNil(t, deb.Config("/non/existant/config/file"))
84109
}

constants.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,14 @@ const (
3131
VcsTypeSubversion VcsType = "Svn" // Subversion
3232
)
3333

34-
const debianBinaryVersion = "2.0\n"
35-
const debianFileExtension = "deb"
34+
const (
35+
DefaultInstallPrefix = "/usr"
36+
DefaultBinDir = "bin"
37+
DefaultSbinDir = "sbin"
38+
DefaultSysConfDir = "etc"
39+
DefaultDataRootDir = "share"
40+
)
41+
42+
const debianPathSeparator = "/"
43+
const debianBinaryVersion = "2.0\n"
44+
const debianFileExtension = "deb"

control_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package debpkg
66

77
import (
8-
"os"
9-
"io/ioutil"
108
"testing"
119
"github.com/xor-gate/debpkg/internal/test"
1210
"github.com/stretchr/testify/assert"
@@ -204,8 +202,9 @@ func TestControlFileExtra(t *testing.T) {
204202
const script = `#!/bin/sh
205203
echo "hello world from debpkg"
206204
`
207-
filename := test.TempDir() + string(os.PathSeparator) + t.Name() + ".sh"
208-
assert.Nil(t, ioutil.WriteFile(filename, []byte(script), 0644))
205+
206+
filepath, err := test.WriteTempFile(t.Name() + ".sh", script)
207+
assert.Nil(t, err)
209208

210209
deb.SetName("debpkg-control-file-extra")
211210
deb.SetArchitecture("all")
@@ -216,9 +215,13 @@ echo "hello world from debpkg"
216215
// parsing file '/var/lib/dpkg/tmp.ci/control' near line 6 package 'debpkg-control-file-extra:any':
217216
// end of file during value of field 'Description' (missing final newline)
218217

219-
deb.AddControlExtra("preinst", filename)
220-
deb.AddControlExtra("postinst", filename)
221-
deb.AddControlExtraString("prerm", filename)
222-
deb.AddControlExtraString("postrm", filename)
218+
deb.AddControlExtra("preinst", filepath)
219+
deb.AddControlExtra("postinst", filepath)
220+
deb.AddControlExtra("prerm", filepath)
221+
deb.AddControlExtra("postrm", filepath)
222+
223+
// FIXME AddControlExtra seems to add the full TMPDIR directory inside the control.tar.gz
224+
// E.g on mac var/folders/s5/x8wc0jqd387_sg4py6tg6xq00000gn/T/debpkg-test921120249
225+
223226
assert.Nil(t, testWrite(t, deb))
224227
}

data.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"io"
1111
"os"
1212
"strings"
13-
13+
"path/filepath"
1414
"github.com/xor-gate/debpkg/internal/targzip"
1515
)
1616

@@ -21,11 +21,16 @@ type data struct {
2121
}
2222

2323
func (d *data) addDirectory(dirpath string) error {
24+
dirpath = filepath.Clean(dirpath)
2425
for _, addedDir := range d.dirs {
2526
if addedDir == dirpath {
2627
return nil
2728
}
2829
}
30+
if dirpath == "." {
31+
return nil
32+
}
33+
2934
if err := d.tgz.AddDirectory(dirpath); err != nil {
3035
return err
3136
}
@@ -49,7 +54,35 @@ func (d *data) addEmptyDirectory(dir string) error {
4954
return nil
5055
}
5156

57+
func (d *data) addDirectoriesForFile(filename string) {
58+
dirname := filepath.Dir(filename)
59+
if dirname != "." {
60+
if os.PathSeparator != '/' {
61+
dirname = strings.Replace(dirname, string(os.PathSeparator), "/", -1)
62+
}
63+
dirs := strings.Split(dirname, "/")
64+
var current string
65+
for _, dir := range dirs {
66+
if len(dir) > 0 {
67+
current += dir + "/"
68+
d.addDirectory(current)
69+
}
70+
}
71+
}
72+
}
73+
5274
func (d *data) addFile(filename string, dest ...string) error {
75+
var destfilename string
76+
77+
if len(dest) > 0 && len(dest[0]) > 0 {
78+
destfilename = dest[0]
79+
} else {
80+
destfilename = filename
81+
}
82+
83+
d.addDirectoriesForFile(destfilename)
84+
85+
//
5386
if err := d.tgz.AddFile(filename, dest...); err != nil {
5487
return err
5588
}

0 commit comments

Comments
 (0)