@@ -8,33 +8,42 @@ 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
+
11
39
export class Commands {
12
40
public constructor (
13
41
private readonly vscodeProposed : typeof vscode ,
14
42
private readonly storage : Storage ,
15
43
) { }
16
44
17
45
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 ] )
38
47
let token : string | undefined = args . length >= 2 ? args [ 1 ] : undefined
39
48
if ( ! token ) {
40
49
const opened = await vscode . env . openExternal ( vscode . Uri . parse ( `${ url } /cli-auth` ) )
0 commit comments