Skip to content

Commit 0e8c4e0

Browse files
committed
Encapsulate process execution in CLI manager
We will need to create a new function for config-ssh anyway since we will be doing our own thing rather than spawning the CLI in order to support multiple deployments so this preemptively encapsulates that logic.
1 parent 9ba74dc commit 0e8c4e0

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.coder.gateway.sdk
22

33
import com.intellij.openapi.diagnostic.Logger
4+
import org.zeroturnaround.exec.ProcessExecutor
45
import java.io.InputStream
56
import java.net.URL
67
import java.nio.file.Files
@@ -97,6 +98,42 @@ class CoderCLIManager(deployment: URL, buildVersion: String) {
9798
}
9899
}
99100

101+
/**
102+
* Use the provided credentials to authenticate the CLI.
103+
*/
104+
fun login(url: String, token: String): String {
105+
return exec("login", url, "--token", token)
106+
}
107+
108+
/**
109+
* Configure SSH to use this binary.
110+
*
111+
* TODO: Support multiple deployments; currently they will clobber each other.
112+
*/
113+
fun configSsh(): String {
114+
return exec("config-ssh", "--yes", "--use-previous-options")
115+
}
116+
117+
/**
118+
* Return the binary version.
119+
*/
120+
fun version(): String {
121+
return exec("version")
122+
}
123+
124+
/**
125+
* Execute the binary with the provided arguments.
126+
*
127+
* @return The command's output.
128+
*/
129+
private fun exec(vararg args: String): String {
130+
return ProcessExecutor()
131+
.command(localBinaryPath.toString(), *args)
132+
.readOutput(true)
133+
.execute()
134+
.outputUTF8()
135+
}
136+
100137
companion object {
101138
val logger = Logger.getInstance(CoderCLIManager::class.java.simpleName)
102139
}

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import kotlinx.coroutines.delay
5959
import kotlinx.coroutines.isActive
6060
import kotlinx.coroutines.launch
6161
import kotlinx.coroutines.withContext
62-
import org.zeroturnaround.exec.ProcessExecutor
6362
import java.awt.Component
6463
import java.awt.Dimension
6564
import java.awt.event.MouseEvent
@@ -530,21 +529,13 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
530529
text = "Configuring Coder CLI..."
531530
fraction = 0.5
532531
}
533-
val loginOutput = ProcessExecutor().command(
534-
cliManager.localBinaryPath.toString(),
535-
"login",
532+
val loginOutput = cliManager.login(
536533
localWizardModel.coderURL,
537-
"--token",
538534
localWizardModel.token
539-
).readOutput(true).execute().outputUTF8()
535+
)
540536
logger.info("coder-cli login output: $loginOutput")
541537
this.indicator.fraction = 0.8
542-
val sshConfigOutput = ProcessExecutor().command(
543-
cliManager.localBinaryPath.toString(),
544-
"config-ssh",
545-
"--yes",
546-
"--use-previous-options"
547-
).readOutput(true).execute().outputUTF8()
538+
val sshConfigOutput = cliManager.configSsh()
548539
logger.info("Result of `${cliManager.localBinaryPath} config-ssh --yes --use-previous-options`: $sshConfigOutput")
549540

550541
this.indicator.apply {

src/test/groovy/CoderCLIManagerTest.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.coder.gateway.sdk
22

3-
import org.zeroturnaround.exec.ProcessExecutor
43
import spock.lang.Unroll
54

65
import java.nio.file.Files
@@ -55,7 +54,7 @@ class CoderCLIManagerTest extends spock.lang.Specification {
5554
downloaded
5655
ccm.localBinaryPath.getParent() == dir
5756
ccm.localBinaryPath.toFile().exists()
58-
new ProcessExecutor().command(ccm.localBinaryPath.toString(), "version").readOutput(true).execute().outputUTF8().contains("Coder")
57+
ccm.version().contains("Coder")
5958
}
6059

6160
def "skips cli download if it already exists"() {

0 commit comments

Comments
 (0)