@@ -8,18 +8,30 @@ import { readConfiguration } from './readConfiguration'
88let cachedAccessToken : Promise < string > | undefined
99
1010export function accessTokenSetting ( ) : Promise < string > {
11+ const fromSettings = readConfiguration ( ) . get < string > ( 'accessToken' , '' )
12+ if ( fromSettings ) {
13+ return Promise . resolve ( fromSettings )
14+ }
15+
1116 const environmentVariable = process . env . SRC_ACCESS_TOKEN
1217 if ( environmentVariable ) {
1318 return Promise . resolve ( environmentVariable )
1419 }
1520
16- const fromSettings = readConfiguration ( ) . get < string > ( 'accessToken' , '' )
17- if ( fromSettings ) {
18- return Promise . resolve ( fromSettings )
21+ return promptUserForAccessTokenSetting ( )
22+ }
23+
24+ export async function deleteAccessTokenSetting ( tokenValueToDelete : string ) : Promise < void > {
25+ const currentValue = readConfiguration ( ) . get < string > ( 'accessToken' )
26+ if ( currentValue === tokenValueToDelete ) {
27+ cachedAccessToken = undefined
28+ await readConfiguration ( ) . update ( 'accessToken' , undefined )
1929 }
30+ }
2031
32+ export async function promptUserForAccessTokenSetting ( title ?: string ) : Promise < string > {
2133 if ( ! cachedAccessToken ) {
22- cachedAccessToken = askUserToCreateAccessToken ( )
34+ cachedAccessToken = unconditionallyPromptUserForAccessTokenSetting ( title )
2335 cachedAccessToken . then (
2436 ( ) => { } ,
2537 error => {
@@ -30,36 +42,42 @@ export function accessTokenSetting(): Promise<string> {
3042 }
3143 return cachedAccessToken
3244}
33-
34- async function askUserToCreateAccessToken ( ) : Promise < string > {
45+ async function unconditionallyPromptUserForAccessTokenSetting ( title ?: string ) : Promise < string > {
3546 const openBrowserMessage = 'Open browser to create an access token'
3647 const learnMore = 'Learn more about access tokens'
48+ const pasteAccessToken = 'Paste existing access token'
3749 const userChoice = await vscode . window . showErrorMessage (
38- 'Missing Sourcegraph Access Token' ,
50+ title || 'Missing Sourcegraph Access Token' ,
3951 {
4052 modal : true ,
4153 detail : 'An access token is required to use the Sourcegraph extension. To fix this problem, create a new access token on the Sourcegraph website or set the $SRC_ACCESS_TOKEN environment variable and restart VS Code.' ,
4254 } ,
4355 openBrowserMessage ,
44- learnMore
56+ learnMore ,
57+ pasteAccessToken
4558 )
46- const openUrl =
47- userChoice === openBrowserMessage
48- ? `${ endpointSetting ( ) } /user/settings/tokens`
49- : userChoice === learnMore
50- ? 'https://docs.sourcegraph.com/cli/how-tos/creating_an_access_token'
51- : undefined
52- if ( openUrl ) {
53- await open ( openUrl )
59+ if ( userChoice ) {
60+ const openUrl =
61+ userChoice === openBrowserMessage
62+ ? `${ endpointSetting ( ) } /user/settings/tokens`
63+ : userChoice === learnMore
64+ ? 'https://docs.sourcegraph.com/cli/how-tos/creating_an_access_token'
65+ : undefined
66+ if ( openUrl ) {
67+ await open ( openUrl )
68+ }
5469 const token = await vscode . window . showInputBox ( {
5570 title : 'Paste your Sourcegraph access token here' ,
5671 ignoreFocusOut : true ,
5772 } )
5873 if ( token ) {
5974 try {
6075 const currentUser = await currentUserQuery ( token )
61- log . appendLine ( `Logged in successfully as '${ currentUser } '` )
76+ const successMessage = `Successfully logged into Sourcegraph as user '${ currentUser } '`
77+ log . appendLine ( successMessage )
78+ await vscode . window . showInformationMessage ( successMessage )
6279 await readConfiguration ( ) . update ( 'accessToken' , token , vscode . ConfigurationTarget . Global )
80+ cachedAccessToken = undefined
6381 return token
6482 } catch {
6583 await vscode . window . showErrorMessage (
@@ -74,3 +92,7 @@ async function askUserToCreateAccessToken(): Promise<string> {
7492 }
7593 throw new Error ( 'No access token' )
7694}
95+
96+ export async function updateAccessTokenSetting ( newValue ?: string ) : Promise < void > {
97+ await readConfiguration ( ) . update ( 'accessToken' , newValue )
98+ }
0 commit comments