mirror of
https://github.com/gitpod-io/gitpod.git
synced 2025-12-08 17:36:30 +00:00
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
// 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 (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"time"
|
|
|
|
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
|
|
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
)
|
|
|
|
var listPortsCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "Lists the workspace ports and their states.",
|
|
Run: func(*cobra.Command, []string) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
ports, portsListError := supervisor_helper.GetPortsList(ctx)
|
|
|
|
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 == supervisor.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 == supervisor.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()
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
portsCmd.AddCommand(listPortsCmd)
|
|
}
|