-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathports-list.go
92 lines (77 loc) · 2.53 KB
/
ports-list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package cmd
import (
"context"
"fmt"
"os"
"time"
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
"github.com/spf13/cobra"
"github.com/olekukonko/tablewriter"
)
var listPortsCmd = &cobra.Command{
Use: "list",
Short: "Lists the workspace ports and their states.",
Run: func(*cobra.Command, []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ports, portsListError := supervisor_helper.GetPortsList(ctx)
if portsListError != nil {
utils.LogError(ctx, portsListError, "Could not get the ports list.")
return
}
if len(ports) == 0 {
fmt.Println("No ports detected.")
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Port", "Status", "URL", "Name & Description"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
for _, port := range ports {
status := "not served"
statusColor := tablewriter.FgHiBlackColor
if port.Exposed == nil && port.Tunneled == nil {
if port.AutoExposure == supervisor.PortAutoExposure_failed {
status = "failed to expose"
statusColor = tablewriter.FgRedColor
} else {
status = "detecting..."
statusColor = tablewriter.FgYellowColor
}
} else if port.Served {
status = "open (" + port.Exposed.Visibility.String() + ")"
if port.Exposed.Visibility == supervisor.PortVisibility_public {
statusColor = tablewriter.FgHiGreenColor
} else {
statusColor = tablewriter.FgHiCyanColor
}
}
nameAndDescription := port.Name
if len(port.Description) > 0 {
if len(nameAndDescription) > 0 {
nameAndDescription = fmt.Sprint(nameAndDescription, ": ", port.Description)
} else {
nameAndDescription = port.Description
}
}
colors := []tablewriter.Colors{}
if !noColor && utils.ColorsEnabled() {
colors = []tablewriter.Colors{{}, {statusColor}, {}, {}}
}
table.Rich(
[]string{fmt.Sprint(port.LocalPort), status, port.Exposed.Url, nameAndDescription},
colors,
)
}
table.Render()
},
}
func init() {
listPortsCmd.Flags().BoolVarP(&noColor, "no-color", "", false, "Disable output colorization")
portsCmd.AddCommand(listPortsCmd)
}