-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
160 lines (142 loc) · 2.91 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"runtime"
)
const (
protocolVersion = `0.1.3`
cliVersion = `0.1.6`
)
var (
messageQueue = make(chan *message, 10)
devices *deviceList
installFlag bool
developerFlag bool
versionFlag bool
devicesFlag bool
)
type message struct {
ID string `json:"id,omitempty"`
Type messageType `json:"type"`
Data json.RawMessage `json:"data,omitempty"`
}
func writePump(ch <-chan *message) {
var (
enc = newEncoder(os.Stdout)
err error
)
for msg := range ch {
switch msg.Type {
case typeDevices:
out := &message{
ID: msg.ID,
Type: typeDevices,
}
if out.Data, err = json.Marshal(devices.all()); err != nil {
log(err)
continue
}
if err = enc.Encode(out); err != nil {
log(err)
continue
}
case typeShare:
share := &messageShare{}
if err = json.Unmarshal(msg.Data, &share); err != nil {
log(err)
continue
}
if dev, ok := devices.get(share.Target); ok {
if err = dev.share(share.URL); err != nil {
log(err)
continue
}
}
case typeDeviceUpdate:
if err = enc.Encode(msg); err != nil {
log(err)
continue
}
case typeVersion:
out := &message{
Type: typeVersion,
}
if out.Data, err = json.Marshal(protocolVersion); err != nil {
log(err)
continue
}
if err = enc.Encode(out); err != nil {
log(err)
continue
}
case typeError:
log(fmt.Errorf("typeError: %+v", msg))
default:
log(fmt.Errorf("Unhandled message type: %+v", msg.Type))
}
}
}
func readPump(ch chan<- *message) {
defer close(ch)
dec := newDecoder(os.Stdin)
for {
msg := &message{}
if err := dec.Decode(msg); err != nil {
if err == io.EOF {
return
}
log(err)
continue
}
ch <- msg
}
}
func log(err error) {
if _, e := fmt.Fprintln(os.Stderr, err); e != nil {
panic(e)
}
}
func init() {
var err error
flag.BoolVar(&installFlag, `install`, false, `Perform installation`)
flag.BoolVar(&developerFlag, `dev`, false, `Install as developer`)
flag.BoolVar(&versionFlag, `version`, false, `Display version`)
flag.BoolVar(&devicesFlag, `devices`, false, `Display visible devices`)
flag.Parse()
if devices, err = newDeviceList(); err != nil {
panic(err)
}
}
func main() {
if versionFlag {
fmt.Printf("kdeconnect-chrome-extension version %s (protocol %s), built with %s\n", cliVersion, protocolVersion, runtime.Version())
os.Exit(0)
}
if installFlag {
if err := install(developerFlag); err != nil {
panic(err)
}
os.Exit(0)
}
if err := devices.getDevices(); err != nil {
log(err)
}
if devicesFlag {
for _, dev := range devices.devices {
fmt.Printf("- %s: %s (paired: %v; reachable: %v)\n", dev.Name, dev.ID, dev.IsTrusted, dev.IsReachable)
}
os.Exit(0)
}
go writePump(messageQueue)
readPump(messageQueue)
shutdown()
}
func shutdown() {
if err := devices.Close(); err != nil {
panic(err)
}
}