-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (140 loc) · 3.1 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
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
"syscall"
"golang.org/x/crypto/ssh/terminal"
"errors"
"github.com/chzyer/readline"
)
func main() {
Version()
completer := readline.NewPrefixCompleter(
readline.PcItem("list"),
readline.PcItem("history"),
readline.PcItem("bump"),
readline.PcItem("search"),
readline.PcItem("add"),
readline.PcItem("delete"), // dup of remove
readline.PcItem("finish"),
readline.PcItem("rate"),
readline.PcItem("quit"),
readline.PcItem("exit"), // dup of quit
readline.PcItem("version"),
)
reader, err := readline.NewEx(&readline.Config{
Prompt: "> ",
AutoComplete: completer,
})
if err != nil {
panic(err)
}
defer reader.Close()
if IsLoggedIn() {
ListWatching()
} else {
fmt.Println("You're not logged in")
Authenticate(loginParser())
if IsLoggedIn() {
ListWatching()
}
}
for {
line, err := reader.Readline()
if err != nil {
break
}
args := strings.Split(line, " ")
if len(args) == 0 {
continue
}
cmd := args[0]
switch cmd {
case "list":
ListWatching()
case "history", "record":
ListCompleted()
case "bump":
id, count, err := bumpParser(args)
if err != nil {
fmt.Println(err)
continue
}
UpdateEpisode(id, count)
case "search":
searchTerm := args[:]
SearchTitle(strings.Join(searchTerm[1:], " "))
case "add":
id, err := strconv.ParseInt(args[1], 10, 0)
if err != nil {
fmt.Println("id must be number")
continue
}
AddTitle(int(id))
case "delete", "remove":
id, err := strconv.ParseInt(args[1], 10, 0)
if err != nil {
fmt.Println("id must be number")
continue
}
RemoveTitle(int(id))
case "rate":
id, err := strconv.ParseInt(args[1], 10, 0)
if err != nil {
fmt.Println("id must be number")
continue
}
rating, err := strconv.ParseFloat(args[2], 32)
if err != nil || !validRating(rating) {
fmt.Println("rate must be 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5")
continue
}
RateTitle(int(id), float32(rating))
case "finish":
id, err := strconv.ParseInt(args[1], 10, 0)
if err != nil {
fmt.Println("id must be number")
continue
}
FinishTitle(int(id))
case "info":
Info()
case "quit", "exit":
os.Exit(0)
case "version":
Version()
default:
}
}
}
func bumpParser(args []string) (int, int, error) {
id, err := strconv.ParseInt(args[1], 10, 0)
if err != nil {
return -1, -1, errors.New("id must be number")
}
var count int64 = 1
if len(args) >= 3 {
count, err = strconv.ParseInt(args[2], 10, 0)
if err != nil {
return -1, -1, errors.New("number of episodes must be number (doh)")
}
}
return int(id), int(count), nil
}
func loginParser() (string, string) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("username: ")
username, _ := reader.ReadString('\n')
fmt.Print("Password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
password := string(bytePassword)
fmt.Println()
return strings.TrimSpace(username), strings.TrimSpace(password)
}
func validRating(rating float64) bool {
return math.Mod(rating, 0.5) == 0
}