Skip to content

Commit d1293f2

Browse files
committed
Add utilities to download coder cli
1 parent 1858f41 commit d1293f2

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.coder.gateway.sdk
2+
3+
import java.io.InputStream
4+
import java.net.URL
5+
import java.nio.file.Files
6+
import java.nio.file.Path
7+
import java.nio.file.StandardCopyOption
8+
9+
class CoderCLIDownloader {
10+
11+
fun downloadCLI(url: URL, outputName: String, ext: String): Path {
12+
val tempPath: Path = Files.createTempFile(outputName, ext)
13+
url.openStream().use {
14+
Files.copy(it as InputStream, tempPath, StandardCopyOption.REPLACE_EXISTING)
15+
}
16+
return tempPath
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.coder.gateway.sdk
2+
3+
import java.net.URL
4+
import java.nio.file.Path
5+
6+
class CoderCLIManager(private val url: URL) {
7+
private val coderCLIDownloader = CoderCLIDownloader()
8+
9+
fun download(): Path? {
10+
val os = getOS()
11+
val cliName = getCoderCLIForOS(os, getArch()) ?: return null
12+
val cliNameWitExt = if (os == OS.WINDOWS) "$cliName.exe" else cliName
13+
return coderCLIDownloader.downloadCLI(URL(url.protocol, url.host, url.port, "/bin/$cliNameWitExt"), cliName, if (os == OS.WINDOWS) ".exe" else "")
14+
}
15+
16+
private fun getCoderCLIForOS(os: OS?, arch: Arch?): String? {
17+
if (os == null || arch == null) {
18+
return null
19+
}
20+
return when (os) {
21+
OS.WINDOWS -> when (arch) {
22+
Arch.amd64 -> "coder-windows-amd64"
23+
Arch.arm64 -> "coder-windows-arm64"
24+
else -> null
25+
}
26+
OS.LINUX -> when (arch) {
27+
Arch.amd64 -> "coder-linux-amd64"
28+
Arch.arm64 -> "coder-linux-arm64"
29+
Arch.armv7 -> "coder-linux-armv7"
30+
}
31+
OS.MAC -> when (arch) {
32+
Arch.amd64 -> "coder-darwin-amd64"
33+
Arch.arm64 -> "coder-darwin-arm64"
34+
else -> null
35+
}
36+
}
37+
}
38+
39+
private fun getOS(): OS? {
40+
val os = System.getProperty("os.name").toLowerCase()
41+
return when {
42+
os.contains("win") -> {
43+
OS.WINDOWS
44+
}
45+
os.contains("nix") || os.contains("nux") || os.contains("aix") -> {
46+
OS.LINUX
47+
}
48+
os.contains("mac") -> {
49+
OS.MAC
50+
}
51+
else -> null
52+
}
53+
}
54+
55+
private fun getArch(): Arch? {
56+
val arch = System.getProperty("os.arch").toLowerCase()
57+
return when {
58+
arch.contains("amd64") -> Arch.amd64
59+
arch.contains("arm64") -> Arch.arm64
60+
arch.contains("armv7") -> Arch.armv7
61+
else -> null
62+
}
63+
}
64+
}
65+
66+
enum class OS {
67+
WINDOWS, LINUX, MAC
68+
}
69+
70+
enum class Arch {
71+
amd64, arm64, armv7
72+
}

0 commit comments

Comments
 (0)