Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/workspace-open.go
2497 views
1
// Copyright (c) 2023 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
"context"
9
"fmt"
10
"log/slog"
11
"time"
12
13
"github.com/bufbuild/connect-go"
14
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
15
"github.com/gitpod-io/local-app/pkg/helper"
16
"github.com/spf13/cobra"
17
)
18
19
var workspaceOpenOpts struct {
20
NoImplicitStart bool
21
}
22
23
// workspaceOpenCmd opens a given workspace in its pre-configured editor
24
var workspaceOpenCmd = &cobra.Command{
25
Use: "open <workspace-id>",
26
Short: "Opens a given workspace in its pre-configured editor",
27
Args: cobra.ExactArgs(1),
28
RunE: func(cmd *cobra.Command, args []string) error {
29
cmd.SilenceUsage = true
30
31
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute)
32
defer cancel()
33
34
workspaceID := args[0]
35
36
gitpod, err := getGitpodClient(cmd.Context())
37
if err != nil {
38
return err
39
}
40
41
ws, err := gitpod.Workspaces.GetWorkspace(ctx, connect.NewRequest(&v1.GetWorkspaceRequest{WorkspaceId: workspaceID}))
42
if err != nil {
43
return err
44
}
45
46
if ws.Msg.Result.Status.Instance.Status.Phase != v1.WorkspaceInstanceStatus_PHASE_RUNNING {
47
if workspaceOpenOpts.NoImplicitStart {
48
return fmt.Errorf("workspace is not running")
49
}
50
slog.Info("workspace is not running, starting it...")
51
_, err := gitpod.Workspaces.StartWorkspace(ctx, connect.NewRequest(&v1.StartWorkspaceRequest{WorkspaceId: workspaceID}))
52
if err != nil {
53
return err
54
}
55
_, err = helper.ObserveWorkspaceUntilStarted(ctx, gitpod, workspaceID)
56
if err != nil {
57
return err
58
}
59
}
60
61
slog.Debug("attempting to open workspace...")
62
return helper.OpenWorkspaceInPreferredEditor(cmd.Context(), gitpod, workspaceID)
63
},
64
}
65
66
func init() {
67
workspaceCmd.AddCommand(workspaceOpenCmd)
68
workspaceOpenCmd.Flags().BoolVarP(&workspaceOpenOpts.NoImplicitStart, "no-implicit-start", "", false, "Do not start the workspace if it is not running")
69
}
70
71