refactor: validate and parse the endpoint and proxy at program load#1267
refactor: validate and parse the endpoint and proxy at program load#1267
Conversation
cmd/src/login.go
Outdated
| if cfg.configFilePath != "" { | ||
| fmt.Fprintln(out) | ||
| fmt.Fprintf(out, "⚠️ Warning: Configuring src with a JSON file is deprecated. Please migrate to using the env vars SRC_ENDPOINT, SRC_ACCESS_TOKEN, and SRC_PROXY instead, and then remove %s. See https://github.com/sourcegraph/src-cli#readme for more information.\n", cfg.ConfigFilePath) | ||
| fmt.Fprintf(out, "⚠️ Warning: Configuring src with a JSON file is deprecated. Please migrate to using the env vars SRC_ENDPOINT, SRC_ACCESS_TOKEN, and SRC_PROXY instead, and then remove %s. See https://github.com/sourcegraph/src-cli#readme for more information.\n", cfg.configFilePath) |
There was a problem hiding this comment.
The config file has been deprecated for a long time, but if it ever is finally removed, the ability to set additional headers will go also. Do we need to keep the ability to set additional headers around? If so, we should probably introduce yet another environment variable for those and delay the config file removal. If not, let's complete the config file deprecation!
There was a problem hiding this comment.
|
This change is part of the following stack: Change managed by git-spice. |
bahrmichael
left a comment
There was a problem hiding this comment.
I think there's a bug in https://github.com/sourcegraph/src-cli/pull/1267/changes#r2903861064. But apart from that good to go, so I'll approve to unblock.
keegancsmith
left a comment
There was a problem hiding this comment.
approving, but agree with Michael's catch on the breaking change.
cab95e8 to
97c1b0a
Compare
Amp-Thread-ID: https://ampcode.com/threads/T-019cdb3f-f7de-750b-b4c3-13762c7dfc11 Co-authored-by: Amp <amp@ampcode.com>
5ccd030 to
9ad25e5
Compare
| out: os.Stdout, | ||
| apiFlags: apiFlags, | ||
| oauthClient: oauth.NewClient(oauth.DefaultClientID), | ||
| loginEndpointURL: loginEndpointURL, |
There was a problem hiding this comment.
| loginEndpointURL: loginEndpointURL, | |
| endpointURL: loginEndpointURL, |
| out io.Writer | ||
| apiFlags *api.Flags | ||
| oauthClient oauth.Client | ||
| loginEndpointURL *url.URL |
There was a problem hiding this comment.
| loginEndpointURL *url.URL | |
| endpointURL *url.URL |
| proxyURL *url.URL | ||
| proxyPath string | ||
| configFilePath string | ||
| endpointURL *url.URL // always non-nil; defaults to https://sourcegraph.com via readConfig |
There was a problem hiding this comment.
if we are sure it will never be nil, then maybe we should just make it url.URL and not a pointer?
There was a problem hiding this comment.
Yeah, I considered that. I do like the signal it sends: using a value says it's a constant, do not modify, owned by the config/program, immutable, non-optional, no nil dereferencing panics.
Propagating it through the code introduces enough cognitive friction that I have hesitated to do it:
.String()usage scattered everywhere that are currently implicit via%son*url.URL- The url parse functions all deal in pointers, so there is inevitable dereferencing friction
I just don't think we gain enough from making it a value to warrant the widespread semantic awkwardness we'd have to deal with afterward.
WDYT?
| func selectLoginFlow(p loginParams) (loginFlowKind, loginFlow) { | ||
| endpointArg := cleanEndpoint(p.endpoint) | ||
|
|
||
| if p.loginEndpointURL != nil && p.loginEndpointURL.String() != p.cfg.endpointURL.String() { |
There was a problem hiding this comment.
if we just make cfg the source of truth and the cli endpoint arg just overrides it we don't have to do this check at all.
There was a problem hiding this comment.
Yes, but that would break current behavior. As confusing as the current behavior is, I was trying to keep that behavior, changing it in a followup PR.
| if err != nil { | ||
| printLoginProblem(p.out, fmt.Sprintf("OAuth Device flow authentication failed: %s", err)) | ||
| fmt.Fprintln(p.out, loginAccessTokenMessage(endpointArg)) | ||
| fmt.Fprintln(p.out, loginAccessTokenMessage(p.cfg.endpointURL)) |
There was a problem hiding this comment.
Note that we are preferring the configured endpointURL over the provided argument endpoint here
| return err | ||
| } | ||
| endpoint := cfg.Endpoint | ||
|
|
There was a problem hiding this comment.
I think after successful parse, we should override whatever is configured in cfg for the endpoint.
So cfg should be source of truth hence forth.
There was a problem hiding this comment.
Indeed it should, and after a followup PR, it will be!
| out io.Writer | ||
| apiFlags *api.Flags | ||
| oauthClient oauth.Client | ||
| loginEndpointURL *url.URL |
There was a problem hiding this comment.
We're using cfg.EndpointURL and never use this endpointURL anymore - so we can remove it
There was a problem hiding this comment.
That'll be the intent of a follow up PR
Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>
Summary
Parse the endpoint URL at program load instead of passing around an unparsed string and needing to check/parse it in multiple places.
Parse the proxy url at program load, supporting
HTTP_PROXY,HTTPS_PROXYandNO_PROXYin addition toSRC_PROXY.SRC_PROXYtakes precedence.Changes
configstruct (cmd/src/main.go)configFromFilestruct containing the JSON elementspackage main)endpointURL-EndpointandProxyare now inconfigFromFileendpointURLinreadConfig- consumers use the parsed URLapi.ClientOpts(internal/api/api.go)Endpoint string→EndpointURL *url.URLJoinPathfor URL construction instead of string concatenation withTrimRightlogincommand (cmd/src/login.go)loginCmdnow uses the config'sendpointURLloginCmdinto the handler, where the CLI arg is parsed and compared before entering the commandcode_intel_upload(cmd/src/code_intel_upload.go)makeCodeIntelUploadURLuses a stack copy (u := *cfg.endpointURL) instead of re-parsing the URLTests
*url.URLTesting
All affected packages pass: