-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd_search.go
53 lines (44 loc) · 950 Bytes
/
cmd_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
package main
import (
"fmt"
"github.com/rubiojr/rindex"
"github.com/urfave/cli/v2"
)
func init() {
cmd := &cli.Command{
Name: "search",
Usage: "Search the index",
Action: doSearch,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Enable verbose output",
Required: false,
},
},
}
appCommands = append(appCommands, cmd)
}
func doSearch(c *cli.Context) error {
initApp()
q := c.Args().Get(0)
verbose := c.Bool("verbose")
idx, err := rindex.New(indexPath, globalOptions.Repo, globalOptions.Password)
if err != nil {
return err
}
fmt.Printf("Searching for %s...\n", q)
count, err := idx.Search(q, func(field string, value []byte) bool {
if filterField(field) && !verbose {
return true
}
printMetadata(field, value, headerColor)
return true
}, func() bool {
fmt.Println()
return true
})
fmt.Printf("Results: %d\n", count)
return err
}