-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
81 lines (69 loc) · 1.42 KB
/
peer.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"gopkg.in/urfave/cli.v1"
)
func main() {
app := cli.NewApp()
app.Name = "peer"
app.Version = "0.3.0dev"
app.Usage = "peer [--path FILEPATH] [--open N] [--reference N] SEARCH_TERMS..."
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "path, p",
Value: wd,
Usage: "Search root",
},
cli.IntFlag{
Name: "open, o",
Value: -1,
Usage: "Open one of the search results",
},
cli.BoolFlag{
Name: "print0",
Usage: "Print results seperated by a space",
},
}
app.Action = func(c *cli.Context) error {
searchTerms := c.Args()
if len(searchTerms) == 0 {
return errors.New("at least one search term must be provided")
}
roots := []string{c.String("path")}
results := search(roots, searchTerms)
if c.Int("open") != -1 {
idx := c.Int("open")
if idx > len(results) {
return errors.New("invalid index to open")
}
cmd := exec.Command("evince", results[idx-1].path)
cmd.Start()
}
if c.Bool("print0") {
names := make([]string, len(results))
for i, r := range results {
names[i] = r.path
}
fmt.Println(strings.Join(names, " "))
return nil
}
for _, result := range results {
fmt.Printf("%70s\t%.2f\n", result, result.score)
}
return nil
}
err = app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}