-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
78 lines (71 loc) · 1.98 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
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"github.com/ntsd/cross-clipboard/pkg/config"
"github.com/ntsd/cross-clipboard/pkg/crossclipboard"
"github.com/ntsd/cross-clipboard/pkg/device"
"github.com/ntsd/cross-clipboard/pkg/xerror"
"github.com/ntsd/cross-clipboard/ui"
)
func main() {
isTerminalMode := flag.Bool("t", false, "run in terminal mode")
flag.Parse()
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
crossClipboard, err := crossclipboard.NewCrossClipboard(cfg)
if err != nil {
log.Fatal(err)
}
if isTerminalMode != nil && *isTerminalMode {
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt)
for {
select {
case l := <-crossClipboard.LogChan:
log.Println("log: ", l)
case err := <-crossClipboard.ErrorChan:
var fatalErr *xerror.FatalError
if errors.As(err, &fatalErr) {
log.Fatal(fmt.Errorf("fatal error: %w", fatalErr))
}
log.Println(fmt.Errorf("runtime error: %w", err))
case <-crossClipboard.ClipboardManager.ClipboardsHistoryUpdated:
// log.Printf("clipboard history updated, history size %d", len(crossClipboard.ClipboardManager.ClipboardsHistory))
case <-crossClipboard.DeviceManager.DevicesUpdated:
for _, dv := range crossClipboard.DeviceManager.Devices {
if dv.Status == device.StatusPending {
fmt.Printf("device %s wanted to connect (Y/n)", dv.Name)
var input string
fmt.Scanln(&input)
if input == "n" {
dv.Block()
} else {
err = dv.Trust()
if err != nil {
log.Println(fmt.Errorf("can not trust device: %w", err))
}
}
crossClipboard.DeviceManager.UpdateDevice(dv)
}
}
case exit := <-exitSignal:
log.Printf("got %s signal. aborting...\n", exit)
err := crossClipboard.Stop()
if err != nil {
log.Panicln(fmt.Errorf("error to graceful eixt: %w", err))
}
os.Exit(0)
}
}
} else {
view := ui.NewView(crossClipboard)
view.Start()
}
}