Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aefcc31

Browse files
committed
Extract setting source description
This will let us use it in auth flows that are not dialog-based like the current auth flow. In the future both flows might be refactored to share a code path but holding off on that until it becomes apparent there is a good interface for that.
1 parent abe0326 commit aefcc31

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/main/kotlin/com/coder/gateway/settings/CoderSettings.kt

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ enum class Source {
2828
QUERY, // From the Gateway link as a query parameter.
2929
SETTINGS, // Pulled from settings.
3030
USER, // Input by the user.
31+
;
32+
33+
/**
34+
* Return a description of the source.
35+
*/
36+
fun description(name: String, url: URL): String = when (this) {
37+
CONFIG -> "This $name was pulled from your global CLI config."
38+
DEPLOYMENT_CONFIG -> "This $name was pulled from your CLI config for ${url.host}."
39+
LAST_USED -> "This last used $name for ${url.host}."
40+
QUERY -> "This $name was pulled from the Gateway link from ${url.host}."
41+
USER -> "The last used $name for ${url.host}."
42+
ENVIRONMENT -> "This $name was pulled from an environment variable."
43+
SETTINGS -> "This $name was pulled from your settings for Coder Gateway."
44+
}
3145
}
3246

3347
open class CoderSettingsState(

src/main/kotlin/com/coder/gateway/util/Dialogs.kt

+7-18
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ class DialogUi(
185185
isRetry: Boolean,
186186
useExisting: Boolean,
187187
): Pair<String, Source>? {
188-
var (existingToken, tokenSource) = token ?: Pair("", Source.USER)
189188
val getTokenUrl = url.withPath("/login?redirect=%2Fcli-auth")
190189

191190
// On the first run either open a browser to generate a new token
@@ -199,7 +198,7 @@ class DialogUi(
199198
// Look on disk in case we already have a token, either in
200199
// the deployment's config or the global config.
201200
val tryToken = settings.token(url)
202-
if (tryToken != null && tryToken.first != existingToken) {
201+
if (tryToken != null && tryToken.first != token?.first) {
203202
return tryToken
204203
}
205204
}
@@ -212,29 +211,19 @@ class DialogUi(
212211
title = "Session Token",
213212
description = if (isRetry) {
214213
"This token was rejected by ${url.host}."
215-
} else if (tokenSource == Source.CONFIG) {
216-
"This token was pulled from your global CLI config."
217-
} else if (tokenSource == Source.DEPLOYMENT_CONFIG) {
218-
"This token was pulled from your CLI config for ${url.host}."
219-
} else if (tokenSource == Source.LAST_USED) {
220-
"This token was the last used token for ${url.host}."
221-
} else if (tokenSource == Source.QUERY) {
222-
"This token was pulled from the Gateway link from ${url.host}."
223-
} else if (existingToken.isNotBlank()) {
224-
"The last used token for ${url.host} is shown above."
225214
} else {
226-
"No existing token for ${url.host} found."
215+
token?.second?.description("token", url)
216+
?: "No existing token for ${url.host} found."
227217
},
228-
placeholder = existingToken,
218+
placeholder = token?.first,
229219
link = Pair("Session Token:", getTokenUrl.toString()),
230220
isError = isRetry,
231221
)
232222
if (tokenFromUser.isNullOrBlank()) {
233223
return null
234224
}
235-
if (tokenFromUser != existingToken) {
236-
tokenSource = Source.USER
237-
}
238-
return Pair(tokenFromUser, tokenSource)
225+
// If the user submitted the same token, keep the same source too.
226+
val source = if (tokenFromUser == token?.first) token.second else Source.USER
227+
return Pair(tokenFromUser, source)
239228
}
240229
}

0 commit comments

Comments
 (0)