Skip to content

Commit 0723994

Browse files
felladrinroboquat
authored andcommitted
Add command 'gp ports list' to gitpod-cli
1 parent 109c45d commit 0723994

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

components/gitpod-cli/cmd/ports.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
"github.com/gitpod-io/gitpod/gitpod-cli/cmd/ports"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var portsCmd = &cobra.Command{
13+
Use: "ports",
14+
Short: "Interact with workspace ports.",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
if len(args) == 0 {
17+
_ = cmd.Help()
18+
}
19+
},
20+
}
21+
22+
var listPortsCmd = &cobra.Command{
23+
Use: "list",
24+
Short: "Lists the workspace ports and their states.",
25+
Run: ports.ListPortsCmd,
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(portsCmd)
30+
portsCmd.AddCommand(listPortsCmd)
31+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 ports
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"os"
11+
"sort"
12+
"time"
13+
14+
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
15+
"github.com/gitpod-io/gitpod/supervisor/api"
16+
log "github.com/sirupsen/logrus"
17+
"github.com/spf13/cobra"
18+
19+
"github.com/olekukonko/tablewriter"
20+
)
21+
22+
func ListPortsCmd(*cobra.Command, []string) {
23+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
24+
defer cancel()
25+
26+
conn := supervisor.Dial()
27+
client := api.NewStatusServiceClient(conn)
28+
29+
ports, portsListError := supervisor.GetPortsList(ctx, client)
30+
31+
if portsListError != nil {
32+
log.WithError(portsListError).Error("Could not get the ports list.")
33+
return
34+
}
35+
36+
if len(ports) == 0 {
37+
fmt.Println("No ports detected.")
38+
return
39+
}
40+
41+
sort.Slice(ports, func(i, j int) bool {
42+
return int(ports[i].LocalPort) < int(ports[j].LocalPort)
43+
})
44+
45+
table := tablewriter.NewWriter(os.Stdout)
46+
table.SetHeader([]string{"Port", "Status", "URL", "Name & Description"})
47+
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
48+
table.SetCenterSeparator("|")
49+
50+
for _, port := range ports {
51+
status := "not served"
52+
statusColor := tablewriter.FgHiBlackColor
53+
if port.Exposed == nil && port.Tunneled == nil {
54+
if port.AutoExposure == api.PortAutoExposure_failed {
55+
status = "failed to expose"
56+
statusColor = tablewriter.FgRedColor
57+
} else {
58+
status = "detecting..."
59+
statusColor = tablewriter.FgYellowColor
60+
}
61+
} else if port.Served {
62+
status = "open (" + port.Exposed.Visibility.String() + ")"
63+
if port.Exposed.Visibility == api.PortVisibility_public {
64+
statusColor = tablewriter.FgHiGreenColor
65+
} else {
66+
statusColor = tablewriter.FgHiCyanColor
67+
}
68+
}
69+
70+
nameAndDescription := port.Name
71+
if len(port.Description) > 0 {
72+
if len(nameAndDescription) > 0 {
73+
nameAndDescription = fmt.Sprint(nameAndDescription, ": ", port.Description)
74+
} else {
75+
nameAndDescription = port.Description
76+
}
77+
}
78+
79+
table.Rich(
80+
[]string{fmt.Sprint(port.LocalPort), status, port.Exposed.Url, nameAndDescription},
81+
[]tablewriter.Colors{{}, {statusColor}, {}, {}},
82+
)
83+
}
84+
85+
table.Render()
86+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 supervisor
6+
7+
import (
8+
"context"
9+
"github.com/gitpod-io/gitpod/supervisor/api"
10+
)
11+
12+
func GetPortsList(ctx context.Context, client api.StatusServiceClient) ([]*api.PortsStatus, error) {
13+
portsStatusClient, portsStatusClientError := client.PortsStatus(ctx, &api.PortsStatusRequest{Observe: false})
14+
15+
if portsStatusClientError != nil {
16+
return nil, portsStatusClientError
17+
}
18+
19+
portsStatusResponse, portsStatusResponseError := portsStatusClient.Recv()
20+
21+
if portsStatusResponseError != nil {
22+
return nil, portsStatusResponseError
23+
}
24+
25+
return portsStatusResponse.GetPorts(), nil
26+
}

0 commit comments

Comments
 (0)