@@ -8,42 +8,80 @@ import { Remote } from "./remote"
8
8
import { Storage } from "./storage"
9
9
import { OpenableTreeItem } from "./workspacesProvider"
10
10
11
- // maybeAskUrl asks the user for the URL if it was not provided and normalizes
12
- // the returned URL.
13
- export async function maybeAskUrl (
14
- providedUrl : string | undefined | null ,
15
- lastUsedUrl ?: string ,
16
- ) : Promise < string | undefined > {
17
- let url =
18
- providedUrl ||
19
- ( await vscode . window . showInputBox ( {
20
- title : "Coder URL" ,
21
- prompt : "Enter the URL of your Coder deployment." ,
22
- placeHolder : "https://example.coder.com" ,
23
- value : lastUsedUrl ,
24
- } ) )
25
- if ( ! url ) {
26
- return undefined
27
- }
28
- if ( ! url . startsWith ( "http://" ) && ! url . startsWith ( "https://" ) ) {
29
- // Default to HTTPS if not provided!
30
- // https://github.com/coder/vscode-coder/issues/44
31
- url = "https://" + url
32
- }
33
- while ( url . endsWith ( "/" ) ) {
34
- url = url . substring ( 0 , url . length - 1 )
35
- }
36
- return url
37
- }
38
-
39
11
export class Commands {
40
12
public constructor (
41
13
private readonly vscodeProposed : typeof vscode ,
42
14
private readonly storage : Storage ,
43
15
) { }
44
16
17
+ /**
18
+ * Ask the user for the URL, letting them choose from a list of recent URLs or
19
+ * CODER_URL or enter a new one. Undefined means the user aborted.
20
+ */
21
+ private askURL ( selection ?: string ) : Promise < string | undefined > {
22
+ const quickPick = vscode . window . createQuickPick ( )
23
+ quickPick . value = selection || process . env . CODER_URL || ""
24
+ quickPick . placeholder = "https://example.coder.com"
25
+ quickPick . title = "Enter the URL of your Coder deployment."
26
+
27
+ // Initial items.
28
+ quickPick . items = this . storage . withUrlHistory ( process . env . CODER_URL ) . map ( ( url ) => ( {
29
+ alwaysShow : true ,
30
+ label : url ,
31
+ } ) )
32
+
33
+ // Quick picks do not allow arbitrary values, so we add the value itself as
34
+ // an option in case the user wants to connect to something that is not in
35
+ // the list.
36
+ quickPick . onDidChangeValue ( ( value ) => {
37
+ quickPick . items = this . storage . withUrlHistory ( process . env . CODER_URL , value ) . map ( ( url ) => ( {
38
+ alwaysShow : true ,
39
+ label : url ,
40
+ } ) )
41
+ } )
42
+
43
+ quickPick . show ( )
44
+
45
+ return new Promise < string | undefined > ( ( resolve ) => {
46
+ quickPick . onDidHide ( ( ) => resolve ( undefined ) )
47
+ quickPick . onDidChangeSelection ( ( selected ) => resolve ( selected [ 0 ] ?. label ) )
48
+ } )
49
+ }
50
+
51
+ /**
52
+ * Ask the user for the URL if it was not provided, letting them choose from a
53
+ * list of recent URLs or CODER_URL or enter a new one, and normalizes the
54
+ * returned URL. Undefined means the user aborted.
55
+ */
56
+ public async maybeAskUrl ( providedUrl : string | undefined | null , lastUsedUrl ?: string ) : Promise < string | undefined > {
57
+ let url = providedUrl || ( await this . askURL ( lastUsedUrl ) )
58
+ if ( ! url ) {
59
+ // User aborted.
60
+ return undefined
61
+ }
62
+
63
+ // Normalize URL.
64
+ if ( ! url . startsWith ( "http://" ) && ! url . startsWith ( "https://" ) ) {
65
+ // Default to HTTPS if not provided!
66
+ // https://github.com/coder/vscode-coder/issues/44
67
+ url = "https://" + url
68
+ }
69
+ while ( url . endsWith ( "/" ) ) {
70
+ url = url . substring ( 0 , url . length - 1 )
71
+ }
72
+ return url
73
+ }
74
+
75
+ /**
76
+ * Log into the provided deployment. If the deployment URL is not specified,
77
+ * ask for it first with a menu showing recent URLs and CODER_URL, if set.
78
+ */
45
79
public async login ( ...args : string [ ] ) : Promise < void > {
46
- const url = await maybeAskUrl ( args [ 0 ] )
80
+ const url = await this . maybeAskUrl ( args [ 0 ] )
81
+ if ( ! url ) {
82
+ return
83
+ }
84
+
47
85
let token : string | undefined = args . length >= 2 ? args [ 1 ] : undefined
48
86
if ( ! token ) {
49
87
const opened = await vscode . env . openExternal ( vscode . Uri . parse ( `${ url } /cli-auth` ) )
0 commit comments