Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gitpod-cli] Add command 'gp ports list' #10388

Merged
merged 1 commit into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"})
felladrin marked this conversation as resolved.
Show resolved Hide resolved
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
}