-
Notifications
You must be signed in to change notification settings - Fork 69
refactor: validate and parse the endpoint and proxy at program load #1267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
50efd9a
7cf81b8
f004487
2d77f7b
921b34b
c45082e
26a0b64
4bfcd6f
39be826
f42f16a
ed31465
97c1b0a
e62a619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,20 +54,23 @@ Examples: | |
| if err := flagSet.Parse(args); err != nil { | ||
| return err | ||
| } | ||
| endpoint := cfg.Endpoint | ||
|
|
||
| if flagSet.NArg() >= 1 { | ||
| endpoint = flagSet.Arg(0) | ||
| } | ||
| if endpoint == "" { | ||
| return cmderrors.Usage("expected exactly one argument: the Sourcegraph URL, or SRC_ENDPOINT to be set") | ||
| arg := flagSet.Arg(0) | ||
| parsed, err := parseEndpoint(arg) | ||
| if err != nil { | ||
| return cmderrors.Usage(fmt.Sprintf("invalid endpoint URL: %s", arg)) | ||
| } | ||
| if parsed.String() != cfg.endpointURL.String() { | ||
| return cmderrors.Usage(fmt.Sprintf("The configured endpoint is %s, not %s", cfg.endpointURL, parsed)) | ||
| } | ||
|
Comment on lines
+64
to
+66
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existence of this check calls into question the command-line parameter - [1] no pun intended, but I do like it
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's reasonable to keep backwards maintainability in this PR, but follow up with one to make the breaking changes. That makes it easier to isolate and revert if necessary.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you are changing the behaviour here. The old behaviour ignored SRC_ENDPOINT for login if the argument was set. The old error message was misleading Edit: I see in the loginCmd function they checked if cfg.Endpoint is the same. I imagine that was a mistake. I agree with you about how this should work. But for now lets keep the same behaviour.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For illustration, here's how it works currently (before these changes): And here's how it works after these changes: |
||
| } | ||
|
|
||
| client := cfg.apiClient(apiFlags, io.Discard) | ||
|
|
||
| return loginCmd(context.Background(), loginParams{ | ||
| cfg: cfg, | ||
| client: client, | ||
| endpoint: endpoint, | ||
| out: os.Stdout, | ||
| useOAuth: *useOAuth, | ||
| apiFlags: apiFlags, | ||
|
|
@@ -85,7 +88,6 @@ Examples: | |
| type loginParams struct { | ||
| cfg *config | ||
| client api.Client | ||
| endpoint string | ||
| out io.Writer | ||
| useOAuth bool | ||
| apiFlags *api.Flags | ||
|
|
@@ -99,16 +101,15 @@ type loginFlowKind int | |
| const ( | ||
| loginFlowOAuth loginFlowKind = iota | ||
| loginFlowMissingAuth | ||
| loginFlowEndpointConflict | ||
| loginFlowValidate | ||
| ) | ||
|
|
||
| var loadStoredOAuthToken = oauth.LoadToken | ||
|
|
||
| func loginCmd(ctx context.Context, p loginParams) error { | ||
| if p.cfg.ConfigFilePath != "" { | ||
| if p.cfg.configFilePath != "" { | ||
| fmt.Fprintln(p.out) | ||
| fmt.Fprintf(p.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", p.cfg.ConfigFilePath) | ||
| fmt.Fprintf(p.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", p.cfg.configFilePath) | ||
| } | ||
|
|
||
| _, flow := selectLoginFlow(ctx, p) | ||
|
|
@@ -117,28 +118,23 @@ func loginCmd(ctx context.Context, p loginParams) error { | |
|
|
||
| // selectLoginFlow decides what login flow to run based on flags and config. | ||
| func selectLoginFlow(ctx context.Context, p loginParams) (loginFlowKind, loginFlow) { | ||
| endpointArg := cleanEndpoint(p.endpoint) | ||
|
|
||
| if p.useOAuth { | ||
| return loginFlowOAuth, runOAuthLogin | ||
| } | ||
| if !hasEffectiveAuth(ctx, p.cfg, endpointArg) { | ||
| if !hasEffectiveAuth(ctx, p.cfg) { | ||
| return loginFlowMissingAuth, runMissingAuthLogin | ||
| } | ||
| if endpointArg != p.cfg.Endpoint { | ||
| return loginFlowEndpointConflict, runEndpointConflictLogin | ||
| } | ||
| return loginFlowValidate, runValidatedLogin | ||
| } | ||
|
|
||
| // hasEffectiveAuth determines whether we have auth credentials to continue. It first checks for a resolved Access Token in | ||
| // config, then it checks for a stored OAuth token. | ||
| func hasEffectiveAuth(ctx context.Context, cfg *config, resolvedEndpoint string) bool { | ||
| if cfg.AccessToken != "" { | ||
| func hasEffectiveAuth(ctx context.Context, cfg *config) bool { | ||
| if cfg.accessToken != "" { | ||
| return true | ||
| } | ||
|
|
||
| if _, err := loadStoredOAuthToken(ctx, resolvedEndpoint); err == nil { | ||
| if _, err := loadStoredOAuthToken(ctx, cfg.endpointURL.String()); err == nil { | ||
| return true | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch to using JoinPath?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe? It is a pretty complex concatenation. Here are a few options for comparison:
Current:
Proposal A:
Proposal B:
I think proposal B is the easiest to read, but also it's nearly the same as the original
fmt.Sprintf.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to use proposal A unless someone has a strong opinion otherwise.