Skip to content

Commit

Permalink
Add command 'gp ports list' to gitpod-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
felladrin authored and roboquat committed Jun 1, 2022
1 parent 109c45d commit 0723994
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
31 changes: 31 additions & 0 deletions components/gitpod-cli/cmd/ports.go
@@ -0,0 +1,31 @@
// 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 (
"github.com/gitpod-io/gitpod/gitpod-cli/cmd/ports"
"github.com/spf13/cobra"
)

var portsCmd = &cobra.Command{
Use: "ports",
Short: "Interact with workspace ports.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
}
},
}

var listPortsCmd = &cobra.Command{
Use: "list",
Short: "Lists the workspace ports and their states.",
Run: ports.ListPortsCmd,
}

func init() {
rootCmd.AddCommand(portsCmd)
portsCmd.AddCommand(listPortsCmd)
}
86 changes: 86 additions & 0 deletions components/gitpod-cli/cmd/ports/ports-list.go
@@ -0,0 +1,86 @@
// 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 ports

import (
"context"
"fmt"
"os"
"sort"
"time"

"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
"github.com/gitpod-io/gitpod/supervisor/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/olekukonko/tablewriter"
)

func ListPortsCmd(*cobra.Command, []string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

conn := supervisor.Dial()
client := api.NewStatusServiceClient(conn)

ports, portsListError := supervisor.GetPortsList(ctx, client)

if portsListError != nil {
log.WithError(portsListError).Error("Could not get the ports list.")
return
}

if len(ports) == 0 {
fmt.Println("No ports detected.")
return
}

sort.Slice(ports, func(i, j int) bool {
return int(ports[i].LocalPort) < int(ports[j].LocalPort)
})

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 == api.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 == api.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
}
}

table.Rich(
[]string{fmt.Sprint(port.LocalPort), status, port.Exposed.Url, nameAndDescription},
[]tablewriter.Colors{{}, {statusColor}, {}, {}},
)
}

table.Render()
}
26 changes: 26 additions & 0 deletions components/gitpod-cli/pkg/supervisor/ports.go
@@ -0,0 +1,26 @@
// 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 supervisor

import (
"context"
"github.com/gitpod-io/gitpod/supervisor/api"
)

func GetPortsList(ctx context.Context, client api.StatusServiceClient) ([]*api.PortsStatus, error) {
portsStatusClient, portsStatusClientError := client.PortsStatus(ctx, &api.PortsStatusRequest{Observe: false})

if portsStatusClientError != nil {
return nil, portsStatusClientError
}

portsStatusResponse, portsStatusResponseError := portsStatusClient.Recv()

if portsStatusResponseError != nil {
return nil, portsStatusResponseError
}

return portsStatusResponse.GetPorts(), nil
}

0 comments on commit 0723994

Please sign in to comment.