@@ -8,33 +8,42 @@ 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+
1139export class Commands {
1240 public constructor (
1341 private readonly vscodeProposed : typeof vscode ,
1442 private readonly storage : Storage ,
1543 ) { }
1644
1745 public async login ( ...args : string [ ] ) : Promise < void > {
18- let url : string | undefined = args . length >= 1 ? args [ 0 ] : undefined
19- if ( ! url ) {
20- url = await vscode . window . showInputBox ( {
21- title : "Coder URL" ,
22- prompt : "Enter the URL of your Coder deployment." ,
23- placeHolder : "https://example.coder.com" ,
24- value : url ,
25- } )
26- }
27- if ( ! url ) {
28- return
29- }
30- if ( ! url . startsWith ( "http://" ) && ! url . startsWith ( "https://" ) ) {
31- // Default to HTTPS if not provided!
32- // https://github.com/coder/vscode-coder/issues/44
33- url = "https://" + url
34- }
35- while ( url . endsWith ( "/" ) ) {
36- url = url . substring ( 0 , url . length - 1 )
37- }
46+ const url = await maybeAskUrl ( args [ 0 ] )
3847 let token : string | undefined = args . length >= 2 ? args [ 1 ] : undefined
3948 if ( ! token ) {
4049 const opened = await vscode . env . openExternal ( vscode . Uri . parse ( `${ url } /cli-auth` ) )
0 commit comments