1
+ package com.coder.gateway
2
+
3
+ import com.coder.gateway.models.RecentWorkspaceConnection
4
+ import com.intellij.remote.AuthType
5
+ import com.intellij.remote.RemoteCredentialsHolder
6
+ import com.intellij.ssh.config.unified.SshConfig
7
+ import com.jetbrains.gateway.ssh.HighLevelHostAccessor
8
+ import com.jetbrains.gateway.ssh.HostDeployInputs
9
+ import com.jetbrains.gateway.ssh.IdeInfo
10
+ import com.jetbrains.gateway.ssh.IdeWithStatus
11
+ import com.jetbrains.gateway.ssh.IntelliJPlatformProduct
12
+ import com.jetbrains.gateway.ssh.deploy.DeployTargetInfo
13
+ import java.net.URI
14
+ import java.time.LocalDateTime
15
+ import java.time.format.DateTimeFormatter
16
+
17
+ private const val CODER_WORKSPACE_HOSTNAME = " coder_workspace_hostname"
18
+ private const val TYPE = " type"
19
+ private const val VALUE_FOR_TYPE = " coder"
20
+ private const val PROJECT_PATH = " project_path"
21
+ private const val IDE_DOWNLOAD_LINK = " ide_download_link"
22
+ private const val IDE_PRODUCT_CODE = " ide_product_code"
23
+ private const val IDE_BUILD_NUMBER = " ide_build_number"
24
+ private const val IDE_PATH_ON_HOST = " ide_path_on_host"
25
+ private const val WEB_TERMINAL_LINK = " web_terminal_link"
26
+
27
+ private val localTimeFormatter = DateTimeFormatter .ofPattern(" yyyy-MMM-dd HH:mm" )
28
+
29
+ fun RecentWorkspaceConnection.toWorkspaceParams (): Map <String , String > {
30
+ val map = mutableMapOf (
31
+ TYPE to VALUE_FOR_TYPE ,
32
+ CODER_WORKSPACE_HOSTNAME to " ${this .coderWorkspaceHostname} " ,
33
+ PROJECT_PATH to this .projectPath!! ,
34
+ IDE_PRODUCT_CODE to IntelliJPlatformProduct .fromProductCode(this .ideProductCode!! )!! .productCode,
35
+ IDE_BUILD_NUMBER to " ${this .ideBuildNumber} " ,
36
+ WEB_TERMINAL_LINK to " ${this .webTerminalLink} "
37
+ )
38
+
39
+ if (! this .downloadSource.isNullOrBlank()) {
40
+ map[IDE_DOWNLOAD_LINK ] = this .downloadSource!!
41
+ } else {
42
+ map[IDE_PATH_ON_HOST ] = this .idePathOnHost!!
43
+ }
44
+ return map
45
+ }
46
+
47
+ fun IdeWithStatus.toWorkspaceParams (): Map <String , String > {
48
+ val workspaceParams = mutableMapOf (
49
+ TYPE to VALUE_FOR_TYPE ,
50
+ IDE_PRODUCT_CODE to this .product.productCode,
51
+ IDE_BUILD_NUMBER to this .buildNumber
52
+ )
53
+
54
+ if (this .download != null ) {
55
+ workspaceParams[IDE_DOWNLOAD_LINK ] = this .download!! .link
56
+ }
57
+
58
+ if (! this .pathOnHost.isNullOrBlank()) {
59
+ workspaceParams[IDE_PATH_ON_HOST ] = this .pathOnHost!!
60
+ }
61
+
62
+ return workspaceParams
63
+ }
64
+
65
+ fun Map <String , String >.withWorkspaceHostname (hostname : String ): Map <String , String > {
66
+ val map = this .toMutableMap()
67
+ map[CODER_WORKSPACE_HOSTNAME ] = hostname
68
+ return map
69
+ }
70
+
71
+ fun Map <String , String >.withProjectPath (projectPath : String ): Map <String , String > {
72
+ val map = this .toMutableMap()
73
+ map[PROJECT_PATH ] = projectPath
74
+ return map
75
+ }
76
+
77
+ fun Map <String , String >.withWebTerminalLink (webTerminalLink : String ): Map <String , String > {
78
+ val map = this .toMutableMap()
79
+ map[WEB_TERMINAL_LINK ] = webTerminalLink
80
+ return map
81
+ }
82
+
83
+ fun Map <String , String >.areCoderType (): Boolean {
84
+ return this [TYPE ] == VALUE_FOR_TYPE && ! this [CODER_WORKSPACE_HOSTNAME ].isNullOrBlank() && ! this [PROJECT_PATH ].isNullOrBlank()
85
+ }
86
+
87
+ fun Map <String , String >.toSshConfig (): SshConfig {
88
+ return SshConfig (true ).apply {
89
+ setHost(this @toSshConfig.workspaceHostname())
90
+ setUsername(" coder" )
91
+ port = 22
92
+ authType = AuthType .OPEN_SSH
93
+ }
94
+ }
95
+
96
+ suspend fun Map <String , String >.toHostDeployInputs (): HostDeployInputs {
97
+ return HostDeployInputs .FullySpecified (
98
+ remoteProjectPath = this [PROJECT_PATH ]!! ,
99
+ deployTarget = this .toDeployTargetInfo(),
100
+ remoteInfo = HostDeployInputs .WithDeployedWorker (
101
+ HighLevelHostAccessor .create(
102
+ RemoteCredentialsHolder ().apply {
103
+ setHost(this @toHostDeployInputs.workspaceHostname())
104
+ userName = " coder"
105
+ port = 22
106
+ authType = AuthType .OPEN_SSH
107
+ },
108
+ true
109
+ ),
110
+ HostDeployInputs .WithHostInfo (this .toSshConfig())
111
+ )
112
+ )
113
+ }
114
+
115
+ private fun Map <String , String >.toIdeInfo (): IdeInfo {
116
+ return IdeInfo (
117
+ product = IntelliJPlatformProduct .fromProductCode(this [IDE_PRODUCT_CODE ]!! )!! ,
118
+ buildNumber = this [IDE_BUILD_NUMBER ]!!
119
+ )
120
+ }
121
+
122
+ private fun Map <String , String >.toDeployTargetInfo (): DeployTargetInfo {
123
+ return if (! this [IDE_DOWNLOAD_LINK ].isNullOrBlank()) DeployTargetInfo .DeployWithDownload (
124
+ URI (this [IDE_DOWNLOAD_LINK ]),
125
+ null ,
126
+ this .toIdeInfo()
127
+ )
128
+ else DeployTargetInfo .NoDeploy (this [IDE_PATH_ON_HOST ]!! , this .toIdeInfo())
129
+ }
130
+
131
+ private fun Map <String , String >.workspaceHostname () = this [CODER_WORKSPACE_HOSTNAME ]!!
132
+ private fun Map <String , String >.projectPath () = this [PROJECT_PATH ]!!
133
+
134
+ fun Map <String , String >.toRecentWorkspaceConnection (): RecentWorkspaceConnection {
135
+ return if (! this [IDE_DOWNLOAD_LINK ].isNullOrBlank()) RecentWorkspaceConnection (
136
+ this .workspaceHostname(),
137
+ this .projectPath(),
138
+ localTimeFormatter.format(LocalDateTime .now()),
139
+ this [IDE_PRODUCT_CODE ]!! ,
140
+ this [IDE_BUILD_NUMBER ]!! ,
141
+ this [IDE_DOWNLOAD_LINK ]!! ,
142
+ null ,
143
+ this [WEB_TERMINAL_LINK ]!!
144
+ ) else RecentWorkspaceConnection (
145
+ this .workspaceHostname(),
146
+ this .projectPath(),
147
+ localTimeFormatter.format(LocalDateTime .now()),
148
+ this [IDE_PRODUCT_CODE ]!! ,
149
+ this [IDE_BUILD_NUMBER ]!! ,
150
+ null ,
151
+ this [IDE_PATH_ON_HOST ],
152
+ this [WEB_TERMINAL_LINK ]!!
153
+ )
154
+ }
0 commit comments