@@ -26,11 +26,12 @@ open class LinkHandler(
26
26
* Throw if required arguments are not supplied or the workspace is not in a
27
27
* connectable state.
28
28
*/
29
- fun handle (
29
+ suspend fun handle (
30
30
parameters : Map <String , String >,
31
31
indicator : ((t: String ) -> Unit )? = null,
32
32
): String {
33
- val deploymentURL = parameters.url() ? : dialogUi.ask(" Deployment URL" , " Enter the full URL of your Coder deployment" )
33
+ val deploymentURL =
34
+ parameters.url() ? : dialogUi.ask(" Deployment URL" , " Enter the full URL of your Coder deployment" )
34
35
if (deploymentURL.isNullOrBlank()) {
35
36
throw MissingArgumentException (" Query parameter \" $URL \" is missing" )
36
37
}
@@ -44,11 +45,12 @@ open class LinkHandler(
44
45
val client = try {
45
46
authenticate(deploymentURL, queryToken)
46
47
} catch (ex: MissingArgumentException ) {
47
- throw MissingArgumentException (" Query parameter \" $TOKEN \" is missing" )
48
+ throw MissingArgumentException (" Query parameter \" $TOKEN \" is missing" , ex )
48
49
}
49
50
50
51
// TODO: Show a dropdown and ask for the workspace if missing.
51
- val workspaceName = parameters.workspace() ? : throw MissingArgumentException (" Query parameter \" $WORKSPACE \" is missing" )
52
+ val workspaceName =
53
+ parameters.workspace() ? : throw MissingArgumentException (" Query parameter \" $WORKSPACE \" is missing" )
52
54
53
55
val workspaces = client.workspaces()
54
56
val workspace =
@@ -60,19 +62,28 @@ open class LinkHandler(
60
62
WorkspaceStatus .PENDING , WorkspaceStatus .STARTING ->
61
63
// TODO: Wait for the workspace to turn on.
62
64
throw IllegalArgumentException (
63
- " The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please wait then try again" ,
65
+ " The workspace \" $workspaceName \" is ${
66
+ workspace.latestBuild.status.toString().lowercase()
67
+ } ; please wait then try again" ,
64
68
)
69
+
65
70
WorkspaceStatus .STOPPING , WorkspaceStatus .STOPPED ,
66
71
WorkspaceStatus .CANCELING , WorkspaceStatus .CANCELED ,
67
- ->
72
+ ->
68
73
// TODO: Turn on the workspace.
69
74
throw IllegalArgumentException (
70
- " The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please start the workspace and try again" ,
75
+ " The workspace \" $workspaceName \" is ${
76
+ workspace.latestBuild.status.toString().lowercase()
77
+ } ; please start the workspace and try again" ,
71
78
)
79
+
72
80
WorkspaceStatus .FAILED , WorkspaceStatus .DELETING , WorkspaceStatus .DELETED ->
73
81
throw IllegalArgumentException (
74
- " The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; unable to connect" ,
82
+ " The workspace \" $workspaceName \" is ${
83
+ workspace.latestBuild.status.toString().lowercase()
84
+ } ; unable to connect" ,
75
85
)
86
+
76
87
WorkspaceStatus .RUNNING -> Unit // All is well
77
88
}
78
89
@@ -83,10 +94,16 @@ open class LinkHandler(
83
94
if (status.pending()) {
84
95
// TODO: Wait for the agent to be ready.
85
96
throw IllegalArgumentException (
86
- " The agent \" ${agent.name} \" has a status of \" ${status.toString().lowercase()} \" ; please wait then try again" ,
97
+ " The agent \" ${agent.name} \" has a status of \" ${
98
+ status.toString().lowercase()
99
+ } \" ; please wait then try again" ,
87
100
)
88
101
} else if (! status.ready()) {
89
- throw IllegalArgumentException (" The agent \" ${agent.name} \" has a status of \" ${status.toString().lowercase()} \" ; unable to connect" )
102
+ throw IllegalArgumentException (
103
+ " The agent \" ${agent.name} \" has a status of \" ${
104
+ status.toString().lowercase()
105
+ } \" ; unable to connect"
106
+ )
90
107
}
91
108
92
109
val cli =
@@ -120,7 +137,7 @@ open class LinkHandler(
120
137
* Throw MissingArgumentException if the user aborts. Any network or invalid
121
138
* token error may also be thrown.
122
139
*/
123
- private fun authenticate (
140
+ private suspend fun authenticate (
124
141
deploymentURL : String ,
125
142
tryToken : Pair <String , Source >? ,
126
143
error : String? = null,
@@ -172,7 +189,7 @@ open class LinkHandler(
172
189
/* *
173
190
* Check that the link is allowlisted. If not, confirm with the user.
174
191
*/
175
- private fun verifyDownloadLink (parameters : Map <String , String >) {
192
+ private suspend fun verifyDownloadLink (parameters : Map <String , String >) {
176
193
val link = parameters.ideDownloadLink()
177
194
if (link.isNullOrBlank()) {
178
195
return // Nothing to verify
@@ -233,7 +250,7 @@ private fun isAllowlisted(url: URL): Triple<Boolean, Boolean, String> {
233
250
234
251
val allowlisted =
235
252
domainAllowlist.any { url.host == it || url.host.endsWith(" .$it " ) } &&
236
- domainAllowlist.any { finalUrl.host == it || finalUrl.host.endsWith(" .$it " ) }
253
+ domainAllowlist.any { finalUrl.host == it || finalUrl.host.endsWith(" .$it " ) }
237
254
val https = url.protocol == " https" && finalUrl.protocol == " https"
238
255
return Triple (allowlisted, https, linkWithRedirect)
239
256
}
@@ -308,4 +325,4 @@ internal fun getMatchingAgent(
308
325
return agent
309
326
}
310
327
311
- class MissingArgumentException (message : String ) : IllegalArgumentException(message)
328
+ class MissingArgumentException (message : String , ex : Throwable ? = null ) : IllegalArgumentException(message, ex )
0 commit comments