@@ -8,42 +8,81 @@ import { Remote } from "./remote"
88import { Storage } from "./storage"
99import { OpenableTreeItem } from "./workspacesProvider"
1010
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-
3911export class Commands {
4012 public constructor (
4113 private readonly vscodeProposed : typeof vscode ,
4214 private readonly storage : Storage ,
4315 ) { }
4416
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 async 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+ const selected = await new Promise < string | undefined > ( ( resolve ) => {
46+ quickPick . onDidHide ( ( ) => resolve ( undefined ) )
47+ quickPick . onDidChangeSelection ( ( selected ) => resolve ( selected [ 0 ] ?. label ) )
48+ } )
49+ quickPick . dispose ( )
50+ return selected
51+ }
52+
53+ /**
54+ * Ask the user for the URL if it was not provided, letting them choose from a
55+ * list of recent URLs or CODER_URL or enter a new one, and normalizes the
56+ * returned URL. Undefined means the user aborted.
57+ */
58+ public async maybeAskUrl ( providedUrl : string | undefined | null , lastUsedUrl ?: string ) : Promise < string | undefined > {
59+ let url = providedUrl || ( await this . askURL ( lastUsedUrl ) )
60+ if ( ! url ) {
61+ // User aborted.
62+ return undefined
63+ }
64+
65+ // Normalize URL.
66+ if ( ! url . startsWith ( "http://" ) && ! url . startsWith ( "https://" ) ) {
67+ // Default to HTTPS if not provided so URLs can be typed more easily.
68+ url = "https://" + url
69+ }
70+ while ( url . endsWith ( "/" ) ) {
71+ url = url . substring ( 0 , url . length - 1 )
72+ }
73+ return url
74+ }
75+
76+ /**
77+ * Log into the provided deployment. If the deployment URL is not specified,
78+ * ask for it first with a menu showing recent URLs and CODER_URL, if set.
79+ */
4580 public async login ( ...args : string [ ] ) : Promise < void > {
46- const url = await maybeAskUrl ( args [ 0 ] )
81+ const url = await this . maybeAskUrl ( args [ 0 ] )
82+ if ( ! url ) {
83+ return
84+ }
85+
4786 let token : string | undefined = args . length >= 2 ? args [ 1 ] : undefined
4887 if ( ! token ) {
4988 const opened = await vscode . env . openExternal ( vscode . Uri . parse ( `${ url } /cli-auth` ) )
0 commit comments