@@ -7,19 +7,15 @@ import (
7
7
"context"
8
8
"errors"
9
9
"fmt"
10
- "io"
11
- "os/exec"
12
- "path"
13
10
14
11
"github.com/daytonaio/daytona/cmd/daytona/config"
15
- "github.com/daytonaio/daytona/internal/util "
12
+ "github.com/daytonaio/daytona/internal/jetbrains "
16
13
"github.com/daytonaio/daytona/internal/util/apiclient"
17
14
"github.com/daytonaio/daytona/internal/util/apiclient/server"
18
- "github.com/daytonaio/daytona/pkg/ports "
15
+ "github.com/daytonaio/daytona/pkg/ide "
19
16
view_util "github.com/daytonaio/daytona/pkg/views/util"
20
17
"github.com/daytonaio/daytona/pkg/views/workspace/selection"
21
18
22
- "github.com/pkg/browser"
23
19
log "github.com/sirupsen/logrus"
24
20
"github.com/spf13/cobra"
25
21
)
@@ -92,7 +88,7 @@ var CodeCmd = &cobra.Command{
92
88
93
89
view_util .RenderInfoMessage (fmt .Sprintf ("Opening the workspace project '%s' in your preferred IDE." , projectName ))
94
90
95
- openIDE (ideId , activeProfile , workspaceId , projectName )
91
+ log . Fatal ( openIDE (ideId , activeProfile , workspaceId , projectName ) )
96
92
},
97
93
}
98
94
@@ -122,115 +118,20 @@ func selectWorkspaceProject(workspaceId string, profile *config.Profile) (*strin
122
118
return nil , errors .New ("no projects found in workspace" )
123
119
}
124
120
125
- func openIDE (ideId string , activeProfile config.Profile , workspaceId string , projectName string ) {
126
- if ideId == "browser" {
127
- err := openBrowserIDE (activeProfile , workspaceId , projectName )
128
- if err != nil {
129
- log .Fatal (err )
121
+ func openIDE (ideId string , activeProfile config.Profile , workspaceId string , projectName string ) error {
122
+ switch ideId {
123
+ case "vscode" :
124
+ return ide .OpenVSCode (activeProfile , workspaceId , projectName )
125
+ case "browser" :
126
+ return ide .OpenBrowserIDE (activeProfile , workspaceId , projectName )
127
+ default :
128
+ _ , ok := jetbrains .GetIdes ()[jetbrains .Id (ideId )]
129
+ if ok {
130
+ return ide .OpenJetbrainsIDE (activeProfile , ideId , workspaceId , projectName )
130
131
}
131
- return
132
- }
133
-
134
- openVSCode (activeProfile , workspaceId , projectName )
135
- }
136
-
137
- func openVSCode (activeProfile config.Profile , workspaceId string , projectName string ) {
138
- err := config .EnsureSshConfigEntryAdded (activeProfile .Id , workspaceId , projectName )
139
- if err != nil {
140
- log .Fatal (err )
141
132
}
142
133
143
- checkAndAlertVSCodeInstalled ()
144
-
145
- projectHostname := config .GetProjectHostname (activeProfile .Id , workspaceId , projectName )
146
-
147
- commandArgument := fmt .Sprintf ("vscode-remote://ssh-remote+%s/%s" , projectHostname , path .Join ("/workspaces" , projectName ))
148
-
149
- var vscCommand * exec.Cmd = exec .Command ("code" , "--folder-uri" , commandArgument )
150
-
151
- err = vscCommand .Run ()
152
-
153
- if err != nil {
154
- log .Fatal (err .Error ())
155
- }
156
- }
157
-
158
- func openBrowserIDE (activeProfile config.Profile , workspaceId string , projectName string ) error {
159
- // Download and start IDE
160
- err := config .EnsureSshConfigEntryAdded (activeProfile .Id , workspaceId , projectName )
161
- if err != nil {
162
- return err
163
- }
164
-
165
- view_util .RenderInfoMessageBold ("Downloading OpenVSCode Server..." )
166
- projectHostname := config .GetProjectHostname (activeProfile .Id , workspaceId , projectName )
167
-
168
- installServerCommand := exec .Command ("ssh" , projectHostname , "curl -fsSL https://download.daytona.io/daytona/get-openvscode-server.sh | sh" )
169
- installServerCommand .Stdout = io .Writer (& util.DebugLogWriter {})
170
- installServerCommand .Stderr = io .Writer (& util.DebugLogWriter {})
171
-
172
- err = installServerCommand .Run ()
173
- if err != nil {
174
- return err
175
- }
176
-
177
- view_util .RenderInfoMessageBold ("Starting OpenVSCode Server..." )
178
-
179
- go func () {
180
- startServerCommand := exec .CommandContext (context .Background (), "ssh" , projectHostname , startVSCodeServerCommand )
181
- startServerCommand .Stdout = io .Writer (& util.DebugLogWriter {})
182
- startServerCommand .Stderr = io .Writer (& util.DebugLogWriter {})
183
-
184
- err = startServerCommand .Run ()
185
- if err != nil {
186
- log .Fatal (err )
187
- }
188
- }()
189
-
190
- // Forward IDE port
191
- browserPort , errChan := ports .ForwardPort (workspaceId , projectName , 63000 )
192
- if browserPort == nil {
193
- if err := <- errChan ; err != nil {
194
- return err
195
- }
196
- }
197
-
198
- view_util .RenderInfoMessageBold (fmt .Sprintf ("Forwarded %s IDE port to %d.\n Opening browser..." , projectName , * browserPort ))
199
-
200
- err = browser .OpenURL (fmt .Sprintf ("http://localhost:%d" , * browserPort ))
201
- if err != nil {
202
- log .Error ("Error opening URL: " + err .Error ())
203
- }
204
-
205
- for {
206
- err := <- errChan
207
- if err != nil {
208
- // Log only in debug mode
209
- // Connection errors to the forwarded port should not exit the process
210
- log .Debug (err )
211
- }
212
- }
213
- }
214
-
215
- const startVSCodeServerCommand = "$HOME/vscode-server/bin/openvscode-server --start-server --port=63000 --host=0.0.0.0 --without-connection-token --disable-workspace-trust --default-folder=$DAYTONA_WS_DIR"
216
-
217
- func checkAndAlertVSCodeInstalled () {
218
- if err := isVSCodeInstalled (); err != nil {
219
- redBold := "\033 [1;31m" // ANSI escape code for red and bold
220
- reset := "\033 [0m" // ANSI escape code to reset text formatting
221
-
222
- errorMessage := "Please install Visual Studio Code and ensure it's in your PATH. "
223
- infoMessage := "More information on: 'https://code.visualstudio.com/docs/editor/command-line#_launching-from-command-line'"
224
-
225
- log .Error (redBold + errorMessage + reset + infoMessage )
226
-
227
- return
228
- }
229
- }
230
-
231
- func isVSCodeInstalled () error {
232
- _ , err := exec .LookPath ("code" )
233
- return err
134
+ return errors .New ("invalid IDE" )
234
135
}
235
136
236
137
var ideFlag string
0 commit comments