Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command 'gp ports list' to gitpod-cli
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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" | ||
) | ||
|
||
// portsCmd represents the ports command | ||
var portsCmd = &cobra.Command{ | ||
Use: "ports", | ||
Short: "Interact with workspace ports", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
_ = cmd.Help() | ||
} | ||
}, | ||
} | ||
|
||
// listPortsCmd represents the ports list command | ||
var listPortsCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "Lists the workspace ports and their state", | ||
Run: ports.ListPortsCmd, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(portsCmd) | ||
portsCmd.AddCommand(listPortsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(cmd *cobra.Command, args []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 | ||
} | ||
|
||
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("|") | ||
|
||
sort.Slice(ports, func(i, j int) bool { | ||
return int(ports[i].LocalPort) < int(ports[j].LocalPort) | ||
}) | ||
|
||
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.FgHiCyanColor | ||
} else { | ||
statusColor = tablewriter.FgHiWhiteColor | ||
} | ||
} | ||
|
||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |