@@ -85,7 +85,7 @@ class CoderWorkspaceStepDialog(
8585 override fun createSouthPanel (): JComponent {
8686 // The plugin provides its own buttons.
8787 // TODO: Is it more idiomatic to handle buttons out here?
88- return panel{}.apply {
88+ return panel {}.apply {
8989 border = JBUI .Borders .empty()
9090 }
9191 }
@@ -96,12 +96,16 @@ class CoderWorkspaceStepDialog(
9696class CoderGatewayConnectionProvider : GatewayConnectionProvider {
9797 private val settings: CoderSettingsService = service<CoderSettingsService >()
9898
99- override suspend fun connect (parameters : Map <String , String >, requestor : ConnectionRequestor ): GatewayConnectionHandle ? {
100- CoderRemoteConnectionHandle ().connect{ indicator ->
99+ override suspend fun connect (
100+ parameters : Map <String , String >,
101+ requestor : ConnectionRequestor ,
102+ ): GatewayConnectionHandle ? {
103+ CoderRemoteConnectionHandle ().connect { indicator ->
101104 logger.debug(" Launched Coder connection provider" , parameters)
102105
103- val deploymentURL = parameters.url()
104- ? : CoderRemoteConnectionHandle .ask(" Enter the full URL of your Coder deployment" )
106+ val deploymentURL =
107+ parameters.url()
108+ ? : CoderRemoteConnectionHandle .ask(" Enter the full URL of your Coder deployment" )
105109 if (deploymentURL.isNullOrBlank()) {
106110 throw IllegalArgumentException (" Query parameter \" $URL \" is missing" )
107111 }
@@ -112,18 +116,28 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
112116 val workspaceName = parameters.workspace() ? : throw IllegalArgumentException (" Query parameter \" $WORKSPACE \" is missing" )
113117
114118 val workspaces = client.workspaces()
115- val workspace = workspaces.firstOrNull{ it.name == workspaceName } ? : throw IllegalArgumentException (" The workspace $workspaceName does not exist" )
119+ val workspace =
120+ workspaces.firstOrNull {
121+ it.name == workspaceName
122+ } ? : throw IllegalArgumentException (" The workspace $workspaceName does not exist" )
116123
117124 when (workspace.latestBuild.status) {
118125 WorkspaceStatus .PENDING , WorkspaceStatus .STARTING ->
119126 // TODO: Wait for the workspace to turn on.
120- throw IllegalArgumentException (" The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please wait then try again" )
127+ throw IllegalArgumentException (
128+ " The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please wait then try again" ,
129+ )
121130 WorkspaceStatus .STOPPING , WorkspaceStatus .STOPPED ,
122- WorkspaceStatus .CANCELING , WorkspaceStatus .CANCELED ->
131+ WorkspaceStatus .CANCELING , WorkspaceStatus .CANCELED ,
132+ ->
123133 // TODO: Turn on the workspace.
124- throw IllegalArgumentException (" The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please start the workspace and try again" )
134+ throw IllegalArgumentException (
135+ " The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; please start the workspace and try again" ,
136+ )
125137 WorkspaceStatus .FAILED , WorkspaceStatus .DELETING , WorkspaceStatus .DELETED ->
126- throw IllegalArgumentException (" The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; unable to connect" )
138+ throw IllegalArgumentException (
139+ " The workspace \" $workspaceName \" is ${workspace.latestBuild.status.toString().lowercase()} ; unable to connect" ,
140+ )
127141 WorkspaceStatus .RUNNING -> Unit // All is well
128142 }
129143
@@ -133,17 +147,20 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
133147
134148 if (status.pending()) {
135149 // TODO: Wait for the agent to be ready.
136- throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${status.toString().lowercase()} ; please wait then try again" )
150+ throw IllegalArgumentException (
151+ " The agent \" ${agent.name} \" is ${status.toString().lowercase()} ; please wait then try again" ,
152+ )
137153 } else if (! status.ready()) {
138154 throw IllegalArgumentException (" The agent \" ${agent.name} \" is ${status.toString().lowercase()} ; unable to connect" )
139155 }
140156
141- val cli = ensureCLI(
142- deploymentURL.toURL(),
143- client.buildInfo().version,
144- settings,
145- indicator,
146- )
157+ val cli =
158+ ensureCLI(
159+ deploymentURL.toURL(),
160+ client.buildInfo().version,
161+ settings,
162+ indicator,
163+ )
147164
148165 // We only need to log in if we are using token-based auth.
149166 if (client.token != = null ) {
@@ -155,16 +172,20 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
155172 cli.configSsh(client.agentNames(workspaces))
156173
157174 val name = " ${workspace.name} .${agent.name} "
158- val openDialog = parameters.ideProductCode().isNullOrBlank() ||
175+ val openDialog =
176+ parameters.ideProductCode().isNullOrBlank() ||
159177 parameters.ideBuildNumber().isNullOrBlank() ||
160178 (parameters.idePathOnHost().isNullOrBlank() && parameters.ideDownloadLink().isNullOrBlank()) ||
161179 parameters.folder().isNullOrBlank()
162180
163181 if (openDialog) {
164182 var data: WorkspaceProjectIDE ? = null
165183 ApplicationManager .getApplication().invokeAndWait {
166- val dialog = CoderWorkspaceStepDialog (name,
167- CoderWorkspacesStepSelection (agent, workspace, cli, client, workspaces))
184+ val dialog =
185+ CoderWorkspaceStepDialog (
186+ name,
187+ CoderWorkspacesStepSelection (agent, workspace, cli, client, workspaces),
188+ )
168189 data = dialog.showAndGetData()
169190 }
170191 data ? : throw Exception (" IDE selection aborted; unable to connect" )
@@ -193,19 +214,29 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
193214 * continues to result in an authentication failure and token authentication
194215 * is required.
195216 */
196- private fun authenticate (deploymentURL : String , queryToken : String? , lastToken : Pair <String , Source >? = null): CoderRestClient {
197- val token = if (settings.requireTokenAuth) {
198- // Use the token from the query, unless we already tried that.
199- val isRetry = lastToken != null
200- if (! queryToken.isNullOrBlank() && ! isRetry)
201- Pair (queryToken, Source .QUERY )
202- else CoderRemoteConnectionHandle .askToken(
203- deploymentURL.toURL(),
204- lastToken,
205- isRetry,
206- useExisting = true ,
207- settings)
208- } else null
217+ private fun authenticate (
218+ deploymentURL : String ,
219+ queryToken : String? ,
220+ lastToken : Pair <String , Source >? = null,
221+ ): CoderRestClient {
222+ val token =
223+ if (settings.requireTokenAuth) {
224+ // Use the token from the query, unless we already tried that.
225+ val isRetry = lastToken != null
226+ if (! queryToken.isNullOrBlank() && ! isRetry) {
227+ Pair (queryToken, Source .QUERY )
228+ } else {
229+ CoderRemoteConnectionHandle .askToken(
230+ deploymentURL.toURL(),
231+ lastToken,
232+ isRetry,
233+ useExisting = true ,
234+ settings,
235+ )
236+ }
237+ } else {
238+ null
239+ }
209240 if (settings.requireTokenAuth && token == null ) { // User aborted.
210241 throw IllegalArgumentException (" Unable to connect to $deploymentURL , query parameter \" $TOKEN \" is missing" )
211242 }
@@ -217,7 +248,9 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
217248 // If doing token auth we can ask and try again.
218249 if (settings.requireTokenAuth) {
219250 authenticate(deploymentURL, queryToken, token)
220- } else throw ex
251+ } else {
252+ throw ex
253+ }
221254 }
222255 }
223256
@@ -230,30 +263,38 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
230263 return // Nothing to verify
231264 }
232265
233- val url = try {
234- link.toURL()
235- } catch (ex: Exception ) {
236- throw IllegalArgumentException (" $link is not a valid URL" )
237- }
266+ val url =
267+ try {
268+ link.toURL()
269+ } catch (ex: Exception ) {
270+ throw IllegalArgumentException (" $link is not a valid URL" )
271+ }
238272
239- val (allowlisted, https, linkWithRedirect) = try {
240- CoderRemoteConnectionHandle .isAllowlisted(url)
241- } catch (e: Exception ) {
242- throw IllegalArgumentException (" Unable to verify $url : $e " )
243- }
273+ val (allowlisted, https, linkWithRedirect) =
274+ try {
275+ CoderRemoteConnectionHandle .isAllowlisted(url)
276+ } catch (e: Exception ) {
277+ throw IllegalArgumentException (" Unable to verify $url : $e " )
278+ }
244279 if (allowlisted && https) {
245280 return
246281 }
247282
248- val comment = if (allowlisted) " The download link is from a non-allowlisted URL"
249- else if (https) " The download link is not using HTTPS"
250- else " The download link is from a non-allowlisted URL and is not using HTTPS"
283+ val comment =
284+ if (allowlisted) {
285+ " The download link is from a non-allowlisted URL"
286+ } else if (https) {
287+ " The download link is not using HTTPS"
288+ } else {
289+ " The download link is from a non-allowlisted URL and is not using HTTPS"
290+ }
251291
252292 if (! CoderRemoteConnectionHandle .confirm(
253293 " Confirm download URL" ,
254294 " $comment . Would you like to proceed?" ,
255295 linkWithRedirect,
256- )) {
296+ )
297+ ) {
257298 throw IllegalArgumentException (" $linkWithRedirect is not allowlisted" )
258299 }
259300 }
@@ -274,28 +315,39 @@ class CoderGatewayConnectionProvider : GatewayConnectionProvider {
274315 *
275316 * @throws [MissingArgumentException, IllegalArgumentException]
276317 */
277- fun getMatchingAgent (parameters : Map <String , String ?>, workspace : Workspace ): WorkspaceAgent {
318+ fun getMatchingAgent (
319+ parameters : Map <String , String ?>,
320+ workspace : Workspace ,
321+ ): WorkspaceAgent {
278322 val agents = workspace.latestBuild.resources.filter { it.agents != null }.flatMap { it.agents!! }
279323 if (agents.isEmpty()) {
280324 throw IllegalArgumentException (" The workspace \" ${workspace.name} \" has no agents" )
281325 }
282326
283327 // If the agent is missing and the workspace has only one, use that.
284328 // Prefer the ID over the name if both are set.
285- val agent = if (! parameters.agentID().isNullOrBlank())
286- agents.firstOrNull { it.id.toString() == parameters.agentID() }
287- else if (! parameters.agentName().isNullOrBlank())
288- agents.firstOrNull { it.name == parameters.agentName()}
289- else if (agents.size == 1 ) agents.first()
290- else null
329+ val agent =
330+ if (! parameters.agentID().isNullOrBlank()) {
331+ agents.firstOrNull { it.id.toString() == parameters.agentID() }
332+ } else if (! parameters.agentName().isNullOrBlank()) {
333+ agents.firstOrNull { it.name == parameters.agentName() }
334+ } else if (agents.size == 1 ) {
335+ agents.first()
336+ } else {
337+ null
338+ }
291339
292340 if (agent == null ) {
293341 if (! parameters.agentID().isNullOrBlank()) {
294342 throw IllegalArgumentException (" The workspace \" ${workspace.name} \" does not have an agent with ID \" ${parameters.agentID()} \" " )
295- } else if (! parameters.agentName().isNullOrBlank()){
296- throw IllegalArgumentException (" The workspace \" ${workspace.name} \" does not have an agent named \" ${parameters.agentName()} \" " )
343+ } else if (! parameters.agentName().isNullOrBlank()) {
344+ throw IllegalArgumentException (
345+ " The workspace \" ${workspace.name} \" does not have an agent named \" ${parameters.agentName()} \" " ,
346+ )
297347 } else {
298- throw MissingArgumentException (" Unable to determine which agent to connect to; one of \" $AGENT_NAME \" or \" $AGENT_ID \" must be set because the workspace \" ${workspace.name} \" has more than one agent" )
348+ throw MissingArgumentException (
349+ " Unable to determine which agent to connect to; one of \" $AGENT_NAME \" or \" $AGENT_ID \" must be set because the workspace \" ${workspace.name} \" has more than one agent" ,
350+ )
299351 }
300352 }
301353
0 commit comments