Skip to content

Commit e065929

Browse files
committed
Move CLI config read into CLI manager
1 parent 848b1d0 commit e065929

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
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: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import com.coder.gateway.sdk.TemplateIconDownloader
1919
import com.coder.gateway.sdk.ex.AuthenticationResponseException
2020
import com.coder.gateway.sdk.ex.TemplateResponseException
2121
import com.coder.gateway.sdk.ex.WorkspaceResponseException
22-
import com.coder.gateway.sdk.getOS
2322
import com.coder.gateway.sdk.toURL
2423
import com.coder.gateway.sdk.v2.models.Workspace
2524
import com.coder.gateway.sdk.withPath
@@ -66,9 +65,6 @@ import java.awt.event.MouseListener
6665
import java.awt.event.MouseMotionListener
6766
import java.awt.font.TextAttribute
6867
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
7268
import java.net.SocketTimeoutException
7369
import javax.swing.Icon
7470
import javax.swing.JCheckBox
@@ -364,45 +360,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
364360
if (!url.isNullOrBlank() && !token.isNullOrBlank()) {
365361
return url to token
366362
}
367-
return readConfig()
368-
}
369-
370-
/**
371-
* Return the URL and token from the CLI config.
372-
*/
373-
private fun readConfig(): Pair<String?, String?> {
374-
val configDir = getConfigDir()
375-
logger.info("Reading config from $configDir")
376-
try {
377-
val url = Files.readString(configDir.resolve("url"))
378-
val token = Files.readString(configDir.resolve("session"))
379-
return url to token
380-
} catch (e: Exception) {
381-
return null to null // Probably has not configured the CLI yet.
382-
}
383-
}
384-
385-
/**
386-
* Return the config directory used by the CLI.
387-
*/
388-
private fun getConfigDir(): Path {
389-
var dir = System.getenv("CODER_CONFIG_DIR")
390-
if (!dir.isNullOrBlank()) {
391-
return Path.of(dir)
392-
}
393-
// The Coder CLI uses https://github.com/kirsle/configdir so this should
394-
// match how it behaves.
395-
return when(getOS()) {
396-
OS.WINDOWS -> Paths.get(System.getenv("APPDATA"), "coderv2")
397-
OS.MAC -> Paths.get(System.getenv("HOME"), "Library/Application Support/coderv2")
398-
else -> {
399-
dir = System.getenv("XDG_CONFIG_HOME")
400-
if (!dir.isNullOrBlank()) {
401-
return Paths.get(dir, "coderv2")
402-
}
403-
return Paths.get(System.getenv("HOME"), ".config/coderv2")
404-
}
405-
}
363+
return CoderCLIManager.readConfig()
406364
}
407365

408366
private fun updateWorkspaceActions() {
@@ -518,7 +476,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
518476
if (openBrowser && !localWizardModel.useExistingToken) {
519477
BrowserUtil.browse(getTokenUrl)
520478
} else if (localWizardModel.useExistingToken) {
521-
val (url, token) = readConfig()
479+
val (url, token) = CoderCLIManager.readConfig()
522480
if (url == localWizardModel.coderURL && !token.isNullOrBlank()) {
523481
logger.info("Injecting valid token from CLI config")
524482
localWizardModel.token = token

0 commit comments

Comments
 (0)