Skip to content

Commit

Permalink
ws start --open
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek committed Oct 30, 2023
1 parent caa71dd commit 7d935db
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components/local-app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var rootCmd = &cobra.Command{

func Execute() {
if err := rootCmd.Execute(); err != nil {
slog.Error("An error occurred", err)
slog.Error("An error occurred", "error", err)
os.Exit(1)
}
}
Expand Down
6 changes: 4 additions & 2 deletions components/local-app/cmd/workspace-start.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ var startWorkspaceCommand = &cobra.Command{
if msg.GetResult().Instance.Status.Phase == v1.WorkspaceInstanceStatus_PHASE_RUNNING {
fmt.Println("Workspace started")
if startOpenSsh {
err = common.SshConnectToWs(ctx, workspaceID, false)
return err
return common.SshConnectToWs(ctx, workspaceID, false)
}
if startOpenEditor {
return common.OpenWsInPreferredEditor(ctx, workspaceID)
}
break
}
Expand Down
89 changes: 89 additions & 0 deletions components/local-app/pkg/common/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ package common

import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"strings"

"github.com/bufbuild/connect-go"
Expand Down Expand Up @@ -62,3 +67,87 @@ func SshConnectToWs(ctx context.Context, workspaceID string, runDry bool) error

return nil
}

type Response struct {
OK bool `json:"ok"`
Desktop Desktop `json:"desktop"`
}

type Desktop struct {
Link string `json:"link"`
Label string `json:"label"`
ClientID string `json:"clientID"`
Kind string `json:"kind"`
}

func OpenWsInPreferredEditor(ctx context.Context, workspaceID string) error {
gitpod, err := GetGitpodClient(ctx)
if err != nil {
return err
}

workspace, err := gitpod.Workspaces.GetWorkspace(ctx, connect.NewRequest(&v1.GetWorkspaceRequest{WorkspaceId: workspaceID}))
if err != nil {
return err
}

wsUrl, err := url.Parse(workspace.Msg.Result.Status.Instance.Status.Url)
if err != nil {
return err
}

wsHost := wsUrl.Host

u := url.URL{
Scheme: "https",
Host: wsHost,
Path: "_supervisor/v1/status/ide/wait/true",
}

fmt.Println((u.String()))

resp, err := http.Get(u.String())
if err != nil {
return err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

var response Response
if err := json.Unmarshal(body, &response); err != nil {
return err
}

fmt.Printf("%+v\n", response)

if response.OK {
if response.Desktop.Link == "" {
return fmt.Errorf("failed to open workspace in editor (no desktop editor)")
}
url := response.Desktop.Link
var cmd *exec.Cmd
switch os := runtime.GOOS; os {
case "darwin":
cmd = exec.Command("open", url)
case "linux":
cmd = exec.Command("xdg-open", url)
case "windows":
cmd = exec.Command("cmd", "/c", "start", url)
default:
panic("unsupported platform")
}

err := cmd.Start()
if err != nil {
return fmt.Errorf("failed to open workspace in editor: %w", err)
}
} else {
return fmt.Errorf("failed to open workspace in editor (workspace not ready yet)")
}

return nil
}

0 comments on commit 7d935db

Please sign in to comment.