Skip to content

Commit ce35939

Browse files
committed
fix: compiler errors (3)
- a couple of functions on the existing models and views transformed into class properties - while other functions are now suspend functions
1 parent 3023aea commit ce35939

File tree

9 files changed

+80
-73
lines changed

9 files changed

+80
-73
lines changed

src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt

+13-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import kotlin.time.Duration.Companion.seconds
4343
class CoderRemoteProvider(
4444
private val serviceLocator: ServiceLocator,
4545
private val httpClient: OkHttpClient,
46-
) : RemoteProvider {
46+
) : RemoteProvider("Coder") {
4747
private val logger = LoggerFactory.getLogger(javaClass)
4848

4949
private val ui: ToolboxUi = serviceLocator.getService(ToolboxUi::class.java)
@@ -185,18 +185,18 @@ class CoderRemoteProvider(
185185
consumer.consumeEnvironments(emptyList(), true)
186186
}
187187

188-
override fun getName(): String = "Coder"
189-
override fun getSvgIcon(): SvgIcon =
188+
override val svgIcon: SvgIcon =
190189
SvgIcon(this::class.java.getResourceAsStream("/icon.svg")?.readAllBytes() ?: byteArrayOf())
191190

192-
override fun getNoEnvironmentsSvgIcon(): ByteArray =
193-
this::class.java.getResourceAsStream("/icon.svg")?.readAllBytes() ?: byteArrayOf()
191+
override val noEnvironmentsSvgIcon: SvgIcon? =
192+
SvgIcon(this::class.java.getResourceAsStream("/icon.svg")?.readAllBytes() ?: byteArrayOf())
194193

195194
/**
196195
* TODO@JB: It would be nice to show "loading workspaces" at first but it
197196
* appears to be only called once.
198197
*/
199-
override fun getNoEnvironmentsDescription(): String = "No workspaces yet"
198+
override val noEnvironmentsDescription: String? = "No workspaces yet"
199+
200200

201201
/**
202202
* TODO@JB: Supposedly, setting this to false causes the new environment
@@ -205,7 +205,7 @@ class CoderRemoteProvider(
205205
* this changes it would be nice to have a new spot to show the
206206
* URL.
207207
*/
208-
override fun canCreateNewEnvironments(): Boolean = false
208+
override val canCreateNewEnvironments: Boolean = false
209209

210210
/**
211211
* Just displays the deployment URL at the moment, but we could use this as
@@ -216,7 +216,7 @@ class CoderRemoteProvider(
216216
/**
217217
* We always show a list of environments.
218218
*/
219-
override fun isSingleEnvironment(): Boolean = false
219+
override val isSingleEnvironment: Boolean = false
220220

221221
/**
222222
* TODO: Possibly a good idea to start/stop polling based on visibility, at
@@ -241,9 +241,11 @@ class CoderRemoteProvider(
241241
*/
242242
override fun handleUri(uri: URI) {
243243
val params = uri.toQueryParameters()
244-
val name = linkHandler.handle(params)
245-
// TODO@JB: Now what? How do we actually connect this workspace?
246-
logger.debug("External request for {}: {}", name, uri)
244+
coroutineScope.launch {
245+
val name = linkHandler.handle(params)
246+
// TODO@JB: Now what? How do we actually connect this workspace?
247+
logger.debug("External request for {}: {}", name, uri)
248+
}
247249
}
248250

249251
/**

src/main/kotlin/com/coder/toolbox/util/Dialogs.kt

+6-9
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ class DialogUi(
1616
private val settings: CoderSettings,
1717
private val ui: ToolboxUi,
1818
) {
19-
fun confirm(title: String, description: String): Boolean {
20-
val f = ui.showOkCancelPopup(title, description, "Yes", "No")
21-
return f.get()
19+
suspend fun confirm(title: String, description: String): Boolean {
20+
return ui.showOkCancelPopup(title, description, "Yes", "No")
2221
}
2322

24-
fun ask(
23+
suspend fun ask(
2524
title: String,
2625
description: String,
2726
placeholder: String? = null,
@@ -30,12 +29,10 @@ class DialogUi(
3029
isError: Boolean = false,
3130
link: Pair<String, String>? = null,
3231
): String? {
33-
val f = ui.showTextInputPopup(title, description, placeholder, TextType.General, "OK", "Cancel")
34-
return f.get()
32+
return ui.showTextInputPopup(title, description, placeholder, TextType.General, "OK", "Cancel")
3533
}
3634

37-
private fun openUrl(url: URL) {
38-
// TODO - check this later
35+
private suspend fun openUrl(url: URL) {
3936
BrowserUtil.browse(url.toString()) {
4037
ui.showErrorInfoPopup(it)
4138
}
@@ -53,7 +50,7 @@ class DialogUi(
5350
* other existing token) unless this is a retry to avoid clobbering the
5451
* token that just failed.
5552
*/
56-
fun askToken(
53+
suspend fun askToken(
5754
url: URL,
5855
token: Pair<String, Source>?,
5956
useExisting: Boolean,

src/main/kotlin/com/coder/toolbox/util/LinkHandler.kt

+31-14
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ open class LinkHandler(
2626
* Throw if required arguments are not supplied or the workspace is not in a
2727
* connectable state.
2828
*/
29-
fun handle(
29+
suspend fun handle(
3030
parameters: Map<String, String>,
3131
indicator: ((t: String) -> Unit)? = null,
3232
): 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")
3435
if (deploymentURL.isNullOrBlank()) {
3536
throw MissingArgumentException("Query parameter \"$URL\" is missing")
3637
}
@@ -44,11 +45,12 @@ open class LinkHandler(
4445
val client = try {
4546
authenticate(deploymentURL, queryToken)
4647
} catch (ex: MissingArgumentException) {
47-
throw MissingArgumentException("Query parameter \"$TOKEN\" is missing")
48+
throw MissingArgumentException("Query parameter \"$TOKEN\" is missing", ex)
4849
}
4950

5051
// 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")
5254

5355
val workspaces = client.workspaces()
5456
val workspace =
@@ -60,19 +62,28 @@ open class LinkHandler(
6062
WorkspaceStatus.PENDING, WorkspaceStatus.STARTING ->
6163
// TODO: Wait for the workspace to turn on.
6264
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",
6468
)
69+
6570
WorkspaceStatus.STOPPING, WorkspaceStatus.STOPPED,
6671
WorkspaceStatus.CANCELING, WorkspaceStatus.CANCELED,
67-
->
72+
->
6873
// TODO: Turn on the workspace.
6974
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",
7178
)
79+
7280
WorkspaceStatus.FAILED, WorkspaceStatus.DELETING, WorkspaceStatus.DELETED ->
7381
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",
7585
)
86+
7687
WorkspaceStatus.RUNNING -> Unit // All is well
7788
}
7889

@@ -83,10 +94,16 @@ open class LinkHandler(
8394
if (status.pending()) {
8495
// TODO: Wait for the agent to be ready.
8596
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",
87100
)
88101
} 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+
)
90107
}
91108

92109
val cli =
@@ -120,7 +137,7 @@ open class LinkHandler(
120137
* Throw MissingArgumentException if the user aborts. Any network or invalid
121138
* token error may also be thrown.
122139
*/
123-
private fun authenticate(
140+
private suspend fun authenticate(
124141
deploymentURL: String,
125142
tryToken: Pair<String, Source>?,
126143
error: String? = null,
@@ -172,7 +189,7 @@ open class LinkHandler(
172189
/**
173190
* Check that the link is allowlisted. If not, confirm with the user.
174191
*/
175-
private fun verifyDownloadLink(parameters: Map<String, String>) {
192+
private suspend fun verifyDownloadLink(parameters: Map<String, String>) {
176193
val link = parameters.ideDownloadLink()
177194
if (link.isNullOrBlank()) {
178195
return // Nothing to verify
@@ -233,7 +250,7 @@ private fun isAllowlisted(url: URL): Triple<Boolean, Boolean, String> {
233250

234251
val allowlisted =
235252
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") }
237254
val https = url.protocol == "https" && finalUrl.protocol == "https"
238255
return Triple(allowlisted, https, linkWithRedirect)
239256
}
@@ -308,4 +325,4 @@ internal fun getMatchingAgent(
308325
return agent
309326
}
310327

311-
class MissingArgumentException(message: String) : IllegalArgumentException(message)
328+
class MissingArgumentException(message: String, ex: Throwable? = null) : IllegalArgumentException(message, ex)

src/main/kotlin/com/coder/toolbox/views/CoderPage.kt

+13-14
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import java.util.function.Consumer
1919
* to use the mouse.
2020
*/
2121
abstract class CoderPage(
22-
private val showIcon: Boolean = true,
23-
) : UiPage {
22+
title: String,
23+
showIcon: Boolean = true,
24+
) : UiPage(title) {
2425
private val logger = LoggerFactory.getLogger(javaClass)
2526

2627
/**
@@ -44,12 +45,10 @@ abstract class CoderPage(
4445
*
4546
* This seems to only work on the first page.
4647
*/
47-
override fun getSvgIcon(): SvgIcon {
48-
return if (showIcon) {
49-
SvgIcon(this::class.java.getResourceAsStream("/icon.svg")?.readAllBytes() ?: byteArrayOf())
50-
} else {
51-
SvgIcon(byteArrayOf())
52-
}
48+
override val svgIcon: SvgIcon? = if (showIcon) {
49+
SvgIcon(this::class.java.getResourceAsStream("/icon.svg")?.readAllBytes() ?: byteArrayOf())
50+
} else {
51+
SvgIcon(byteArrayOf())
5352
}
5453

5554
/**
@@ -87,14 +86,14 @@ abstract class CoderPage(
8786
* An action that simply runs the provided callback.
8887
*/
8988
class Action(
90-
private val label: String,
91-
private val closesPage: Boolean = false,
92-
private val enabled: () -> Boolean = { true },
89+
description: String,
90+
closesPage: Boolean = false,
91+
enabled: () -> Boolean = { true },
9392
private val actionBlock: () -> Unit,
9493
) : RunnableActionDescription {
95-
override fun getLabel(): String = label
96-
override fun getShouldClosePage(): Boolean = closesPage
97-
override fun isEnabled(): Boolean = enabled()
94+
override val label: String = description
95+
override val shouldClosePage: Boolean = closesPage
96+
override val isEnabled: Boolean = enabled()
9897
override fun run() {
9998
actionBlock()
10099
}

src/main/kotlin/com/coder/toolbox/views/CoderSettingsPage.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.jetbrains.toolbox.api.ui.components.UiField
1414
* TODO@JB: There is no scroll, and our settings do not fit. As a consequence,
1515
* I have not been able to test this page.
1616
*/
17-
class CoderSettingsPage(private val settings: CoderSettingsService) : CoderPage(false) {
17+
class CoderSettingsPage(private val settings: CoderSettingsService) : CoderPage("Coder Settings", false) {
1818
// TODO: Copy over the descriptions, holding until I can test this page.
1919
private val binarySourceField = TextField("Binary source", settings.binarySource, TextType.General)
2020
private val binaryDirectoryField = TextField("Binary directory", settings.binaryDirectory, TextType.General)
@@ -30,7 +30,7 @@ class CoderSettingsPage(private val settings: CoderSettingsService) : CoderPage(
3030
TextField("TLS alternate hostname", settings.tlsAlternateHostname, TextType.General)
3131
private val disableAutostartField = CheckboxField(settings.disableAutostart, "Disable autostart")
3232

33-
override fun getFields(): MutableList<UiField> = mutableListOf(
33+
override val fields: MutableList<UiField> = mutableListOf(
3434
binarySourceField,
3535
enableDownloadsField,
3636
binaryDirectoryField,
@@ -44,9 +44,7 @@ class CoderSettingsPage(private val settings: CoderSettingsService) : CoderPage(
4444
disableAutostartField,
4545
)
4646

47-
override fun getTitle(): String = "Coder Settings"
48-
49-
override fun getActionButtons(): MutableList<RunnableActionDescription> = mutableListOf(
47+
override val actionButtons: MutableList<RunnableActionDescription> = mutableListOf(
5048
Action("Save", closesPage = true) {
5149
settings.binarySource = binarySourceField.text.value
5250
settings.binaryDirectory = binaryDirectoryField.text.value

src/main/kotlin/com/coder/toolbox/views/ConnectPage.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class ConnectPage(
2929
client: CoderRestClient,
3030
cli: CoderCLIManager,
3131
) -> Unit,
32-
) : CoderPage() {
32+
) : CoderPage("Connecting to Coder") {
3333
private var signInJob: Job? = null
3434

3535
private var statusField = LabelField("Connecting to ${url.host}...")
3636

37-
override fun getTitle(): String = "Connecting to Coder"
38-
override fun getDescription(): String = "Please wait while we configure Toolbox for ${url.host}."
37+
override val description: String = "Please wait while we configure Toolbox for ${url.host}."
3938

4039
init {
4140
connect()
@@ -46,15 +45,15 @@ class ConnectPage(
4645
*
4746
* TODO@JB: This looks kinda sparse. A centered spinner would be welcome.
4847
*/
49-
override fun getFields(): MutableList<UiField> = listOfNotNull(
48+
override val fields: MutableList<UiField> = listOfNotNull(
5049
statusField,
5150
errorField,
5251
).toMutableList()
5352

5453
/**
5554
* Show a retry button on error.
5655
*/
57-
override fun getActionButtons(): MutableList<RunnableActionDescription> = listOfNotNull(
56+
override val actionButtons: MutableList<RunnableActionDescription> = listOfNotNull(
5857
if (errorField != null) Action("Retry", closesPage = false) { retry() } else null,
5958
if (errorField != null) Action("Cancel", closesPage = false) { onCancel() } else null,
6059
).toMutableList()

src/main/kotlin/com/coder/toolbox/views/NewEnvironmentPage.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.jetbrains.toolbox.api.ui.components.UiField
1010
* For now we just use this to display the deployment URL since we do not
1111
* support creating environments from the plugin.
1212
*/
13-
class NewEnvironmentPage(private val deploymentURL: String?) : CoderPage() {
14-
override fun getFields(): MutableList<UiField> = mutableListOf()
15-
override fun getTitle(): String = deploymentURL ?: ""
13+
class NewEnvironmentPage(private val deploymentURL: String?) : CoderPage(deploymentURL ?: "") {
14+
override val fields: MutableList<UiField> = mutableListOf()
1615
}

src/main/kotlin/com/coder/toolbox/views/SignInPage.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ import java.net.URL
1717
class SignInPage(
1818
private val deploymentURL: Pair<String, Source>?,
1919
private val onSignIn: (deploymentURL: URL) -> Unit,
20-
) : CoderPage() {
20+
) : CoderPage("Sign In to Coder") {
2121
private val urlField = TextField("Deployment URL", deploymentURL?.first ?: "", TextType.General)
2222

23-
override fun getTitle(): String = "Sign In to Coder"
24-
2523
/**
2624
* Fields for this page, displayed in order.
2725
*
2826
* TODO@JB: Fields are reset when you navigate back.
2927
* Ideally they remember what the user entered.
3028
*/
31-
override fun getFields(): MutableList<UiField> = listOfNotNull(
29+
override val fields: MutableList<UiField> = listOfNotNull(
3230
urlField,
3331
deploymentURL?.let { LabelField(deploymentURL.second.description("URL")) },
3432
errorField,
@@ -37,7 +35,7 @@ class SignInPage(
3735
/**
3836
* Buttons displayed at the bottom of the page.
3937
*/
40-
override fun getActionButtons(): MutableList<RunnableActionDescription> = mutableListOf(
38+
override val actionButtons: MutableList<RunnableActionDescription> = mutableListOf(
4139
Action("Sign In", closesPage = false) { submit() },
4240
)
4341

0 commit comments

Comments
 (0)