Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/terminal-list.go
2498 views
1
// Copyright (c) 2020 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
"sort"
12
"strings"
13
"text/tabwriter"
14
"time"
15
16
"github.com/spf13/cobra"
17
18
"github.com/gitpod-io/gitpod/common-go/log"
19
"github.com/gitpod-io/gitpod/supervisor/api"
20
)
21
22
var terminalListCmd = &cobra.Command{
23
Use: "list",
24
Short: "lists all of supervisor's terminals",
25
Run: func(cmd *cobra.Command, args []string) {
26
client := api.NewTerminalServiceClient(dialSupervisor())
27
28
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
29
defer cancel()
30
31
resp, err := client.List(ctx, &api.ListTerminalsRequest{})
32
if err != nil {
33
log.WithError(err).Fatal("cannot list terminals")
34
}
35
36
tw := tabwriter.NewWriter(os.Stdout, 2, 4, 1, ' ', 0)
37
defer tw.Flush()
38
39
fmt.Fprintf(tw, "ALIAS\tPID\tCOMMAND\tANNOTATIONS\n")
40
for _, term := range resp.Terminals {
41
annotations := make([]string, 0, len(term.Annotations))
42
for k, v := range term.Annotations {
43
annotations = append(annotations, fmt.Sprintf("%s=%s", k, v))
44
}
45
sort.Slice(annotations, func(i, j int) bool { return annotations[i] < annotations[j] })
46
fmt.Fprintf(tw, "%s\t%d\t%s\t%s\n", term.Alias, term.Pid, strings.Join(term.Command, " "), strings.Join(annotations, ","))
47
}
48
},
49
}
50
51
func init() {
52
terminalCmd.AddCommand(terminalListCmd)
53
}
54
55