Path: blob/main/components/local-app/cmd/workspace-open.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"fmt"9"log/slog"10"time"1112"github.com/bufbuild/connect-go"13v1 "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)1718var workspaceOpenOpts struct {19NoImplicitStart bool20}2122// workspaceOpenCmd opens a given workspace in its pre-configured editor23var workspaceOpenCmd = &cobra.Command{24Use: "open <workspace-id>",25Short: "Opens a given workspace in its pre-configured editor",26Args: cobra.ExactArgs(1),27RunE: func(cmd *cobra.Command, args []string) error {28cmd.SilenceUsage = true2930ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Minute)31defer cancel()3233workspaceID := args[0]3435gitpod, err := getGitpodClient(cmd.Context())36if err != nil {37return err38}3940ws, err := gitpod.Workspaces.GetWorkspace(ctx, connect.NewRequest(&v1.GetWorkspaceRequest{WorkspaceId: workspaceID}))41if err != nil {42return err43}4445if ws.Msg.Result.Status.Instance.Status.Phase != v1.WorkspaceInstanceStatus_PHASE_RUNNING {46if workspaceOpenOpts.NoImplicitStart {47return fmt.Errorf("workspace is not running")48}49slog.Info("workspace is not running, starting it...")50_, err := gitpod.Workspaces.StartWorkspace(ctx, connect.NewRequest(&v1.StartWorkspaceRequest{WorkspaceId: workspaceID}))51if err != nil {52return err53}54_, err = helper.ObserveWorkspaceUntilStarted(ctx, gitpod, workspaceID)55if err != nil {56return err57}58}5960slog.Debug("attempting to open workspace...")61return helper.OpenWorkspaceInPreferredEditor(cmd.Context(), gitpod, workspaceID)62},63}6465func init() {66workspaceCmd.AddCommand(workspaceOpenCmd)67workspaceOpenCmd.Flags().BoolVarP(&workspaceOpenOpts.NoImplicitStart, "no-implicit-start", "", false, "Do not start the workspace if it is not running")68}697071