-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
executable file
·188 lines (185 loc) · 5.25 KB
/
cli.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"strconv"
"strings"
)
func RunCui(config *Configuration) {
end := false
for end != true {
end = CliCommand(config)
}
}
// CliCommand runs the main loop for a CLI based session
func CliCommand(config *Configuration) bool {
command := ""
_, err := fmt.Scanf("%s", &command)
if err != nil {
return false
}
command = strings.ToLower(command)
if command == "exit" {
return true
} else if command[0] == '/' {
fmt.Printf("%s is your search, sub to subscribe, exit to exit search, and \n", command[1:])
results, err := searchItunes(string(command[1:]))
if err != nil {
fmt.Printf("Error searching Itunes, %s\n", err.Error())
} else {
for {
fmt.Scanf("%s", &command)
if command == "exit" {
//exit this loop
break
} else if command == "sub" {
//sub to podcast
//load in the number
fmt.Scanf("%s", &command)
num, err := strconv.Atoi(command)
if err != nil {
fmt.Println("error converting to int")
break
}
for i, result := range results {
if i == num {
fmt.Println("appending the result to subscribed")
//add description to it
podcastAddDescription(&result)
//then add
config.Subscribed = append(config.Subscribed, result)
writeConfig(config) //update config on disk
fmt.Println("subscribed and written to disk")
goto searchEnd //considered harmful
}
}
fmt.Println("Number is in wrong format or too large, try again")
} else {
fmt.Println("Please input either exit or sub <number>")
}
}
searchEnd:
}
} else if command == "ls" {
for i, entry := range config.Subscribed {
//do nothing
fmt.Printf("%2d\t%2s\t%15s\n", i, entry.CollectionName, entry.ArtistName)
}
} else if command == "rm" {
fmt.Scanf("%s", &command)
num, err := strconv.Atoi(command)
if err != nil {
fmt.Println("please use in the form of \"rm <number>\"")
return false
}
for i := range config.Subscribed {
if i == num {
//then remove this one
fmt.Printf("Removing %s\n", config.Subscribed[i].CollectionName)
config.Subscribed = append(config.Subscribed[:i], config.Subscribed[i+1:]...)
writeConfig(config)
}
}
} else if command == "show" {
fmt.Scanf("%s", &command)
num, err := strconv.Atoi(command)
if err != nil {
fmt.Println("please use in the form of \"show <number>\"")
return false
}
found := false
for i, pc := range config.Subscribed {
if i == num {
found = true
entries, err := getPodcastEntries(pc, &config.Cached)
if err != nil {
fmt.Printf("%s when attempting to parse RSS\n", err.Error())
break
}
for i, entry := range entries {
fmt.Printf("%d Title: %s\n Summary: %s \n Content: %s\n\n", i, entry.Title, entry.Summary, entry.Content)
}
}
}
if found == false {
fmt.Printf("Num %d not found \n", num)
}
return false
} else if command == "download" {
fmt.Scanf("%s", &command)
pcNum, err := strconv.Atoi(command)
if err != nil {
fmt.Println("please use in the form of \"download <podcast number> <episode number>\"")
return false
}
fmt.Scanf("%s", &command)
epNum, err := strconv.Atoi(command)
if err != nil {
fmt.Println("please use in the form of \"download <podcast number> <episode number>\"")
return false
}
for ii, pc := range config.Subscribed {
if ii == pcNum {
entries, err := getPodcastEntries(pc, &config.Cached)
if err != nil {
fmt.Printf("%s when attempting to parse RSS\n", err.Error())
break
}
for i, entry := range entries {
if i == epNum {
err := downloadPodcast(config, pc, entry)
if err != nil {
fmt.Printf("Error when downloading: %s\n", err.Error())
} else {
fmt.Println("Finished downloading")
}
return false
}
}
fmt.Printf("Invalid episode number %d\n", epNum)
return false
}
}
fmt.Println("Invalid subscription number")
return false
} else if command == "play" {
fmt.Scanf("%s", &command)
pcNum, err := strconv.Atoi(command)
if err != nil {
fmt.Println("please use in the form of \"play <downloaded episode number>\"")
return false
}
i := 0
for _, value := range config.Downloaded {
if i == pcNum {
//send storage location to player
SetPlaying(value.StorageLocation)
sendPlayerMessage(PlayerPlay)
break
}
i++
}
fmt.Println("episode not found")
return false
} else if command == "stop" {
sendPlayerMessage(PlayerStop)
} else if command == "pause" {
sendPlayerMessage(PlayerPause)
} else if command == "resume" {
sendPlayerMessage(PlayerResume)
} else if command == "ff" {
sendPlayerMessage(PlayerFastForward)
} else if command == "rewind" {
sendPlayerMessage(PlayerRewind)
} else if command == "ls-downloaded" {
for i, podcast := range config.Downloaded {
fmt.Printf("%d %s %s\n", i, podcast.PodcastTitle, podcast.Title)
}
} else if command == "help" {
fmt.Println("Type ls to list your subscriptions, ls-downloaded to list downloads, start <num> to play, stop to stop, resume to resume, show <num> to show subscribed, /<string> to search, exit to exit, help to show this")
} else if command == "settings" {
fmt.Println(*config)
} else {
fmt.Println("Type help for a list of commands")
}
return false
}