Skip to content

Commit 6668d4f

Browse files
committed
Go API: NewConfigurator takes unbound bin path
1 parent 03e29f3 commit 6668d4f

File tree

8 files changed

+36
-32
lines changed

8 files changed

+36
-32
lines changed

cmd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, args []string
6868
client := &http.Client{Timeout: clientTimeout}
6969
// Create configurators
7070
dnsCrypto := dnscrypto.New(client, "", "") // TODO checksums for build
71-
const unboundDir = "/unbound"
72-
dnsConf := unbound.NewConfigurator(logger, os.OpenFile, dnsCrypto, unboundDir)
71+
const unboundEtcDir = "/unbound"
72+
const unboundPath = "/unbound/unbound"
73+
dnsConf := unbound.NewConfigurator(logger, os.OpenFile, dnsCrypto, unboundEtcDir, unboundPath)
7374

7475
if len(args) > 1 && args[1] == "build" {
7576
if err := dnsConf.SetupFiles(ctx); err != nil {

pkg/unbound/command.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@ import (
1010

1111
func (c *configurator) Start(ctx context.Context, verbosityDetailsLevel uint8) (
1212
stdout io.ReadCloser, wait func() error, err error) {
13-
configFilepath := filepath.Join(c.unboundDir, unboundConfigFilename)
13+
configFilepath := filepath.Join(c.unboundEtcDir, unboundConfigFilename)
1414
args := []string{"-d", "-c", configFilepath}
1515
if verbosityDetailsLevel > 0 {
1616
args = append(args, "-"+strings.Repeat("v", int(verbosityDetailsLevel)))
1717
}
1818

19-
binFilepath := filepath.Join(c.unboundDir, unboundBinFilename)
2019
// Only logs to stderr
21-
_, stdout, wait, err = c.commander.Start(ctx, binFilepath, args...)
20+
_, stdout, wait, err = c.commander.Start(ctx, c.unboundPath, args...)
2221
return stdout, wait, err
2322
}
2423

2524
func (c *configurator) Version(ctx context.Context) (version string, err error) {
26-
binFilepath := filepath.Join(c.unboundDir, unboundBinFilename)
27-
output, err := c.commander.Run(ctx, binFilepath, "-V")
25+
output, err := c.commander.Run(ctx, c.unboundPath, "-V")
2826
if err != nil {
2927
return "", fmt.Errorf("unbound version: %w", err)
3028
}

pkg/unbound/command_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import (
1414
func Test_Start(t *testing.T) {
1515
t.Parallel()
1616
mockCtrl := gomock.NewController(t)
17-
const unboundDir = "/unbound"
17+
const unboundEtcDir = "/unbound"
18+
const unboundPath = "/usr/sbin/unbound"
1819
commander := mock_command.NewMockCommander(mockCtrl)
19-
commander.EXPECT().Start(context.Background(), "/unbound/unbound", "-d", "-c", "/unbound/unbound.conf", "-vv").
20+
commander.EXPECT().Start(context.Background(), unboundPath, "-d", "-c", "/unbound/unbound.conf", "-vv").
2021
Return(nil, nil, nil, nil).Times(1)
2122
c := &configurator{
22-
commander: commander,
23-
unboundDir: unboundDir,
23+
commander: commander,
24+
unboundEtcDir: unboundEtcDir,
25+
unboundPath: unboundPath,
2426
}
2527
stdout, waitFn, err := c.Start(context.Background(), 2)
2628
assert.Nil(t, stdout)
@@ -56,12 +58,14 @@ func Test_Version(t *testing.T) {
5658
commander := mock_command.NewMockCommander(mockCtrl)
5759
ctx := context.Background()
5860

59-
const unboundDir = "/unbound"
60-
commander.EXPECT().Run(ctx, "/unbound/unbound", "-V").
61+
const unboundEtcDir = "/unbound"
62+
const unboundPath = "/usr/sbin/unbound"
63+
commander.EXPECT().Run(ctx, unboundPath, "-V").
6164
Return(tc.runOutput, tc.runErr).Times(1)
6265
c := &configurator{
63-
unboundDir: unboundDir,
64-
commander: commander,
66+
commander: commander,
67+
unboundEtcDir: unboundEtcDir,
68+
unboundPath: unboundPath,
6569
}
6670
version, err := c.Version(ctx)
6771
if tc.err != nil {

pkg/unbound/conf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import (
1717

1818
func (c *configurator) MakeUnboundConf(settings models.Settings,
1919
hostnamesLines, ipsLines []string, username string, puid, pgid int) (err error) {
20-
configFilepath := filepath.Join(c.unboundDir, unboundConfigFilename)
20+
configFilepath := filepath.Join(c.unboundEtcDir, unboundConfigFilename)
2121
file, err := c.openFile(configFilepath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
2222
if err != nil {
2323
return err
2424
}
2525

26-
lines := generateUnboundConf(settings, hostnamesLines, ipsLines, c.unboundDir, username)
26+
lines := generateUnboundConf(settings, hostnamesLines, ipsLines, c.unboundEtcDir, username)
2727
_, err = file.WriteString(strings.Join(lines, "\n"))
2828
if err != nil {
2929
_ = file.Close()

pkg/unbound/constants.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
const (
1010
unboundConfigFilename = "unbound.conf"
11-
unboundBinFilename = "unbound"
1211
cacertsFilename = "ca-certificates.crt"
1312
rootHints = "root.hints"
1413
rootKey = "root.key"

pkg/unbound/dns.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@ type Configurator interface {
3030
}
3131

3232
type configurator struct {
33-
openFile os.OpenFileFunc
34-
commander command.Commander
35-
resolver *net.Resolver
36-
dnscrypto dnscrypto.DNSCrypto
37-
unboundDir string
33+
openFile os.OpenFileFunc
34+
commander command.Commander
35+
resolver *net.Resolver
36+
dnscrypto dnscrypto.DNSCrypto
37+
unboundEtcDir string
38+
unboundPath string
3839
}
3940

4041
func NewConfigurator(logger logging.Logger, openFile os.OpenFileFunc,
41-
dnscrypto dnscrypto.DNSCrypto, unboundDir string) Configurator {
42+
dnscrypto dnscrypto.DNSCrypto, unboundEtcDir, unboundPath string) Configurator {
4243
return &configurator{
43-
openFile: openFile,
44-
commander: command.NewCommander(),
45-
resolver: net.DefaultResolver,
46-
dnscrypto: dnscrypto,
47-
unboundDir: unboundDir,
44+
openFile: openFile,
45+
commander: command.NewCommander(),
46+
resolver: net.DefaultResolver,
47+
dnscrypto: dnscrypto,
48+
unboundEtcDir: unboundEtcDir,
49+
unboundPath: unboundPath,
4850
}
4951
}

pkg/unbound/files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
const includeConfFilename = "include.conf"
99

1010
func (c *configurator) createEmptyIncludeConf() error {
11-
filepath := filepath.Join(c.unboundDir, includeConfFilename)
11+
filepath := filepath.Join(c.unboundEtcDir, includeConfFilename)
1212
file, err := c.openFile(filepath, os.O_CREATE, 0644)
1313
if err != nil {
1414
return err

pkg/unbound/roots.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (c *configurator) downloadRootHints(ctx context.Context) error {
2929
return err
3030
}
3131

32-
filepath := filepath.Join(c.unboundDir, rootHints)
32+
filepath := filepath.Join(c.unboundEtcDir, rootHints)
3333
file, err := c.openFile(filepath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
3434
if err != nil {
3535
return err
@@ -57,7 +57,7 @@ func (c *configurator) downloadRootKeys(ctx context.Context) error {
5757
return err
5858
}
5959

60-
filepath := filepath.Join(c.unboundDir, rootKey)
60+
filepath := filepath.Join(c.unboundEtcDir, rootKey)
6161
file, err := c.openFile(filepath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
6262
if err != nil {
6363
return err

0 commit comments

Comments
 (0)