Skip to content

Commit dbf8560

Browse files
committed
impl: support for downloading the releases.coder.com signature
1 parent fb6e784 commit dbf8560

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,18 @@ class CoderCLIManager(
158158
val cliDownloadResult = withContext(Dispatchers.IO) {
159159
downloader.downloadCli(buildVersion, showTextProgress)
160160
}
161+
if (cliDownloadResult.isSkipped()) return false
162+
if (cliDownloadResult.isNotFoundOrFailed()) throw IllegalStateException("Could not find or download Coder CLI")
161163

162164
var singatureDownloadResult = withContext(Dispatchers.IO) {
163165
downloader.downloadSignature(showTextProgress)
164166
}
165167

168+
if (singatureDownloadResult.isNotDownloaded()) {
169+
context.logger.info("Trying to download signature file from releases.coder.com")
170+
singatureDownloadResult = downloader.downloadReleasesSignature(showTextProgress)
171+
}
172+
166173
return cliDownloadResult.isDownloaded()
167174
}
168175

src/main/kotlin/com/coder/toolbox/cli/downloader/CoderDownloadService.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.io.FileInputStream
1313
import java.net.HttpURLConnection.HTTP_NOT_FOUND
1414
import java.net.HttpURLConnection.HTTP_NOT_MODIFIED
1515
import java.net.HttpURLConnection.HTTP_OK
16+
import java.net.URI
1617
import java.net.URL
1718
import java.nio.file.Files
1819
import java.nio.file.Path
@@ -48,7 +49,7 @@ class CoderDownloadService(
4849
HTTP_OK -> {
4950
context.logger.info("Downloading binary to $localBinaryPath")
5051
response.saveToDisk(localBinaryPath, showTextProgress, buildVersion)?.makeExecutable()
51-
DownloadResult.Downloaded(localBinaryPath)
52+
DownloadResult.Downloaded(remoteBinaryURL, localBinaryPath)
5253
}
5354

5455
HTTP_NOT_MODIFIED -> {
@@ -148,10 +149,14 @@ class CoderDownloadService(
148149
}
149150

150151
suspend fun downloadSignature(showTextProgress: (String) -> Unit): DownloadResult {
152+
return downloadSignature(remoteBinaryURL, showTextProgress)
153+
}
154+
155+
private suspend fun downloadSignature(url: URL, showTextProgress: (String) -> Unit): DownloadResult {
151156
val defaultCliNameWithoutExt = context.settingsStore.defaultCliBinaryNameByOsAndArch.split('.').first()
152157
val signatureName = "$defaultCliNameWithoutExt.asc"
153158

154-
val signatureURL = remoteBinaryURL.withLastSegment(signatureName)
159+
val signatureURL = url.withLastSegment(signatureName)
155160
val localSignaturePath = localBinaryPath.parent.resolve(signatureName)
156161
context.logger.info("Downloading signature from $signatureURL")
157162

@@ -163,7 +168,7 @@ class CoderDownloadService(
163168
return when (response.code()) {
164169
HTTP_OK -> {
165170
response.saveToDisk(localSignaturePath, showTextProgress)
166-
DownloadResult.Downloaded(localSignaturePath)
171+
DownloadResult.Downloaded(signatureURL, localSignaturePath)
167172
}
168173

169174
HTTP_NOT_FOUND -> {
@@ -180,5 +185,10 @@ class CoderDownloadService(
180185
)
181186
}
182187
}
188+
189+
}
190+
191+
suspend fun downloadReleasesSignature(showTextProgress: (String) -> Unit): DownloadResult {
192+
return downloadSignature(URI.create("https://releases.coder.com/bin").toURL(), showTextProgress)
183193
}
184194
}

src/main/kotlin/com/coder/toolbox/cli/downloader/DownloadResult.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coder.toolbox.cli.downloader
22

3+
import java.net.URL
34
import java.nio.file.Path
45

56

@@ -9,8 +10,14 @@ import java.nio.file.Path
910
sealed class DownloadResult {
1011
object Skipped : DownloadResult()
1112
object NotFound : DownloadResult()
12-
data class Downloaded(val savePath: Path) : DownloadResult()
13+
data class Downloaded(val source: URL, val dst: Path) : DownloadResult()
1314
data class Failed(val error: Exception) : DownloadResult()
1415

16+
fun isSkipped(): Boolean = this is Skipped
17+
18+
fun isNotFoundOrFailed(): Boolean = this is NotFound || this is Failed
19+
1520
fun isDownloaded(): Boolean = this is Downloaded
21+
22+
fun isNotDownloaded(): Boolean = this !is Downloaded
1623
}

0 commit comments

Comments
 (0)