Skip to content

Commit 64897ef

Browse files
committed
inform user of oauth login
1 parent 239125f commit 64897ef

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

cmd/src/login.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ func loginCmd(ctx context.Context, p loginParams) error {
112112
export SRC_ACCESS_TOKEN=(your access token)
113113
114114
To verify that it's working, run the login command again.
115-
`, endpointArg, endpointArg)
115+
116+
Alternatively, you can try logging in using OAuth by running: src login --oauth %s
117+
`, endpointArg, endpointArg, endpointArg)
116118

117119
if cfg.ConfigFilePath != "" {
118120
fmt.Fprintln(out)
@@ -121,8 +123,17 @@ func loginCmd(ctx context.Context, p loginParams) error {
121123

122124
noToken := cfg.AccessToken == ""
123125
endpointConflict := endpointArg != cfg.Endpoint
124-
125-
cfg.Endpoint = endpointArg
126+
if !p.useOAuth && (noToken || endpointConflict) {
127+
fmt.Fprintln(out)
128+
switch {
129+
case noToken:
130+
printProblem("No access token is configured.")
131+
case endpointConflict:
132+
printProblem(fmt.Sprintf("The configured endpoint is %s, not %s.", cfg.Endpoint, endpointArg))
133+
}
134+
fmt.Fprintln(out, createAccessTokenMessage)
135+
return cmderrors.ExitCode1
136+
}
126137

127138
if p.useOAuth {
128139
token, err := runOAuthDeviceFlow(ctx, endpointArg, out, p.deviceFlowClient)
@@ -146,16 +157,6 @@ func loginCmd(ctx context.Context, p loginParams) error {
146157
ProxyPath: cfg.ProxyPath,
147158
OAuthToken: token,
148159
})
149-
} else if noToken || endpointConflict {
150-
fmt.Fprintln(out)
151-
switch {
152-
case noToken:
153-
printProblem("No access token is configured.")
154-
case endpointConflict:
155-
printProblem(fmt.Sprintf("The configured endpoint is %s, not %s.", cfg.Endpoint, endpointArg))
156-
}
157-
fmt.Fprintln(out, createAccessTokenMessage)
158-
return cmderrors.ExitCode1
159160
}
160161

161162
// See if the user is already authenticated.

cmd/src/login_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestLogin(t *testing.T) {
3434
if err != cmderrors.ExitCode1 {
3535
t.Fatal(err)
3636
}
37-
wantOut := "❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token by going to https://sourcegraph.example.com/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=https://sourcegraph.example.com\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again."
37+
wantOut := "❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token by going to https://sourcegraph.example.com/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=https://sourcegraph.example.com\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again.\n\n Alternatively, you can try logging in using OAuth by running: src login --oauth https://sourcegraph.example.com"
3838
if out != wantOut {
3939
t.Errorf("got output %q, want %q", out, wantOut)
4040
}
@@ -45,7 +45,7 @@ func TestLogin(t *testing.T) {
4545
if err != cmderrors.ExitCode1 {
4646
t.Fatal(err)
4747
}
48-
wantOut := "❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token by going to https://sourcegraph.example.com/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=https://sourcegraph.example.com\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again."
48+
wantOut := "❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token by going to https://sourcegraph.example.com/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=https://sourcegraph.example.com\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again.\n\n Alternatively, you can try logging in using OAuth by running: src login --oauth https://sourcegraph.example.com"
4949
if out != wantOut {
5050
t.Errorf("got output %q, want %q", out, wantOut)
5151
}
@@ -56,7 +56,7 @@ func TestLogin(t *testing.T) {
5656
if err != cmderrors.ExitCode1 {
5757
t.Fatal(err)
5858
}
59-
wantOut := "⚠️ 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 f. See https://github.com/sourcegraph/src-cli#readme for more information.\n\n❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token by going to https://example.com/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=https://example.com\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again."
59+
wantOut := "⚠️ 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 f. See https://github.com/sourcegraph/src-cli#readme for more information.\n\n❌ Problem: No access token is configured.\n\n🛠 To fix: Create an access token by going to https://example.com/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=https://example.com\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again.\n\n Alternatively, you can try logging in using OAuth by running: src login --oauth https://example.com"
6060
if out != wantOut {
6161
t.Errorf("got output %q, want %q", out, wantOut)
6262
}
@@ -74,7 +74,7 @@ func TestLogin(t *testing.T) {
7474
if err != cmderrors.ExitCode1 {
7575
t.Fatal(err)
7676
}
77-
wantOut := "❌ Problem: Invalid access token.\n\n🛠 To fix: Create an access token by going to $ENDPOINT/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=$ENDPOINT\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again.\n\n (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)"
77+
wantOut := "❌ Problem: Invalid access token.\n\n🛠 To fix: Create an access token by going to $ENDPOINT/user/settings/tokens, then set the following environment variables in your terminal:\n\n export SRC_ENDPOINT=$ENDPOINT\n export SRC_ACCESS_TOKEN=(your access token)\n\n To verify that it's working, run the login command again.\n\n Alternatively, you can try logging in using OAuth by running: src login --oauth $ENDPOINT\n\n (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)"
7878
wantOut = strings.ReplaceAll(wantOut, "$ENDPOINT", endpoint)
7979
if out != wantOut {
8080
t.Errorf("got output %q, want %q", out, wantOut)

0 commit comments

Comments
 (0)