Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/tasks-list.go
2498 views
1
// Copyright (c) 2022 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
"fmt"
10
"os"
11
"time"
12
13
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
14
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
15
"github.com/gitpod-io/gitpod/supervisor/api"
16
"github.com/spf13/cobra"
17
"golang.org/x/xerrors"
18
19
"github.com/olekukonko/tablewriter"
20
)
21
22
// listTasksCmd represents the tasks list command
23
var listTasksCmd = &cobra.Command{
24
Use: "list",
25
Short: "Lists the workspace tasks and their state",
26
RunE: func(cmd *cobra.Command, args []string) error {
27
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
28
defer cancel()
29
30
client, err := supervisor.New(ctx)
31
if err != nil {
32
return xerrors.Errorf("cannot get task list: %w", err)
33
}
34
defer client.Close()
35
36
tasks, err := client.GetTasksList(ctx)
37
if err != nil {
38
return xerrors.Errorf("cannot get task list: %w", err)
39
}
40
41
if len(tasks) == 0 {
42
fmt.Println("No tasks detected")
43
return nil
44
}
45
46
table := tablewriter.NewWriter(os.Stdout)
47
table.SetHeader([]string{"Terminal ID", "Name", "State"})
48
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
49
table.SetCenterSeparator("|")
50
51
mapStatusToColor := map[api.TaskState]int{
52
0: tablewriter.FgHiGreenColor,
53
1: tablewriter.FgHiGreenColor,
54
2: tablewriter.FgHiBlackColor,
55
}
56
57
mapCurrentToColor := map[bool]int{
58
false: tablewriter.FgWhiteColor,
59
true: tablewriter.FgHiGreenColor,
60
}
61
62
ppid := int64(os.Getppid())
63
64
for _, task := range tasks {
65
colors := []tablewriter.Colors{}
66
67
isCurrent := false
68
69
if task.State == api.TaskState_running {
70
terminal, err := client.Terminal.Get(ctx, &api.GetTerminalRequest{Alias: task.Terminal})
71
if err != nil {
72
panic(err)
73
}
74
75
if ppid == terminal.Pid {
76
isCurrent = true
77
}
78
}
79
80
if !noColor && utils.ColorsEnabled() {
81
colors = []tablewriter.Colors{{mapCurrentToColor[isCurrent]}, {}, {mapStatusToColor[task.State]}}
82
}
83
84
table.Rich([]string{task.Terminal, task.Presentation.Name, task.State.String()}, colors)
85
}
86
87
table.Render()
88
return nil
89
},
90
}
91
92
func init() {
93
listTasksCmd.Flags().BoolVarP(&noColor, "no-color", "", false, "Disable output colorization")
94
tasksCmd.AddCommand(listTasksCmd)
95
}
96
97