-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
130 lines (112 loc) · 3.24 KB
/
Copy pathmain.go
File metadata and controls
130 lines (112 loc) · 3.24 KB
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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
const version = "0.2.0"
func main() {
if len(os.Args) < 2 {
printUsage()
os.Exit(2)
}
switch os.Args[1] {
case "add":
cmdAdd(os.Args[2:])
case "serve":
cmdServe(os.Args[2:])
case "_daemon":
cmdDaemon()
case "list":
cmdList()
case "version", "--version", "-v":
fmt.Printf("fall %s\n", version)
case "help", "--help", "-h":
printUsage()
default:
cmdSearch(os.Args[1:])
}
}
func cmdSearch(args []string) {
fs := flag.NewFlagSet("fall", flag.ExitOnError)
colors := fs.Bool("colors", false, "ANSI color highlighting (fall colors)")
jsonOut := fs.Bool("json", false, "JSONL output")
listOnly := fs.Bool("l", false, "list matching files only")
maxResults := fs.Int("n", 50, "max file results")
contextLines := fs.Int("context", 0, "context lines around matches")
indexDir := fs.String("index-dir", defaultIndexDir(), "zoekt index directory")
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: fall [flags] <query>\n\n")
fmt.Fprintf(os.Stderr, "Examples:\n")
fmt.Fprintf(os.Stderr, " fall \"functionName\" search all repos\n")
fmt.Fprintf(os.Stderr, " fall \"repo:magnus defun\" filter by repo\n")
fmt.Fprintf(os.Stderr, " fall \"file:*.go func main\" filter by file\n")
fmt.Fprintf(os.Stderr, " fall \"lang:swift class\" filter by language\n")
fmt.Fprintf(os.Stderr, " fall -l \"TODO\" list files only\n")
fmt.Fprintf(os.Stderr, " fall --colors \"error\" human-friendly output\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
fs.PrintDefaults()
}
fs.Parse(args)
if fs.NArg() == 0 {
fs.Usage()
os.Exit(2)
}
query := fs.Arg(0)
for i := 1; i < fs.NArg(); i++ {
query += " " + fs.Arg(i)
}
files, err := runSearch(query, SearchOpts{
IndexDir: *indexDir,
MaxResults: *maxResults,
ContextLines: *contextLines,
})
if err != nil {
fmt.Fprintf(os.Stderr, "fall: %v\n", err)
os.Exit(1)
}
if len(files) == 0 {
os.Exit(0)
}
w := os.Stdout
switch {
case *jsonOut:
formatJSON(w, files, *listOnly)
case *colors:
formatColor(w, files, *listOnly)
default:
formatPlain(w, files, *listOnly)
}
}
func printUsage() {
fmt.Fprintf(os.Stderr, `fall — find all
Indexed code search powered by zoekt trigram indices.
Usage:
fall <query> search indexed repos
fall add <path> ... add git repos to the index
fall list list indexed repos
fall serve <action> manage the web UI (start|stop|status|restart)
fall version print version
Search flags:
--colors fall-colored ANSI highlighting
--json JSONL output
-l list matching files only
-n <count> max results (default: 50)
--context <n> context lines (default: 0)
Query syntax:
fall "func main" literal search
fall "repo:name query" filter by repo
fall "file:*.go query" filter by file pattern
fall "lang:go query" filter by language
fall "/regex/" regular expression
Install: go install github.com/hrishikeshs/fall@latest
`)
}
func defaultIndexDir() string {
home, err := os.UserHomeDir()
if err != nil {
return filepath.Join(".", ".zoekt")
}
return filepath.Join(home, ".zoekt")
}