Skip to content

Commit ac09196

Browse files
committed
[jb-remote] gp cli support
1 parent c526268 commit ac09196

File tree

12 files changed

+961
-1
lines changed

12 files changed

+961
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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["wait"]) ?: "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+
}

components/ide/jetbrains/backend-plugin/src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<applicationService serviceImplementation="io.gitpod.jetbrains.remote.services.HeartbeatService" preload="true"/>
2121
<applicationService serviceImplementation="io.gitpod.jetbrains.remote.GitpodManager" preload="true"/>
2222
<notificationGroup id="Gitpod Notifications" displayType="STICKY_BALLOON" />
23+
<httpRequestHandler implementation="io.gitpod.jetbrains.remote.GitpodCLIService"/>
2324
</extensions>
2425

2526
</idea-plugin>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packages:
2+
- name: app
3+
type: go
4+
srcs:
5+
- "**/*.go"
6+
- "go.mod"
7+
- "go.sum"
8+
env:
9+
- CGO_ENABLED=0
10+
- GOOS=linux
11+
config:
12+
packaging: app
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 cmd
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"net/http"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var wait bool
16+
17+
var openCmd = &cobra.Command{
18+
Use: "open",
19+
Args: cobra.ExactArgs(1),
20+
Run: func(cmd *cobra.Command, args []string) {
21+
file := args[0]
22+
resp, err := http.Get(fmt.Sprintf("http://localhost:63342/api/gitpod/cli/op=open&file=%swait=%t", file, wait))
23+
if err != nil {
24+
log.Fatalf("%s", err)
25+
}
26+
if resp.StatusCode != http.StatusOK {
27+
log.Fatalf("%s", resp.Status)
28+
}
29+
},
30+
}
31+
32+
func init() {
33+
rootCmd.AddCommand(openCmd)
34+
openCmd.Flags().BoolVar(&wait, "wait", false, "")
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 cmd
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"net/http"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var previewCmd = &cobra.Command{
16+
Use: "preview",
17+
Run: func(cmd *cobra.Command, args []string) {
18+
url := args[0]
19+
resp, err := http.Get(fmt.Sprintf("http://localhost:63342/api/gitpod/cli/op=preview&url=%s", url))
20+
if err != nil {
21+
log.Fatalf("%s", err)
22+
}
23+
if resp.StatusCode != http.StatusOK {
24+
log.Fatalf("%s", resp.Status)
25+
}
26+
},
27+
}
28+
29+
func init() {
30+
rootCmd.AddCommand(previewCmd)
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 cmd
6+
7+
import (
8+
"os"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var rootCmd = &cobra.Command{
14+
Use: "idea-cli",
15+
}
16+
17+
func Execute() {
18+
err := rootCmd.Execute()
19+
if err != nil {
20+
os.Exit(1)
21+
}
22+
}
23+
24+
func init() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module idea-cli
2+
3+
go 1.17
4+
5+
require (
6+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
7+
github.com/spf13/cobra v1.3.0 // indirect
8+
github.com/spf13/pflag v1.0.5 // indirect
9+
)

0 commit comments

Comments
 (0)