Skip to content

Commit 15a4f26

Browse files
committed
refactor: move Zeroconf options to a struct
1 parent f0c13a9 commit 15a4f26

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

cmd/daemon/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,17 @@ func (app *App) withAppPlayer(ctx context.Context, appPlayerFunc func(context.Co
244244
}
245245

246246
// start zeroconf server and dispatch
247-
z, err := zeroconf.NewZeroconf(app.log, app.cfg.ZeroconfPort, app.cfg.DeviceName, app.deviceId, app.deviceType, app.cfg.ZeroconfInterfacesToAdvertise)
247+
z, err := zeroconf.NewZeroconf(zeroconf.Options{
248+
Log: app.log,
249+
250+
Port: app.cfg.ZeroconfPort,
251+
252+
DeviceName: app.cfg.DeviceName,
253+
DeviceId: app.deviceId,
254+
DeviceType: app.deviceType,
255+
256+
InterfacesToAdvertise: app.cfg.ZeroconfInterfacesToAdvertise,
257+
})
248258
if err != nil {
249259
return fmt.Errorf("failed initializing zeroconf: %w", err)
250260
}

zeroconf/zeroconf.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/devgianlu/go-librespot/dh"
1818
devicespb "github.com/devgianlu/go-librespot/proto/spotify/connectstate/devices"
1919
"github.com/grandcat/zeroconf"
20+
log "github.com/sirupsen/logrus"
2021
)
2122

2223
type Zeroconf struct {
@@ -46,16 +47,29 @@ type NewUserRequest struct {
4647
result chan bool
4748
}
4849

49-
func NewZeroconf(log librespot.Logger, port int, deviceName, deviceId string, deviceType devicespb.DeviceType, interfacesToAdvertise []string) (_ *Zeroconf, err error) {
50-
z := &Zeroconf{log: log, deviceId: deviceId, deviceName: deviceName, deviceType: deviceType}
50+
type Options struct {
51+
Log librespot.Logger
52+
53+
Port int
54+
55+
DeviceName string
56+
DeviceId string
57+
DeviceType devicespb.DeviceType
58+
59+
DiscoveryImplementation string
60+
InterfacesToAdvertise []string
61+
}
62+
63+
func NewZeroconf(opts Options) (_ *Zeroconf, err error) {
64+
z := &Zeroconf{log: opts.Log, deviceId: opts.DeviceId, deviceName: opts.DeviceName, deviceType: opts.DeviceType}
5165
z.reqsChan = make(chan NewUserRequest)
5266

5367
z.dh, err = dh.NewDiffieHellman()
5468
if err != nil {
5569
return nil, fmt.Errorf("failed initializing diffiehellman: %w", err)
5670
}
5771

58-
z.listener, err = net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
72+
z.listener, err = net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", opts.Port))
5973
if err != nil {
6074
return nil, fmt.Errorf("failed starting zeroconf listener: %w", err)
6175
}
@@ -64,7 +78,7 @@ func NewZeroconf(log librespot.Logger, port int, deviceName, deviceId string, de
6478
log.Infof("zeroconf server listening on port %d", listenPort)
6579

6680
var ifaces []net.Interface
67-
for _, ifaceName := range interfacesToAdvertise {
81+
for _, ifaceName := range opts.InterfacesToAdvertise {
6882
liface, err := net.InterfaceByName(ifaceName)
6983
if err != nil {
7084
return nil, fmt.Errorf("failed to get info for network interface %s: %w", ifaceName, err)
@@ -74,7 +88,7 @@ func NewZeroconf(log librespot.Logger, port int, deviceName, deviceId string, de
7488
log.Info(fmt.Sprintf("advertising on network interface %s", ifaceName))
7589
}
7690

77-
z.server, err = zeroconf.Register(deviceName, "_spotify-connect._tcp", "local.", listenPort, []string{"CPath=/", "VERSION=1.0", "Stack=SP"}, ifaces)
91+
z.server, err = zeroconf.Register(z.deviceName, "_spotify-connect._tcp", "local.", listenPort, []string{"CPath=/", "VERSION=1.0", "Stack=SP"}, ifaces)
7892
if err != nil {
7993
return nil, fmt.Errorf("failed registering zeroconf server: %w", err)
8094
}

0 commit comments

Comments
 (0)