This repository has been archived by the owner on Mar 3, 2019. It is now read-only.
/
discover.go
130 lines (116 loc) · 2.74 KB
/
discover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package discover
import (
"errors"
"fmt"
"log"
"net"
"net/url"
"os"
"strconv"
"text/tabwriter"
"github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/av1"
"github.com/tv42/cliutil/flagx"
"github.com/tv42/cliutil/positional"
"github.com/tv42/cliutil/subcommands"
"github.com/tv42/sinus/cli"
"github.com/tv42/sinus/util"
)
type discoverCommand struct {
subcommands.Description
Arguments struct {
positional.Optional
Matches []flagx.Regexp `positional:",metavar=REGEXP"`
}
}
func quote(s string) string {
q := strconv.Quote(s)
if len(s) > 0 && q[0] == '"' && q[len(q)-1] == '"' {
q = q[1 : len(q)-1]
}
return q
}
func hostname(u *url.URL) string {
host := u.Host
if host == "" {
return u.Opaque
}
if h, _, err := net.SplitHostPort(host); err == nil {
return h
}
return host
}
func match(dev *goupnp.RootDevice, matches []flagx.Regexp) bool {
for _, m := range matches {
if m.FindStringIndex(dev.Device.FriendlyName) == nil &&
m.FindStringIndex(dev.Device.Manufacturer) == nil &&
m.FindStringIndex(dev.Device.ModelName) == nil &&
m.FindStringIndex(dev.URLBase.String()) == nil &&
m.FindStringIndex(util.DeviceUUID(dev)) == nil {
return false
}
}
return true
}
func (c *discoverCommand) Run() error {
maybes, err := goupnp.DiscoverDevices(av1.URN_AVTransport_1)
if err != nil {
return err
}
if len(maybes) == 0 {
return errors.New("no devices discovered")
}
devs := make(map[*goupnp.RootDevice]struct{}, len(maybes))
for _, maybe := range maybes {
if maybe.Err != nil {
continue
}
devs[maybe.Root] = struct{}{}
}
if len(devs) == 0 {
for _, maybe := range maybes {
log.Printf("discovery error: %v", maybe.Err)
}
return errors.New("discovery failed")
}
if len(c.Arguments.Matches) > 0 {
for dev := range devs {
if !match(dev, c.Arguments.Matches) {
delete(devs, dev)
}
}
if len(devs) == 0 {
return errors.New("no device matches")
}
}
if len(devs) == 1 {
// we have a match
for dev := range devs {
if err := cli.App.WriteConfig("location", []byte(dev.URLBase.String())); err != nil {
return fmt.Errorf("cannot store location: %v", err)
}
log.Printf("remember device: %v", quote(dev.Device.FriendlyName))
}
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
for dev := range devs {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n",
quote(dev.Device.FriendlyName),
quote(dev.Device.Manufacturer),
quote(dev.Device.ModelName),
quote(hostname(&dev.URLBase)),
quote(util.DeviceUUID(dev)),
)
}
w.Flush()
fmt.Println()
fmt.Println("run `sinus discover REGEXP..` to select a player")
return nil
}
var discover = discoverCommand{
Description: "discover Sonos players in the network",
}
func init() {
subcommands.Register(&discover)
}