-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
102 lines (87 loc) · 1.73 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
package main
import (
"fmt"
"github.com/RileyR387/sc-dtc-client/dtc"
"github.com/pborman/getopt/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var (
Version string
quit chan int
signals chan os.Signal
client dtc.DtcClient
)
func init() {
quit = make(chan int)
signals = make(chan os.Signal)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
}
func main() {
args := dtc.ConnectArgs{
viper.GetString("dtc.Host"),
viper.GetString("dtc.Port"),
viper.GetString("dtc.HistPort"),
viper.GetString("dtc.Username"),
viper.GetString("dtc.Password"),
viper.GetBool("dtc.Reconnect"),
}
err := client.Connect(args)
if err != nil {
log.Fatalf("Failed to connect with: %v\n", err)
}
CatchInterupt()
ScreenServer()
os.Exit(0)
}
func ScreenServer() {
for {
select {
case <-quit:
if client.Connected() {
client.Terminate()
}
return
default:
if !client.Terminated() {
time.Sleep(time.Second)
} else {
log.Printf("Client unexpectedly disconnected from server\n")
// TODO: Attempt reconnect? Client connection will need timeout function..
time.Sleep(time.Second)
return
}
}
}
}
func CatchInterupt() {
log.Infof("%v %v - Started", me, Version)
go func() {
i := <-signals
log.Printf("Received signal: %v, exiting...\n", i)
quit <- 0
}()
}
func usage(msg ...string) {
if len(msg) > 0 {
fmt.Fprintf(os.Stderr, "%s\n", msg[0])
}
// strip off the first line of generated usage
b := &strings.Builder{}
getopt.PrintUsage(b)
u := strings.SplitAfterN(b.String(), "\n", 2)
fmt.Printf(`Usage: %s [OPTIONS]
OPTIONS
%s
`, me, u[1])
os.Exit(1)
}
func ShowVersion() {
fmt.Printf("%v %v\n", me, Version)
os.Exit(0)
}