-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsearch.go
309 lines (286 loc) · 7.26 KB
/
search.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package main
import (
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"mvdan.cc/fdroidcl/adb"
"mvdan.cc/fdroidcl/fdroid"
)
var cmdSearch = &Command{
UsageLine: "search [<regexp...>]",
Short: "Search available apps",
}
var (
searchQuiet = cmdSearch.Fset.Bool("q", false, "Print package names only")
searchInstalled = cmdSearch.Fset.Bool("i", false, "Filter installed apps")
searchUpdates = cmdSearch.Fset.Bool("u", false, "Filter apps with updates")
searchDays = cmdSearch.Fset.Int("d", 0, "Select apps last updated in the last <n> days; a negative value drops them instead")
searchCategory = cmdSearch.Fset.String("c", "", "Filter apps by category")
searchSortBy = cmdSearch.Fset.String("o", "", "Sort order (added, updated)")
searchUser = cmdSearch.Fset.String("user", "all", "Filter installed apps by user <USER_ID|current|all>")
)
func init() {
cmdSearch.Run = runSearch
}
func runSearch(args []string) error {
if *searchInstalled && *searchUpdates {
return fmt.Errorf("-i is redundant if -u is specified")
}
sfunc, err := sortFunc(*searchSortBy)
if err != nil {
return err
}
apps, err := loadIndexes()
if err != nil {
return err
}
if len(apps) > 0 && *searchCategory != "" {
apps = filterAppsCategory(apps, *searchCategory)
if apps == nil {
return fmt.Errorf("no such category: %s", *searchCategory)
}
}
if len(apps) > 0 && len(args) > 0 {
apps = filterAppsSearch(apps, args)
}
var device *adb.Device
var inst map[string]adb.Package
if *searchInstalled || *searchUpdates {
if device, err = oneDevice(); err != nil {
return err
}
if inst, err = device.Installed(); err != nil {
return err
}
}
var filterUser *int
if *searchUser != "all" && *searchUser != "current" {
n, err := strconv.Atoi(*searchUser)
if err != nil {
return fmt.Errorf("-user has to be <USER_ID|current|all>")
}
if n < 0 {
return fmt.Errorf("-user cannot have a negative number as USER_ID")
}
allUids := adb.AllUserIds(inst)
if _, exists := allUids[n]; !exists {
return fmt.Errorf("user %d does not exist", n)
}
filterUser = &n
} else {
if *installUser == "current" {
uid, err := device.CurrentUserId()
if err != nil {
return err
}
filterUser = &uid
} else {
filterUser = nil
}
}
if len(apps) > 0 && *searchInstalled {
apps = filterAppsInstalled(apps, inst, filterUser)
}
if len(apps) > 0 && *searchUpdates {
apps = filterAppsUpdates(apps, inst, device, filterUser)
}
if len(apps) > 0 && *searchDays != 0 {
apps = filterAppsLastUpdated(apps, *searchDays)
}
if sfunc != nil {
apps = sortApps(apps, sfunc)
}
if *searchQuiet {
for _, app := range apps {
fmt.Fprintln(os.Stdout, app.PackageName)
}
} else {
printApps(apps, inst, device)
}
return nil
}
func filterAppsSearch(apps []fdroid.App, terms []string) []fdroid.App {
regexes := make([]*regexp.Regexp, len(terms))
for i, term := range terms {
regexes[i] = regexp.MustCompile(term)
}
var result []fdroid.App
for _, app := range apps {
fields := []string{
strings.ToLower(app.PackageName),
strings.ToLower(app.Name),
strings.ToLower(app.Summary),
strings.ToLower(app.Description),
}
if !appMatches(fields, regexes) {
continue
}
result = append(result, app)
}
return result
}
func appMatches(fields []string, regexes []*regexp.Regexp) bool {
fieldLoop:
for _, field := range fields {
for _, regex := range regexes {
if !regex.MatchString(field) {
continue fieldLoop
}
}
return true
}
return false
}
func printApps(apps []fdroid.App, inst map[string]adb.Package, device *adb.Device) {
maxIDLen := 0
for _, app := range apps {
if len(app.PackageName) > maxIDLen {
maxIDLen = len(app.PackageName)
}
}
for _, app := range apps {
var pkg *adb.Package
p, e := inst[app.PackageName]
if e {
pkg = &p
}
printApp(app, maxIDLen, pkg, device)
}
}
func descVersion(app fdroid.App, inst *adb.Package, device *adb.Device) string {
if inst != nil {
suggested := app.SuggestedApk(device)
if suggested != nil && inst.VersCode < suggested.VersCode {
return fmt.Sprintf("%s (%d) -> %s (%d)", inst.VersName, inst.VersCode,
suggested.VersName, suggested.VersCode)
}
return fmt.Sprintf("%s (%d)", inst.VersName, inst.VersCode)
}
return fmt.Sprintf("%s (%d)", app.SugVersName, app.SugVersCode)
}
func printApp(app fdroid.App, IDLen int, inst *adb.Package, device *adb.Device) {
fmt.Printf("%s%s %s - %s\n", app.PackageName, strings.Repeat(" ", IDLen-len(app.PackageName)),
app.Name, descVersion(app, inst, device))
fmt.Printf(" %s\n", app.Summary)
}
func filterAppsInstalled(apps []fdroid.App, inst map[string]adb.Package, user *int) []fdroid.App {
var result []fdroid.App
for _, app := range apps {
p, e := inst[app.PackageName]
if !e || p.IsSystem {
continue
}
if user != nil {
installedForUser := false
for _, appUser := range p.InstalledForUsers {
if appUser == *user {
installedForUser = true
break
}
}
if !installedForUser {
continue
}
}
result = append(result, app)
}
return result
}
func filterAppsUpdates(apps []fdroid.App, inst map[string]adb.Package, device *adb.Device, user *int) []fdroid.App {
var result []fdroid.App
for _, app := range apps {
p, e := inst[app.PackageName]
if !e || p.IsSystem {
continue
}
if user != nil {
installedForUser := false
for _, appUser := range p.InstalledForUsers {
if appUser == *user {
installedForUser = true
break
}
}
if !installedForUser {
continue
}
}
suggested := app.SuggestedApk(device)
if suggested == nil {
continue
}
if p.VersCode >= suggested.VersCode {
continue
}
result = append(result, app)
}
return result
}
func filterAppsLastUpdated(apps []fdroid.App, days int) []fdroid.App {
var result []fdroid.App
newer := true
if days < 0 {
days = -days
newer = false
}
date := time.Now().Truncate(24*time.Hour).AddDate(0, 0, 1-days)
for _, app := range apps {
if app.Updated.Before(date) == newer {
continue
}
result = append(result, app)
}
return result
}
func contains(l []string, s string) bool {
for _, s1 := range l {
if s1 == s {
return true
}
}
return false
}
func filterAppsCategory(apps []fdroid.App, categ string) []fdroid.App {
var result []fdroid.App
for _, app := range apps {
if !contains(app.Categories, categ) {
continue
}
result = append(result, app)
}
return result
}
func cmpAdded(a, b *fdroid.App) bool {
return a.Added.Before(b.Added.Time)
}
func cmpUpdated(a, b *fdroid.App) bool {
return a.Updated.Before(b.Updated.Time)
}
func sortFunc(sortBy string) (func(a, b *fdroid.App) bool, error) {
switch sortBy {
case "added":
return cmpAdded, nil
case "updated":
return cmpUpdated, nil
case "":
return nil, nil
}
return nil, fmt.Errorf("unknown sort order: %s", sortBy)
}
type appList struct {
l []fdroid.App
f func(a, b *fdroid.App) bool
}
func (al appList) Len() int { return len(al.l) }
func (al appList) Swap(i, j int) { al.l[i], al.l[j] = al.l[j], al.l[i] }
func (al appList) Less(i, j int) bool { return al.f(&al.l[i], &al.l[j]) }
func sortApps(apps []fdroid.App, f func(a, b *fdroid.App) bool) []fdroid.App {
sort.Sort(appList{l: apps, f: f})
return apps
}