1
+ // Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
+ // Licensed under the GNU Affero General Public License (AGPL).
3
+ // See License-AGPL.txt in the project root for license information.
4
+
5
+ package io.gitpod.jetbrains.remote
6
+
7
+ import com.intellij.ide.BrowserUtil
8
+ import com.intellij.ide.CommandLineProcessor
9
+ import com.intellij.openapi.diagnostic.thisLogger
10
+ import com.intellij.openapi.util.io.FileUtilRt
11
+ import com.intellij.util.containers.ContainerUtil
12
+ import io.netty.channel.ChannelHandlerContext
13
+ import io.netty.handler.codec.http.FullHttpRequest
14
+ import io.netty.handler.codec.http.QueryStringDecoder
15
+ import org.jetbrains.ide.RestService
16
+ import java.nio.file.InvalidPathException
17
+ import java.nio.file.Path
18
+
19
+
20
+ class GitpodCLIService : RestService () {
21
+
22
+ override fun getServiceName () = SERVICE_NAME
23
+
24
+ override fun execute (urlDecoder : QueryStringDecoder , request : FullHttpRequest , context : ChannelHandlerContext ): String? {
25
+ val parameters = urlDecoder.parameters()
26
+ val operation = ContainerUtil .getLastItem(parameters[" op" ])
27
+ if (operation == " open" ) {
28
+ val fileStr = ContainerUtil .getLastItem(parameters[" file" ])
29
+ if (fileStr.isNullOrBlank()) {
30
+ return " file is missing"
31
+ }
32
+ val file = parseFilePath(fileStr) ? : return " invalid file"
33
+ val shouldWait = (ContainerUtil .getLastItem(parameters[" shouldWait" ]) ? : " false" ).toBoolean()
34
+
35
+ CommandLineProcessor .doOpenFileOrProject(file, shouldWait).future.get()
36
+ return null
37
+ }
38
+ if (operation == " preview" ) {
39
+ val url = ContainerUtil .getLastItem(parameters[" url" ])
40
+ if (url.isNullOrBlank()) {
41
+ return " url is missing"
42
+ }
43
+ BrowserUtil .browse(url)
44
+ return null
45
+ }
46
+ return " invalid operation"
47
+ }
48
+
49
+ private fun parseFilePath (path : String ): Path ? {
50
+ return try {
51
+ var file: Path = Path .of(FileUtilRt .toSystemDependentName(path)) // handle paths like '/file/foo\qwe'
52
+ if (! file.isAbsolute) {
53
+ file = file.toAbsolutePath()
54
+ }
55
+ file.normalize()
56
+ } catch (e: InvalidPathException ) {
57
+ thisLogger().warn(" gitpod cli: failed to parse file path:" , e)
58
+ null
59
+ }
60
+ }
61
+
62
+ companion object {
63
+ const val SERVICE_NAME = " gitpod/cli"
64
+ }
65
+ }
0 commit comments