-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.go
More file actions
48 lines (39 loc) · 1.02 KB
/
Copy pathsearch.go
File metadata and controls
48 lines (39 loc) · 1.02 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
package main
import (
"context"
"fmt"
"io"
"log"
"github.com/sourcegraph/zoekt"
"github.com/sourcegraph/zoekt/query"
"github.com/sourcegraph/zoekt/search"
)
type SearchOpts struct {
IndexDir string
MaxResults int
ContextLines int
}
func runSearch(pattern string, opts SearchOpts) ([]zoekt.FileMatch, error) {
// Suppress zoekt's internal logging (shard loading info)
log.SetOutput(io.Discard)
searcher, err := search.NewDirectorySearcher(opts.IndexDir)
if err != nil {
return nil, fmt.Errorf("opening index at %s: %w", opts.IndexDir, err)
}
defer searcher.Close()
q, err := query.Parse(pattern)
if err != nil {
return nil, fmt.Errorf("parsing query: %w", err)
}
q = query.Map(q, query.ExpandFileContent)
q = query.Simplify(q)
sOpts := &zoekt.SearchOptions{
MaxDocDisplayCount: opts.MaxResults,
NumContextLines: opts.ContextLines,
}
result, err := searcher.Search(context.Background(), q, sOpts)
if err != nil {
return nil, fmt.Errorf("search failed: %w", err)
}
return result.Files, nil
}