Path: blob/main/components/gitpod-cli/cmd/tasks-list.go
2498 views
// Copyright (c) 2022 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"os"10"time"1112"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"13"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"14"github.com/gitpod-io/gitpod/supervisor/api"15"github.com/spf13/cobra"16"golang.org/x/xerrors"1718"github.com/olekukonko/tablewriter"19)2021// listTasksCmd represents the tasks list command22var listTasksCmd = &cobra.Command{23Use: "list",24Short: "Lists the workspace tasks and their state",25RunE: func(cmd *cobra.Command, args []string) error {26ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)27defer cancel()2829client, err := supervisor.New(ctx)30if err != nil {31return xerrors.Errorf("cannot get task list: %w", err)32}33defer client.Close()3435tasks, err := client.GetTasksList(ctx)36if err != nil {37return xerrors.Errorf("cannot get task list: %w", err)38}3940if len(tasks) == 0 {41fmt.Println("No tasks detected")42return nil43}4445table := tablewriter.NewWriter(os.Stdout)46table.SetHeader([]string{"Terminal ID", "Name", "State"})47table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})48table.SetCenterSeparator("|")4950mapStatusToColor := map[api.TaskState]int{510: tablewriter.FgHiGreenColor,521: tablewriter.FgHiGreenColor,532: tablewriter.FgHiBlackColor,54}5556mapCurrentToColor := map[bool]int{57false: tablewriter.FgWhiteColor,58true: tablewriter.FgHiGreenColor,59}6061ppid := int64(os.Getppid())6263for _, task := range tasks {64colors := []tablewriter.Colors{}6566isCurrent := false6768if task.State == api.TaskState_running {69terminal, err := client.Terminal.Get(ctx, &api.GetTerminalRequest{Alias: task.Terminal})70if err != nil {71panic(err)72}7374if ppid == terminal.Pid {75isCurrent = true76}77}7879if !noColor && utils.ColorsEnabled() {80colors = []tablewriter.Colors{{mapCurrentToColor[isCurrent]}, {}, {mapStatusToColor[task.State]}}81}8283table.Rich([]string{task.Terminal, task.Presentation.Name, task.State.String()}, colors)84}8586table.Render()87return nil88},89}9091func init() {92listTasksCmd.Flags().BoolVarP(&noColor, "no-color", "", false, "Disable output colorization")93tasksCmd.AddCommand(listTasksCmd)94}959697