-
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
1738d6e
a6113fb
fb49d7a
5c98127
2cd44d0
e2b8d62
23d6f0c
b7faaa3
a1620bd
2475734
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 |
|---|---|---|
|
|
@@ -44,17 +44,21 @@ 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("endpoint argument %s conflicts with configured endpoint %s", parsed, cfg.endpointURL)) | ||
| } | ||
|
Comment on lines
+54
to
+56
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 |
||
| } | ||
|
|
||
| client := cfg.apiClient(apiFlags, io.Discard) | ||
|
|
||
| return loginCmd(context.Background(), cfg, client, endpoint, os.Stdout) | ||
| return loginCmd(context.Background(), cfg, client, os.Stdout) | ||
| } | ||
|
|
||
| commands = append(commands, &command{ | ||
|
|
@@ -64,9 +68,7 @@ Examples: | |
| }) | ||
| } | ||
|
|
||
| func loginCmd(ctx context.Context, cfg *config, client api.Client, endpointArg string, out io.Writer) error { | ||
| endpointArg = cleanEndpoint(endpointArg) | ||
|
|
||
| func loginCmd(ctx context.Context, cfg *config, client api.Client, out io.Writer) error { | ||
| printProblem := func(problem string) { | ||
| fmt.Fprintf(out, "❌ Problem: %s\n", problem) | ||
| } | ||
|
|
@@ -77,23 +79,16 @@ func loginCmd(ctx context.Context, cfg *config, client api.Client, endpointArg s | |
| export SRC_ACCESS_TOKEN=(your access token) | ||
| To verify that it's working, run the login command again. | ||
| `, endpointArg, endpointArg) | ||
| `, cfg.endpointURL, cfg.endpointURL) | ||
|
|
||
| if cfg.ConfigFilePath != "" { | ||
| 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) | ||
|
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 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! |
||
| } | ||
|
|
||
| noToken := cfg.AccessToken == "" | ||
| endpointConflict := endpointArg != cfg.Endpoint | ||
| if noToken || endpointConflict { | ||
| if cfg.accessToken == "" { | ||
| fmt.Fprintln(out) | ||
| switch { | ||
| case noToken: | ||
| printProblem("No access token is configured.") | ||
| case endpointConflict: | ||
| printProblem(fmt.Sprintf("The configured endpoint is %s, not %s.", cfg.Endpoint, endpointArg)) | ||
| } | ||
| printProblem("No access token is configured.") | ||
| fmt.Fprintln(out, createAccessTokenMessage) | ||
| return cmderrors.ExitCode1 | ||
| } | ||
|
|
@@ -107,7 +102,7 @@ func loginCmd(ctx context.Context, cfg *config, client api.Client, endpointArg s | |
| if strings.HasPrefix(err.Error(), "error: 401 Unauthorized") || strings.HasPrefix(err.Error(), "error: 403 Forbidden") { | ||
| printProblem("Invalid access token.") | ||
| } else { | ||
| printProblem(fmt.Sprintf("Error communicating with %s: %s", endpointArg, err)) | ||
| printProblem(fmt.Sprintf("Error communicating with %s: %s", cfg.endpointURL, err)) | ||
| } | ||
| fmt.Fprintln(out, createAccessTokenMessage) | ||
| fmt.Fprintln(out, " (If you need to supply custom HTTP request headers, see information about SRC_HEADER_* and SRC_HEADERS env vars at https://github.com/sourcegraph/src-cli/blob/main/AUTH_PROXY.md)") | ||
|
|
@@ -117,11 +112,11 @@ func loginCmd(ctx context.Context, cfg *config, client api.Client, endpointArg s | |
| if result.CurrentUser == nil { | ||
| // This should never happen; we verified there is an access token, so there should always be | ||
| // a user. | ||
| printProblem(fmt.Sprintf("Unable to determine user on %s.", endpointArg)) | ||
| printProblem(fmt.Sprintf("Unable to determine user on %s.", cfg.endpointURL)) | ||
| return cmderrors.ExitCode1 | ||
| } | ||
| fmt.Fprintln(out) | ||
| fmt.Fprintf(out, "✔️ Authenticated as %s on %s\n", result.CurrentUser.Username, endpointArg) | ||
| fmt.Fprintf(out, "✔️ Authenticated as %s on %s\n", result.CurrentUser.Username, cfg.endpointURL) | ||
| fmt.Fprintln(out) | ||
| return nil | ||
| } | ||
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.
coy by dereference makes a shallow copy - affects User mostly/only - should we include a comment to that effect here?