diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index 09397af54..ea501b73e 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -19,11 +19,17 @@ import ( "golang.org/x/sync/errgroup" ) +// PortOptions represents the options accepted by the ports command. type PortsOptions struct { + // CodespaceName is the name of the codespace, optional. CodespaceName string - AsJSON bool + + // AsJSON dictates whether the command returns a json output or not, optional. + AsJSON bool } +// NewPortsCmd returns a Cobra "ports" command that displays a table of available ports, +// according to the specified flags. func NewPortsCmd() *cobra.Command { opts := &PortsOptions{} @@ -32,7 +38,7 @@ func NewPortsCmd() *cobra.Command { Short: "List ports in a Codespace", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - return Ports(opts) + return ports(opts) }, } @@ -50,7 +56,7 @@ func init() { rootCmd.AddCommand(NewPortsCmd()) } -func Ports(opts *PortsOptions) error { +func ports(opts *PortsOptions) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() log := output.NewLogger(os.Stdout, os.Stderr, opts.AsJSON) @@ -87,7 +93,7 @@ func Ports(opts *PortsOptions) error { } table := output.NewTable(os.Stdout, opts.AsJSON) - table.SetHeader([]string{"Label", "Source Port", "Destination Port", "Public", "Browse URL"}) + table.SetHeader([]string{"Label", "Port", "Public", "Browse URL"}) for _, port := range ports { sourcePort := strconv.Itoa(port.SourcePort) var portName string @@ -100,7 +106,6 @@ func Ports(opts *PortsOptions) error { table.Append([]string{ portName, sourcePort, - strconv.Itoa(port.DestinationPort), strings.ToUpper(strconv.FormatBool(port.IsPublic)), fmt.Sprintf("https://%s-%s.githubpreview.dev/", codespace.Name, sourcePort), }) @@ -168,6 +173,8 @@ func getDevContainer(ctx context.Context, apiClient *api.API, codespace *api.Cod return ch } +// NewPortsPublicCmd returns a Cobra "ports public" subcommand, which makes a given port public. +// to make a given port public. func NewPortsPublicCmd() *cobra.Command { return &cobra.Command{ Use: "public ", @@ -180,6 +187,7 @@ func NewPortsPublicCmd() *cobra.Command { } } +// NewPortsPrivateCmd returns a Cobra "ports private" subcommand, which makes a given port private. func NewPortsPrivateCmd() *cobra.Command { return &cobra.Command{ Use: "private ", @@ -239,6 +247,8 @@ func updatePortVisibility(log *output.Logger, codespaceName, sourcePort string, return nil } +// NewPortsForwardCmd returns a Cobra "ports forward" subcommand, which forwards a set of +// port pairs from the codespace to localhost. func NewPortsForwardCmd() *cobra.Command { return &cobra.Command{ Use: "forward :",