1
1
package com.coder.gateway.sdk
2
2
3
3
import com.intellij.openapi.diagnostic.Logger
4
+ import java.io.InputStream
4
5
import java.net.URL
6
+ import java.nio.file.Files
5
7
import java.nio.file.Path
6
8
import java.nio.file.Paths
9
+ import java.nio.file.StandardCopyOption
7
10
8
11
class CoderCLIManager (url : URL , buildVersion : String ) {
9
- var remoteCliPath: URL
10
- var localCliPath: Path
12
+ var remoteCli: URL
13
+ var localCli: Path
14
+ private var cliNamePrefix: String
15
+ private var tmpDir: String
16
+ private var cliFileName: String
11
17
12
18
init {
13
19
val os = getOS()
14
- val cliName = getCoderCLIForOS(os, getArch())
15
- val cliNameWithExt = if (os == OS .WINDOWS ) " $cliName .exe" else cliName
16
- val filename = if (os == OS .WINDOWS ) " ${cliName } -${buildVersion} .exe" else " ${cliName } -${buildVersion} "
20
+ cliNamePrefix = getCoderCLIForOS(os, getArch())
21
+ val cliNameWithExt = if (os == OS .WINDOWS ) " $cliNamePrefix .exe" else cliNamePrefix
22
+ cliFileName = if (os == OS .WINDOWS ) " ${cliNamePrefix } -${buildVersion} .exe" else " ${cliNamePrefix } -${buildVersion} "
17
23
18
- remoteCliPath = URL (url.protocol, url.host, url.port, " /bin/$cliNameWithExt " )
19
- localCliPath = Paths .get(System .getProperty(" java.io.tmpdir" ), filename)
24
+ remoteCli = URL (url.protocol, url.host, url.port, " /bin/$cliNameWithExt " )
25
+ tmpDir = System .getProperty(" java.io.tmpdir" )
26
+ localCli = Paths .get(tmpDir, cliFileName)
20
27
}
21
28
22
- private fun getCoderCLIForOS (os : OS ? , arch : Arch ? ): String? {
29
+ private fun getCoderCLIForOS (os : OS ? , arch : Arch ? ): String {
23
30
logger.info(" Resolving coder cli for $os $arch " )
24
31
if (os == null ) {
25
- return null
32
+ logger.error(" Could not resolve client OS and architecture, defaulting to WINDOWS AMD64" )
33
+ return " coder-windows-amd64"
26
34
}
27
35
return when (os) {
28
36
OS .WINDOWS -> when (arch) {
@@ -46,6 +54,25 @@ class CoderCLIManager(url: URL, buildVersion: String) {
46
54
}
47
55
}
48
56
57
+ fun downloadCLI (): Boolean {
58
+ if (Files .exists(localCli)) {
59
+ logger.info(" ${localCli.toAbsolutePath()} already exists, skipping download" )
60
+ return false
61
+ }
62
+ logger.info(" Starting Coder CLI download to ${localCli.toAbsolutePath()} " )
63
+ remoteCli.openStream().use {
64
+ Files .copy(it as InputStream , localCli, StandardCopyOption .REPLACE_EXISTING )
65
+ }
66
+ return true
67
+ }
68
+
69
+ fun removeOldCli () {
70
+ Files .walk(Path .of(tmpDir)).sorted().map { it.toFile() }.filter { it.name.contains(cliNamePrefix) && ! it.name.contains(cliFileName) }.forEach {
71
+ logger.info(" Removing $it because it is an old coder cli" )
72
+ it.delete()
73
+ }
74
+ }
75
+
49
76
companion object {
50
77
val logger = Logger .getInstance(CoderCLIManager ::class .java.simpleName)
51
78
}
0 commit comments