Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/cmd/workspace-start.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
"log/slog"
10
"time"
11
12
"github.com/bufbuild/connect-go"
13
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
14
"github.com/gitpod-io/local-app/pkg/helper"
15
"github.com/spf13/cobra"
16
)
17
18
// workspaceStartCmd starts to a given workspace
19
var workspaceStartCmd = &cobra.Command{
20
Use: "start <workspace-id>",
21
Short: "Start a given workspace",
22
Args: cobra.ExactArgs(1),
23
RunE: func(cmd *cobra.Command, args []string) error {
24
cmd.SilenceUsage = true
25
26
workspaceID := args[0]
27
28
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute)
29
defer cancel()
30
31
gitpod, err := getGitpodClient(ctx)
32
if err != nil {
33
return err
34
}
35
36
slog.Info("starting workspace...")
37
wsInfo, err := gitpod.Workspaces.StartWorkspace(ctx, connect.NewRequest(&v1.StartWorkspaceRequest{WorkspaceId: workspaceID}))
38
if err != nil {
39
return err
40
}
41
42
if wsInfo.Msg.GetResult().Status.Instance.Status.Phase == v1.WorkspaceInstanceStatus_PHASE_RUNNING {
43
slog.Info("workspace already running")
44
return nil
45
}
46
47
if workspaceStartOpts.DontWait {
48
slog.Info("workspace initialization started")
49
return nil
50
}
51
52
_, err = helper.ObserveWorkspaceUntilStarted(ctx, gitpod, workspaceID)
53
if err != nil {
54
return err
55
}
56
57
switch {
58
case workspaceStartOpts.OpenSSH:
59
return helper.SSHConnectToWorkspace(ctx, gitpod, workspaceID, false)
60
case workspaceStartOpts.OpenEditor:
61
return helper.OpenWorkspaceInPreferredEditor(ctx, gitpod, workspaceID)
62
}
63
64
return nil
65
},
66
}
67
68
type workspaceStartOptions struct {
69
DontWait bool
70
OpenSSH bool
71
OpenEditor bool
72
}
73
74
func addWorkspaceStartOptions(cmd *cobra.Command, opts *workspaceStartOptions) {
75
cmd.Flags().BoolVar(&opts.DontWait, "dont-wait", false, "do not wait for workspace to fully start, only initialize")
76
cmd.Flags().BoolVar(&opts.OpenSSH, "ssh", false, "open an SSH connection to workspace after starting")
77
cmd.Flags().BoolVar(&opts.OpenEditor, "open", false, "open the workspace in an editor after starting")
78
79
cmd.MarkFlagsMutuallyExclusive("ssh", "open")
80
}
81
82
var workspaceStartOpts workspaceStartOptions
83
84
func init() {
85
workspaceCmd.AddCommand(workspaceStartCmd)
86
addWorkspaceStartOptions(workspaceStartCmd, &workspaceStartOpts)
87
}
88
89