This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
132 lines (118 loc) · 2.2 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
package main
import (
"bytes"
"fmt"
"github.com/dynport/dgtk/cli"
)
func main() {
router := cli.NewRouter()
router.RegisterFunc("status", status, "Status")
router.RegisterFunc("connect", connect, "Connect")
router.RegisterFunc("disconnect", disconnect, "Disconnect")
switch e := router.RunWithArgs(); e {
case nil, cli.ErrorHelpRequested, cli.ErrorNoRoute:
// ignore
return
default:
logger.Fatal(e)
}
}
func disconnect() error {
b := browser()
ok, e := online()
if e != nil {
return e
}
if !ok {
logger.Printf("you are not online!")
}
e = b.Visit(disconnectUrl)
if e != nil {
return e
}
logger.Printf("you are disconnected now")
return nil
}
func available() bool {
b := browser()
e := b.Visit(railControllerUrl)
if e != nil {
return false
}
return true
}
func connect() error {
b := browser()
logger.Printf("checking online status")
if ok, e := online(); e == nil && ok {
logger.Printf(yellow("you are already connected!"))
return nil
}
logger.Printf("checking for availablity")
if !available() {
return fmt.Errorf("hotspot seems to be not available")
}
body, e := b.Body()
if e != nil {
return e
}
if !bytes.Contains(body, []byte("HotSpot verfügbar")) {
return fmt.Errorf("hotspot not available")
}
logger.Printf("hotspot available")
e = b.Visit(connectUrl)
if e != nil {
return e
}
forms, e := b.Forms()
if e != nil {
return e
}
if len(forms) != 1 {
return fmt.Errorf("expected to find 1 form, found %d", len(forms))
}
form := forms[0]
login, e := getEnv(envLogin)
if e != nil {
return e
}
pwd, e := getEnv(envPwd)
if e != nil {
return e
}
e = form.FillIn("username", login)
if e != nil {
return e
}
e = form.FillIn("password", pwd)
if e != nil {
return e
}
e = b.Submit(form)
if e != nil {
return e
}
ok, e := online()
if e != nil {
return e
}
if ok {
logger.Printf("you are online now!")
} else {
logger.Printf("seems that you are not online")
}
return nil
}
func status() error {
if ok, e := online(); e == nil && ok {
logger.Printf(green("you are online!"))
return nil
} else {
s := "you are offline"
if !available() {
s += " (hotspot not available)"
}
logger.Printf(red(s))
}
return nil
}