Path: blob/main/components/local-app/cmd/workspace-start.go
2497 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"context"8"log/slog"9"time"1011"github.com/bufbuild/connect-go"12v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"13"github.com/gitpod-io/local-app/pkg/helper"14"github.com/spf13/cobra"15)1617// workspaceStartCmd starts to a given workspace18var workspaceStartCmd = &cobra.Command{19Use: "start <workspace-id>",20Short: "Start a given workspace",21Args: cobra.ExactArgs(1),22RunE: func(cmd *cobra.Command, args []string) error {23cmd.SilenceUsage = true2425workspaceID := args[0]2627ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute)28defer cancel()2930gitpod, err := getGitpodClient(ctx)31if err != nil {32return err33}3435slog.Info("starting workspace...")36wsInfo, err := gitpod.Workspaces.StartWorkspace(ctx, connect.NewRequest(&v1.StartWorkspaceRequest{WorkspaceId: workspaceID}))37if err != nil {38return err39}4041if wsInfo.Msg.GetResult().Status.Instance.Status.Phase == v1.WorkspaceInstanceStatus_PHASE_RUNNING {42slog.Info("workspace already running")43return nil44}4546if workspaceStartOpts.DontWait {47slog.Info("workspace initialization started")48return nil49}5051_, err = helper.ObserveWorkspaceUntilStarted(ctx, gitpod, workspaceID)52if err != nil {53return err54}5556switch {57case workspaceStartOpts.OpenSSH:58return helper.SSHConnectToWorkspace(ctx, gitpod, workspaceID, false)59case workspaceStartOpts.OpenEditor:60return helper.OpenWorkspaceInPreferredEditor(ctx, gitpod, workspaceID)61}6263return nil64},65}6667type workspaceStartOptions struct {68DontWait bool69OpenSSH bool70OpenEditor bool71}7273func addWorkspaceStartOptions(cmd *cobra.Command, opts *workspaceStartOptions) {74cmd.Flags().BoolVar(&opts.DontWait, "dont-wait", false, "do not wait for workspace to fully start, only initialize")75cmd.Flags().BoolVar(&opts.OpenSSH, "ssh", false, "open an SSH connection to workspace after starting")76cmd.Flags().BoolVar(&opts.OpenEditor, "open", false, "open the workspace in an editor after starting")7778cmd.MarkFlagsMutuallyExclusive("ssh", "open")79}8081var workspaceStartOpts workspaceStartOptions8283func init() {84workspaceCmd.AddCommand(workspaceStartCmd)85addWorkspaceStartOptions(workspaceStartCmd, &workspaceStartOpts)86}878889