Path: blob/main/components/local-app/cmd/workspace-list.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"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/config"14"github.com/gitpod-io/local-app/pkg/helper"15"github.com/gitpod-io/local-app/pkg/prettyprint"16"github.com/sagikazarmark/slog-shim"17"github.com/spf13/cobra"18)1920// workspaceListCmd lists all available workspaces21var workspaceListCmd = &cobra.Command{22Use: "list",23Short: "Lists workspaces",24Aliases: []string{"ls"},25RunE: func(cmd *cobra.Command, args []string) error {26cmd.SilenceUsage = true2728ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)29defer cancel()3031gitpod, err := getGitpodClient(ctx)32if err != nil {33return err34}3536cfg := config.FromContext(ctx)37gpctx, err := cfg.GetActiveContext()38if err != nil {39return err40}41orgId := gpctx.OrganizationID4243workspaces, err := gitpod.Workspaces.ListWorkspaces(ctx, connect.NewRequest(&v1.ListWorkspacesRequest{44OrganizationId: orgId,45}))46if err != nil {47return err48}4950result := make([]tabularWorkspace, 0, len(workspaces.Msg.GetResult()))51for _, ws := range workspaces.Msg.GetResult() {52r := newTabularWorkspace(ws)53if r == nil {54continue55}56if workspaceListOpts.RunningOnly && ws.Status.Instance.Status.Phase != v1.WorkspaceInstanceStatus_PHASE_RUNNING {57continue58}59result = append(result, *r)60}6162return WriteTabular(result, workspaceListOpts.Format, prettyprint.WriterFormatWide)63},64}6566func newTabularWorkspace(ws *v1.Workspace) *tabularWorkspace {67if !helper.HasInstanceStatus(ws) {68slog.Debug("workspace has no instance status - removing from output", "workspace", ws.WorkspaceId)69return nil70}7172var repo string73wsDetails := ws.Context.GetDetails()74switch d := wsDetails.(type) {75case *v1.WorkspaceContext_Git_:76repo = fmt.Sprintf("%s/%s", d.Git.Repository.Owner, d.Git.Repository.Name)77case *v1.WorkspaceContext_Prebuild_:78repo = fmt.Sprintf("%s/%s", d.Prebuild.OriginalContext.Repository.Owner, d.Prebuild.OriginalContext.Repository.Name)79}80var branch string81if ws.Status.Instance.Status.GitStatus != nil {82branch = ws.Status.Instance.Status.GitStatus.Branch83if branch == "" || branch == "(detached)" {84branch = ""85}86}87return &tabularWorkspace{88ID: ws.WorkspaceId,89Repository: repo,90Branch: branch,91Status: prettyprint.FormatWorkspacePhase(ws.Status.Instance.Status.Phase),92}93}9495type tabularWorkspace struct {96ID string `print:"id"`97Repository string `print:"repository"`98Branch string `print:"branch"`99Status string `print:"status"`100}101102var workspaceListOpts struct {103Format formatOpts104RunningOnly bool105}106107func init() {108workspaceCmd.AddCommand(workspaceListCmd)109addFormatFlags(workspaceListCmd, &workspaceListOpts.Format)110workspaceListCmd.Flags().BoolVarP(&workspaceListOpts.RunningOnly, "running-only", "r", false, "Only list running workspaces")111}112113114