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
- 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>
| } | ||
|
|
||
| func makeCodeIntelUploadURL(uploadID int) string { | ||
| u := *cfg.endpointURL |
There was a problem hiding this comment.
coy by dereference makes a shallow copy - affects User mostly/only - should we include a comment to that effect here?
| if parsed.String() != cfg.endpointURL.String() { | ||
| return cmderrors.Usage(fmt.Sprintf("endpoint argument %s conflicts with configured endpoint %s", parsed, cfg.endpointURL)) | ||
| } |
There was a problem hiding this comment.
The existence of this check calls into question the command-line parameter - SRC_ENDPOINT (or the value from the config file) is the source[1] of truth, forcing the user to put the same value on the command line.
Adding to the confusion is that if there is nothing in the config file or in SRC_ENDPOINT, cfg.endpointURL defaults to https://sourcegraph.com, which is probably wrong pretty much all of the time, and still invalidates anything different on the command line.
I kept the error condition and message (although I did change the semantics by changing from an exit error to a usage error) because I didn't want to rip that rug out from under anyone at this point.
Thoughts?
[1] no pun intended, but I do like it
| 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!
Summary
Parse the endpoint URL at program load instead of passing around an unparsed string and needing to check/parse it in multiple places.
Changes
configstruct (cmd/src/main.go)package main)endpointURL, removeendpointandproxyreadConfig; consumers use the parsed URLsapi.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.URL"net/url"imports where neededTesting
All affected packages pass: