Skip to content

Commit 35339a2

Browse files
authored
search: add support for streaming search (#485)
This adds the flag `-stream` to the `search` sub-command. Searches submitted with `-stream` are routed to the endpoint `search/stream` instead of the GraphQL API. In addition, users can set `-display <int>` to set the display limit. We will add support for JSON in a follow-up PR.
1 parent ecd2663 commit 35339a2

9 files changed

Lines changed: 627 additions & 10 deletions

File tree

cmd/src/format.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func parseTemplate(text string) (*template.Template, error) {
6060
"addFloat": func(x, y float64) float64 {
6161
return x + y
6262
},
63+
"addInt32": func(x, y int32) int32 {
64+
return x + y
65+
},
6366
"debug": func(v interface{}) string {
6467
data, _ := marshalIndent(v)
6568
fmt.Println(string(data))
@@ -91,6 +94,13 @@ func parseTemplate(text string) (*template.Template, error) {
9194
"buildVersionHasNewSearchInterface": searchTemplateFuncs["buildVersionHasNewSearchInterface"],
9295
"renderResult": searchTemplateFuncs["renderResult"],
9396

97+
// Register stream-search specific template functions.
98+
"streamSearchSequentialLineNumber": streamSearchTemplateFuncs["streamSearchSequentialLineNumber"],
99+
"streamSearchHighlightMatch": streamSearchTemplateFuncs["streamSearchHighlightMatch"],
100+
"streamSearchHighlightCommit": streamSearchTemplateFuncs["streamSearchHighlightCommit"],
101+
"streamSearchRenderCommitLabel": streamSearchTemplateFuncs["streamSearchRenderCommitLabel"],
102+
"matchOrMatches": streamSearchTemplateFuncs["matchOrMatches"],
103+
94104
// Alert rendering
95105
"searchAlertRender": func(alert searchResultsAlert) string {
96106
if content, err := alert.Render(); err != nil {

cmd/src/search.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
isatty "github.com/mattn/go-isatty"
1919
"github.com/sourcegraph/src-cli/internal/api"
20+
"github.com/sourcegraph/src-cli/internal/streaming"
2021
"jaytaylor.com/html2text"
2122
)
2223

@@ -50,17 +51,28 @@ Other tips:
5051

5152
flagSet := flag.NewFlagSet("search", flag.ExitOnError)
5253
var (
53-
jsonFlag = flagSet.Bool("json", false, "Whether or not to output results as JSON")
54+
jsonFlag = flagSet.Bool("json", false, "Whether or not to output results as JSON.")
5455
explainJSONFlag = flagSet.Bool("explain-json", false, "Explain the JSON output schema and exit.")
5556
apiFlags = api.NewFlags(flagSet)
56-
lessFlag = flagSet.Bool("less", true, "Pipe output to 'less -R' (only if stdout is terminal, and not json flag)")
57+
lessFlag = flagSet.Bool("less", true, "Pipe output to 'less -R' (only if stdout is terminal, and not json flag).")
58+
streamFlag = flagSet.Bool("stream", false, "Consume results as stream. Streaming search only supports a subset of flags and parameters: trace, insecure-skip-verify, display.")
59+
display = flagSet.Int("display", -1, "Limit the number of results that are displayed. Only supported together with stream flag. Statistics continue to report all results.")
5760
)
5861

5962
handler := func(args []string) error {
6063
if err := flagSet.Parse(args); err != nil {
6164
return err
6265
}
6366

67+
if *streamFlag {
68+
opts := streaming.Opts{
69+
Display: *display,
70+
Trace: apiFlags.Trace(),
71+
}
72+
client := cfg.apiClient(apiFlags, flagSet.Output())
73+
return streamSearch(flagSet.Arg(0), opts, client, os.Stdout)
74+
}
75+
6476
if *explainJSONFlag {
6577
fmt.Println(searchJSONExplanation)
6678
return nil

cmd/src/search_alert.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ func init() {
1919
}
2020
}
2121

22+
// ProposedQuery is a suggested query to run when we emit an alert.
23+
type ProposedQuery struct {
24+
Description string
25+
Query string
26+
}
27+
2228
// searchResultsAlert is a type that can be used to unmarshal values returned by
2329
// the searchResultsAlertFragment GraphQL fragment below.
2430
type searchResultsAlert struct {
2531
Title string
2632
Description string
27-
ProposedQueries []struct {
28-
Description string
29-
Query string
30-
}
33+
ProposedQueries []ProposedQuery
3134
}
3235

3336
// Render renders an alert to a string ready to be output to a console,

cmd/src/search_alert_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ func TestRender(t *testing.T) {
1313
full := &searchResultsAlert{
1414
Title: "foo",
1515
Description: "bar",
16-
ProposedQueries: []struct {
17-
Description string
18-
Query string
19-
}{
16+
ProposedQueries: []ProposedQuery{
2017
{
2118
Description: "quux",
2219
Query: "xyz:abc",

0 commit comments

Comments
 (0)