Skip to content

Commit ac9d363

Browse files
committed
Move CLI config read into CLI manager
1 parent c25c0df commit ac9d363

File tree

2 files changed

+48
-68
lines changed

2 files changed

+48
-68
lines changed

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

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

3+
import com.coder.gateway.views.steps.CoderWorkspacesStepView
34
import com.intellij.openapi.diagnostic.Logger
45
import org.zeroturnaround.exec.ProcessExecutor
56
import java.io.InputStream
67
import java.net.URL
78
import java.nio.file.Files
89
import java.nio.file.Path
10+
import java.nio.file.Paths
911
import java.nio.file.attribute.PosixFilePermissions
1012

1113
/**
@@ -153,5 +155,45 @@ class CoderCLIManager(deployment: URL, buildVersion: String) {
153155

154156
companion object {
155157
val logger = Logger.getInstance(CoderCLIManager::class.java.simpleName)
158+
159+
/**
160+
* Return the URL and token from the CLI config.
161+
*/
162+
@JvmStatic
163+
fun readConfig(): Pair<String?, String?> {
164+
val configDir = getConfigDir()
165+
CoderWorkspacesStepView.logger.info("Reading config from $configDir")
166+
return try {
167+
val url = Files.readString(configDir.resolve("url"))
168+
val token = Files.readString(configDir.resolve("session"))
169+
url to token
170+
} catch (e: Exception) {
171+
null to null // Probably has not configured the CLI yet.
172+
}
173+
}
174+
175+
/**
176+
* Return the config directory used by the CLI.
177+
*/
178+
@JvmStatic
179+
fun getConfigDir(): Path {
180+
var dir = System.getenv("CODER_CONFIG_DIR")
181+
if (!dir.isNullOrBlank()) {
182+
return Path.of(dir)
183+
}
184+
// The Coder CLI uses https://github.com/kirsle/configdir so this should
185+
// match how it behaves.
186+
return when (getOS()) {
187+
OS.WINDOWS -> Paths.get(System.getenv("APPDATA"), "coderv2")
188+
OS.MAC -> Paths.get(System.getenv("HOME"), "Library/Application Support/coderv2")
189+
else -> {
190+
dir = System.getenv("XDG_CONFIG_HOME")
191+
if (!dir.isNullOrBlank()) {
192+
return Paths.get(dir, "coderv2")
193+
}
194+
return Paths.get(System.getenv("HOME"), ".config/coderv2")
195+
}
196+
}
197+
}
156198
}
157199
}

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

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,13 @@ import com.coder.gateway.icons.CoderIcons
66
import com.coder.gateway.models.CoderWorkspacesWizardModel
77
import com.coder.gateway.models.WorkspaceAgentModel
88
import com.coder.gateway.models.WorkspaceAgentStatus
9-
import com.coder.gateway.models.WorkspaceAgentStatus.FAILED
10-
import com.coder.gateway.models.WorkspaceAgentStatus.RUNNING
11-
import com.coder.gateway.models.WorkspaceAgentStatus.STOPPED
9+
import com.coder.gateway.models.WorkspaceAgentStatus.*
1210
import com.coder.gateway.models.WorkspaceVersionStatus
13-
import com.coder.gateway.sdk.Arch
14-
import com.coder.gateway.sdk.CoderCLIManager
15-
import com.coder.gateway.sdk.CoderRestClientService
16-
import com.coder.gateway.sdk.CoderSemVer
17-
import com.coder.gateway.sdk.OS
18-
import com.coder.gateway.sdk.TemplateIconDownloader
11+
import com.coder.gateway.sdk.*
1912
import com.coder.gateway.sdk.ex.AuthenticationResponseException
2013
import com.coder.gateway.sdk.ex.TemplateResponseException
2114
import com.coder.gateway.sdk.ex.WorkspaceResponseException
22-
import com.coder.gateway.sdk.getOS
23-
import com.coder.gateway.sdk.toURL
2415
import com.coder.gateway.sdk.v2.models.Workspace
25-
import com.coder.gateway.sdk.withPath
2616
import com.intellij.ide.ActivityTracker
2717
import com.intellij.ide.BrowserUtil
2818
import com.intellij.ide.IdeBundle
@@ -51,30 +41,16 @@ import com.intellij.util.ui.JBUI
5141
import com.intellij.util.ui.ListTableModel
5242
import com.intellij.util.ui.table.IconTableCellRenderer
5343
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
54-
import kotlinx.coroutines.CoroutineScope
55-
import kotlinx.coroutines.Dispatchers
56-
import kotlinx.coroutines.Job
57-
import kotlinx.coroutines.cancel
58-
import kotlinx.coroutines.delay
59-
import kotlinx.coroutines.isActive
60-
import kotlinx.coroutines.launch
61-
import kotlinx.coroutines.withContext
44+
import kotlinx.coroutines.*
6245
import java.awt.Component
6346
import java.awt.Dimension
6447
import java.awt.event.MouseEvent
6548
import java.awt.event.MouseListener
6649
import java.awt.event.MouseMotionListener
6750
import java.awt.font.TextAttribute
6851
import java.awt.font.TextAttribute.UNDERLINE_ON
69-
import java.nio.file.Files
70-
import java.nio.file.Path
71-
import java.nio.file.Paths
7252
import java.net.SocketTimeoutException
73-
import javax.swing.Icon
74-
import javax.swing.JCheckBox
75-
import javax.swing.JTable
76-
import javax.swing.JTextField
77-
import javax.swing.ListSelectionModel
53+
import javax.swing.*
7854
import javax.swing.table.DefaultTableCellRenderer
7955
import javax.swing.table.TableCellRenderer
8056

@@ -395,45 +371,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
395371
if (!url.isNullOrBlank() && !token.isNullOrBlank()) {
396372
return url to token
397373
}
398-
return readConfig()
399-
}
400-
401-
/**
402-
* Return the URL and token from the CLI config.
403-
*/
404-
private fun readConfig(): Pair<String?, String?> {
405-
val configDir = getConfigDir()
406-
logger.info("Reading config from $configDir")
407-
try {
408-
val url = Files.readString(configDir.resolve("url"))
409-
val token = Files.readString(configDir.resolve("session"))
410-
return url to token
411-
} catch (e: Exception) {
412-
return null to null // Probably has not configured the CLI yet.
413-
}
414-
}
415-
416-
/**
417-
* Return the config directory used by the CLI.
418-
*/
419-
private fun getConfigDir(): Path {
420-
var dir = System.getenv("CODER_CONFIG_DIR")
421-
if (!dir.isNullOrBlank()) {
422-
return Path.of(dir)
423-
}
424-
// The Coder CLI uses https://github.com/kirsle/configdir so this should
425-
// match how it behaves.
426-
return when (getOS()) {
427-
OS.WINDOWS -> Paths.get(System.getenv("APPDATA"), "coderv2")
428-
OS.MAC -> Paths.get(System.getenv("HOME"), "Library/Application Support/coderv2")
429-
else -> {
430-
dir = System.getenv("XDG_CONFIG_HOME")
431-
if (!dir.isNullOrBlank()) {
432-
return Paths.get(dir, "coderv2")
433-
}
434-
return Paths.get(System.getenv("HOME"), ".config/coderv2")
435-
}
436-
}
374+
return CoderCLIManager.readConfig()
437375
}
438376

439377
private fun updateWorkspaceActions() {
@@ -554,7 +492,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
554492
if (openBrowser && !localWizardModel.useExistingToken) {
555493
BrowserUtil.browse(getTokenUrl)
556494
} else if (localWizardModel.useExistingToken) {
557-
val (url, token) = readConfig()
495+
val (url, token) = CoderCLIManager.readConfig()
558496
if (url == localWizardModel.coderURL && !token.isNullOrBlank()) {
559497
logger.info("Injecting valid token from CLI config")
560498
localWizardModel.token = token

0 commit comments

Comments
 (0)