Skip to content

Commit cb5166c

Browse files
committed
Add `-c' option argument.
Also removes `-l', allows `-s' to be omitted defaulting to `.+' and fixes a bug while displaying the list. Signed-off-by: Savio Sena <savio.sena@gmail.com>
1 parent 05a2b81 commit cb5166c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

main.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
"time"
1515
)
1616

17+
// Maximum value of an int64.
18+
const MaxInt64 = int64(^uint(0) >> 1)
19+
1720
// ListenBrainzAPI points to the root of the ListenBrainz REST API.
1821
// https://listenbrainz.readthedocs.io/en/latest/users/api
1922
const ListenBrainzAPI = "https://api.listenbrainz.org/1"
@@ -107,7 +110,7 @@ func lastTimestamp(listens []Listen) int64 {
107110
}
108111

109112
func getAllListens() []Listen {
110-
listens := make([]Listen, ItemsPerPage)
113+
var listens []Listen
111114
timestamp := int64(0)
112115
for {
113116
page := getListens(timestamp)
@@ -117,6 +120,9 @@ func getAllListens() []Listen {
117120
timestamp = lastTimestamp(page.Payload.Listens)
118121
for _, listen := range page.Payload.Listens {
119122
listens = append(listens, listen)
123+
if int64(len(listens)) >= maxCount {
124+
return listens
125+
}
120126
}
121127
}
122128
return listens
@@ -163,7 +169,7 @@ func getListens(max int64) Listens {
163169
}
164170

165171
var (
166-
listListens bool
172+
maxCount int64
167173
deleteListens bool
168174
userName string
169175
searchPattern string
@@ -172,18 +178,17 @@ var (
172178
)
173179

174180
func init() {
175-
flag.BoolVar(&listListens, "l", true, "Print matched listens.")
181+
flag.Int64Var(&maxCount, "c", MaxInt64, "Maxium number of items.")
176182
flag.BoolVar(&deleteListens, "d", false, "Delete matched listens.")
177183
flag.BoolVar(&verbosePrint, "v", false, "Debug/verbose output.")
178184
flag.StringVar(&userName, "u", "", "The user name or login ID.")
179-
flag.StringVar(&searchPattern, "s", "", "The search pattern.")
185+
flag.StringVar(&searchPattern, "s", ".+", "The search pattern.")
180186
flag.BoolVar(&showUsage, "h", false, "Show usage help.")
181187
}
182188

183189
func usage() {
184-
fmt.Printf("This program requires the environment variable LISTENBRAINZ_TOKEN to be defined.\n\n")
185-
fmt.Println("Usage: go run main.go [-ldvh] -u <username> -s <regexp>")
186-
fmt.Println(" -l: List matched listens. (default)")
190+
fmt.Println("Usage: go run main.go [-lcdvh] -u <username> -s <regexp>")
191+
fmt.Println(" -c: Limit action to a number of items.")
187192
fmt.Println(" -d: Delete matched listens.")
188193
fmt.Println(" -u: The user name or login ID.")
189194
fmt.Println(" -s: Search regexp pattern.")
@@ -201,9 +206,7 @@ func brainz() {
201206
os.Exit(1)
202207
}
203208
if match {
204-
if listListens {
205-
fmt.Println(listen)
206-
}
209+
fmt.Println(listen)
207210
if deleteListens && !deleteListen(listen) {
208211
fmt.Printf("Warning: failed deleting listen: %s", listen)
209212
}
@@ -228,8 +231,8 @@ func main() {
228231
usage()
229232
}
230233

231-
if searchPattern == "" {
232-
fmt.Println("Error: search pattern not provided.")
234+
if maxCount < 1 {
235+
fmt.Println("Error: invalid maxCount:", maxCount)
233236
usage()
234237
}
235238

0 commit comments

Comments
 (0)