Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/ports-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
18
"github.com/olekukonko/tablewriter"
19
)
20
21
var listPortsCmd = &cobra.Command{
22
Use: "list",
23
Short: "Lists the workspace ports and their states.",
24
RunE: func(cmd *cobra.Command, args []string) error {
25
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Second)
26
defer cancel()
27
28
client, err := supervisor.New(ctx)
29
if err != nil {
30
return err
31
}
32
defer client.Close()
33
34
ports, err := client.GetPortsList(ctx)
35
if err != nil {
36
return err
37
}
38
39
if len(ports) == 0 {
40
fmt.Println("No ports detected.")
41
return nil
42
}
43
44
table := tablewriter.NewWriter(os.Stdout)
45
table.SetHeader([]string{"Port", "Status", "Protocol", "URL", "Name & Description"})
46
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
47
table.SetCenterSeparator("|")
48
49
for _, port := range ports {
50
status := ""
51
statusColor := tablewriter.FgHiBlackColor
52
accessible := port.Exposed != nil || port.Tunneled != nil
53
54
exposedUrl := ""
55
if port.Exposed != nil {
56
exposedUrl = port.Exposed.Url
57
}
58
59
if !port.Served {
60
status = "not served"
61
} else if !accessible {
62
if port.AutoExposure == api.PortAutoExposure_failed {
63
status = "failed to expose"
64
statusColor = tablewriter.FgRedColor
65
} else {
66
status = "detecting..."
67
statusColor = tablewriter.FgYellowColor
68
}
69
} else if port.Exposed != nil {
70
if port.Exposed.Visibility == api.PortVisibility_public {
71
status = "open (public)"
72
statusColor = tablewriter.FgHiGreenColor
73
}
74
if port.Exposed.Visibility == api.PortVisibility_private {
75
status = "open (private)"
76
statusColor = tablewriter.FgHiCyanColor
77
}
78
} else if port.Tunneled != nil {
79
if port.Tunneled.Visibility == api.TunnelVisiblity(api.TunnelVisiblity_value["network"]) {
80
status = "open on all interfaces"
81
statusColor = tablewriter.FgHiGreenColor
82
}
83
if port.Tunneled.Visibility == api.TunnelVisiblity(api.TunnelVisiblity_value["host"]) {
84
status = "open on localhost"
85
statusColor = tablewriter.FgHiGreenColor
86
}
87
}
88
89
nameAndDescription := port.Name
90
if len(port.Description) > 0 {
91
if len(nameAndDescription) > 0 {
92
nameAndDescription = fmt.Sprint(nameAndDescription, ": ", port.Description)
93
} else {
94
nameAndDescription = port.Description
95
}
96
}
97
98
colors := []tablewriter.Colors{}
99
if !noColor && utils.ColorsEnabled() {
100
colors = []tablewriter.Colors{{}, {statusColor}, {}, {}}
101
}
102
103
table.Rich(
104
[]string{fmt.Sprint(port.LocalPort), status, port.Exposed.Protocol.String(), exposedUrl, nameAndDescription},
105
colors,
106
)
107
}
108
109
table.Render()
110
return nil
111
},
112
}
113
114
func init() {
115
listPortsCmd.Flags().BoolVarP(&noColor, "no-color", "", false, "Disable output colorization")
116
portsCmd.AddCommand(listPortsCmd)
117
}
118
119