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.
cmd/src/batch_remote.go
Outdated
| executionURL := fmt.Sprintf( | ||
| "%s/%s/batch-changes/%s/executions/%s", | ||
| strings.TrimSuffix(cfg.Endpoint, "/"), | ||
| cfg.endpointURL, |
There was a problem hiding this comment.
Maybe? It is a pretty complex concatenation. Here are a few options for comparison:
Current:
executionURL := fmt.Sprintf(
"%s/%s/batch-changes/%s/executions/%s",
cfg.endpointURL,
strings.TrimPrefix(namespace.URL, "/"),
batchChangeName,
batchSpecID,
)
Proposal A:
executionURL := cfg.endpointURL.JoinPath(
fmt.Sprintf(
"%s/batch-changes/%s/executions/%s",
namespace.URL,
batchChangeName,
batchSpecID,
),
).String()
Proposal B:
executionURL := endpointURL.
JoinPath(namespace.URL).
JoinPath("batch-changes").
JoinPath(batchChangeName).
JoinPath("executions").
JoinPath(batchSpecID).
String()
I think proposal A is the easiest to read, but also it's nearly the same as the original fmt.Sprintf.
There was a problem hiding this comment.
I'm going to use proposal A unless someone has a strong opinion otherwise.
keegancsmith
left a comment
There was a problem hiding this comment.
approving, but agree with Michael's catch on the breaking change.
- config struct: unexport fields, replace Endpoint/Proxy strings with endpointURL/proxyURL (*url.URL) and proxyPath, parse once in readConfig - api.ClientOpts: Endpoint string → EndpointURL *url.URL - login: accept *url.URL, move endpoint conflict check to handler - code_intel_upload: simplify makeCodeIntelUploadURL with stack copy, remove error return - api.go: use JoinPath for URL construction instead of string concat - Update all call sites and tests Amp-Thread-ID: https://ampcode.com/threads/T-019cc5da-fb05-71f8-962b-689cfc5b494d Co-authored-by: Amp <amp@ampcode.com>
…tain a query string
cab95e8 to
97c1b0a
Compare
Amp-Thread-ID: https://ampcode.com/threads/T-019cd85e-222f-769f-9950-b0ed936a38dd Co-authored-by: Amp <amp@ampcode.com> # Conflicts: # cmd/src/login.go # cmd/src/login_oauth.go # cmd/src/login_test.go # cmd/src/login_validate.go # cmd/src/main.go # cmd/src/main_test.go # internal/api/api.go
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: