Skip to content

Conversation

@stefanhengl
Copy link
Member

@stefanhengl stefanhengl commented Mar 4, 2021

Stacked on top of #484

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.

Screenshot 2021-03-04 at 13 43 53

We will add support for JSON in a follow-up PR.

@stefanhengl stefanhengl requested a review from keegancsmith March 4, 2021 12:27
@stefanhengl stefanhengl force-pushed the sh/support-streaming branch 2 times, most recently from 5c13ca5 to c881c3e Compare March 4, 2021 12:40
@mrnugget
Copy link
Contributor

mrnugget commented Mar 4, 2021

I'll take a deeper look, but please add a changelog for these additions :)

Copy link
Contributor

@mrnugget mrnugget left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved but left a few comments, because I think the code can be made cleaner and easier to understand/maintain.

Also: no tests? 😬

Comment on lines 43 to 46
return &Flags{
trace: flagSet.Bool("trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"),
insecureSkipVerify: flagSet.Bool("insecure-skip-verify", false, "Skip validation of TLS certificates against trusted chains"),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the duplication here? The NewFlags already returns a *Flags with both of these fields.

Edit: And wouldn't parsing fail if someone sets the get-curl flag, for example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: And wouldn't parsing fail if someone sets the get-curl flag, for example?

parsing only fails if someone calls search with -stream AND -get-curl, which is intended. -curl and -dump make a lot less sense for streaming than for the GraphQL API. I assume the use case for get-curl is to pipe the output to something like jq? The streaming endpoint does not return valid JSON, but a stream of events that contain JSON.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could make get curl work though? My original streaming client was curl :D But yeah, until we have stabalized the API properly I don't want people to start writing there own clients.

}
return streamHandler(argsWOStream)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you define -stream as a noop on the other flagset that you then use in streamHandler you wouldn't have to do this here, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but then we mess with the auto-documentation. Suddenly the "streaming search" has a "-stream" flag which is odd. I traded odd code against odd documentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not create a separate stream-search command?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is based on a discussion we had around exhaustive search which is documented in RFC 288. At some point we want phase out -stream and replace search with streaming search. That is part of the reason why I wrote it as handler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of weird though:

$ ./src search -stream -get-curl foo
flag provided but not defined: -get-curl
Usage of streaming search:
  -display int
    	Limit the number of results that are displayed. Note that the statistics continue to report all results. (default -1)
  -insecure-skip-verify
    	Skip validation of TLS certificates against trusted chains
  -trace
    	Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing

Given we intend on replacing search with stream search in the future, can't we make the stream search stuff not use its own flagset. I would think an approach like

streaming.Search(streaming.Opts{
  Query: flagSet.Args(),
  Trace: *trace,
  Display: *display,
  InsecureSkipVerify: *insecureSkipVerify,
})

would make sense. Or even you pass in the http client you use that hides the trace/insecure stuff.

That way you can also move the large amounts of business logic out of ./cmd/src into ./internal/streaming

Copy link
Member Author

@stefanhengl stefanhengl Mar 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like this behavior, but since both reviewers commented on this I may be on the wrong track here. I refactored it to be a simple function call instead of a handler and moved parts of the logic to internal/streaming.

},
OnMatches: func(matches []streaming.EventMatch) {
for _, match := range matches {
if file, ok := match.(*streaming.EventFileMatch); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a switch match := match.(type) instead of if and continue?

},
)
if err != nil {
fmt.Printf("ExecuteTemplate: %s\n", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not print all of these error messages stderr? (You can use flagSet.Output() as below)

defer resp.Body.Close()

// Process response.
err = streaming.Decoder{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd personally pull some of this out to make it more readable. All of these callbacks require only the template (and stderr, see below), so you could use closures to make it flatter:

func newDecoder(t *template.Template) streaming.Decoder {
	return streaming.Decoder{
		OnProgress: onProgress(t),
		// [...]
	}
}

func onProgress(t *template.Template) func(*streaming.Progress) {
	return func(progress *streaming.Progress) {
		// We only show the final progress.
		if !progress.Done {
			return
		}
		err = t.ExecuteTemplate(os.Stdout, "progress", progress)
		if err != nil {
			fmt.Printf("ExecuteTemplate: %s\n", err)
		}
		return
	}
}

Comment on lines 133 to 100
err = t.ExecuteTemplate(os.Stdout, "commit", struct {
SourcegraphEndpoint string
*streaming.EventCommitMatch
}{
SourcegraphEndpoint: cfg.Endpoint,
EventCommitMatch: commit,
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could clean up this render code a bit if you introduce something like a Context struct that has all of the fields defined but on which you only set the current struct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I also went back and forth about this one. I think the current approach is pretty straightforward although maybe a bit verbose. If we use a common context struct then the templates get more difficult to read. I will see if I can find something better.

@stefanhengl
Copy link
Member Author

stefanhengl commented Mar 4, 2021

Also: no tests? 😬

The PRs are stacked. You reviewed before I had the chance to push the 3rd PR with tests :-).

EDIT: Acutally ended up closing the test-PR because (#486). I rewrote the tests and pushed here.

@stefanhengl
Copy link
Member Author

@keegancsmith PTAL. Tests are now integrated in this PR.

}
return streamHandler(argsWOStream)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of weird though:

$ ./src search -stream -get-curl foo
flag provided but not defined: -get-curl
Usage of streaming search:
  -display int
    	Limit the number of results that are displayed. Note that the statistics continue to report all results. (default -1)
  -insecure-skip-verify
    	Skip validation of TLS certificates against trusted chains
  -trace
    	Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing

Given we intend on replacing search with stream search in the future, can't we make the stream search stuff not use its own flagset. I would think an approach like

streaming.Search(streaming.Opts{
  Query: flagSet.Args(),
  Trace: *trace,
  Display: *display,
  InsecureSkipVerify: *insecureSkipVerify,
})

would make sense. Or even you pass in the http client you use that hides the trace/insecure stuff.

That way you can also move the large amounts of business logic out of ./cmd/src into ./internal/streaming

Base automatically changed from sh/add-stream-client to main March 9, 2021 09:15
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.
This removes the dedicated flagSet for streaming search and move parts
of the logic to internal streaming.
@stefanhengl stefanhengl force-pushed the sh/support-streaming branch from a96cc4b to 5c951db Compare March 9, 2021 09:27
@stefanhengl stefanhengl merged commit 35339a2 into main Mar 9, 2021
@stefanhengl stefanhengl deleted the sh/support-streaming branch March 9, 2021 09:48
scjohns pushed a commit that referenced this pull request Apr 24, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants