Skip to content

Commit 39dca82

Browse files
committed
Move CLI config read into CLI manager
1 parent b6e0077 commit 39dca82

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
/**
@@ -151,5 +153,45 @@ class CoderCLIManager(deployment: URL, buildVersion: String) {
151153

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

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() {
@@ -555,7 +493,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
555493
if (openBrowser && !localWizardModel.useExistingToken) {
556494
BrowserUtil.browse(getTokenUrl)
557495
} else if (localWizardModel.useExistingToken) {
558-
val (url, token) = readConfig()
496+
val (url, token) = CoderCLIManager.readConfig()
559497
if (url == localWizardModel.coderURL && !token.isNullOrBlank()) {
560498
logger.info("Injecting valid token from CLI config")
561499
localWizardModel.token = token

0 commit comments

Comments
 (0)