Skip to content

Commit 3668d46

Browse files
committed
chore: compact code and run signature download on the IO thread
1 parent ea3e379 commit 3668d46

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/main/kotlin/com/coder/toolbox/cli/CoderCLIManager.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,31 +159,36 @@ class CoderCLIManager(
159159
* Download the CLI from the deployment if necessary.
160160
*/
161161
suspend fun download(buildVersion: String, showTextProgress: (String) -> Unit): Boolean {
162-
val cliDownloadResult = withContext(Dispatchers.IO) {
162+
val cliResult = withContext(Dispatchers.IO) {
163163
downloader.downloadCli(buildVersion, showTextProgress)
164+
}.let { result ->
165+
when {
166+
result.isSkipped() -> return false
167+
result.isNotFoundOrFailed() -> throw IllegalStateException("Could not find or download Coder CLI")
168+
else -> result as Downloaded
169+
}
164170
}
165-
if (cliDownloadResult.isSkipped()) return false
166-
if (cliDownloadResult.isNotFoundOrFailed()) throw IllegalStateException("Could not find or download Coder CLI")
167171

168-
var singatureDownloadResult = withContext(Dispatchers.IO) {
172+
var signatureDownloadResult = withContext(Dispatchers.IO) {
169173
downloader.downloadSignature(showTextProgress)
170174
}
171175

172-
if (singatureDownloadResult.isNotDownloaded()) {
176+
if (signatureDownloadResult.isNotDownloaded()) {
173177
context.logger.info("Trying to download signature file from releases.coder.com")
174-
singatureDownloadResult = downloader.downloadReleasesSignature(showTextProgress)
178+
signatureDownloadResult = withContext(Dispatchers.IO) {
179+
downloader.downloadReleasesSignature(showTextProgress)
180+
}
175181
}
176182

177183
// if we could not find any signature and the user wants to explicitly
178184
// confirm whether we run an unsigned cli
179-
if (singatureDownloadResult.isNotDownloaded()) {
180-
val cli = cliDownloadResult as Downloaded
185+
if (signatureDownloadResult.isNotDownloaded()) {
181186
if (context.settingsStore.allowUnsignedBinaryWithoutPrompt) {
182-
context.logger.warn("Running unsigned CLI from ${cli.source}")
187+
context.logger.warn("Running unsigned CLI from ${cliResult.source}")
183188
} else {
184189
val acceptsUnsignedBinary = context.ui.showYesNoPopup(
185190
context.i18n.ptrl("Security Warning"),
186-
context.i18n.pnotr("Can't verify the integrity of the Coder CLI pulled from ${cli.source}"),
191+
context.i18n.pnotr("Can't verify the integrity of the Coder CLI pulled from ${cliResult.source}"),
187192
context.i18n.ptrl("Accept"),
188193
context.i18n.ptrl("Abort"),
189194
)
@@ -193,13 +198,13 @@ class CoderCLIManager(
193198
} else {
194199
// remove the cli, otherwise next time the user tries to login the cached cli is picked up
195200
// and we don't verify cached cli signatures
196-
Files.delete(cli.dst)
197-
throw UnsignedBinaryExecutionDeniedException("Running unsigned CLI from ${cli.source} was denied by the user")
201+
Files.delete(cliResult.dst)
202+
throw UnsignedBinaryExecutionDeniedException("Running unsigned CLI from ${cliResult.source} was denied by the user")
198203
}
199204
}
200205
}
201206

202-
return cliDownloadResult.isDownloaded()
207+
return cliResult.isDownloaded()
203208
}
204209

205210
/**

0 commit comments

Comments
 (0)