Skip to content

Commit c881c3e

Browse files
committed
search: add support for streaming search
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 a989b54 commit c881c3e

File tree

6 files changed

+461
-8
lines changed

6 files changed

+461
-8
lines changed

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,29 @@ Other tips:
5454
explainJSONFlag = flagSet.Bool("explain-json", false, "Explain the JSON output schema and exit.")
5555
apiFlags = api.NewFlags(flagSet)
5656
lessFlag = flagSet.Bool("less", true, "Pipe output to 'less -R' (only if stdout is terminal, and not json flag)")
57+
streamFlag = flagSet.Bool("stream", false, "Consume results as stream.")
58+
59+
// Streaming.
60+
_ = flagSet.Int("display", -1, "Limit the number of results shown. Only supported for streaming.")
5761
)
5862

5963
handler := func(args []string) error {
6064
if err := flagSet.Parse(args); err != nil {
6165
return err
6266
}
6367

68+
if *streamFlag {
69+
// Remove -stream from args.
70+
argsWOStream := make([]string, 0, len(args)-1)
71+
for _, a := range args {
72+
if a == "-stream" {
73+
continue
74+
}
75+
argsWOStream = append(argsWOStream, a)
76+
}
77+
return streamHandler(argsWOStream)
78+
}
79+
6480
if *explainJSONFlag {
6581
fmt.Println(searchJSONExplanation)
6682
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)