From 3dcee5cca72f0a70aed7cdc270cd4a0d6f0d584b Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Fri, 27 Aug 2021 12:41:36 +0000 Subject: [PATCH 1/5] remove dst port column and add docs --- cmd/ghcs/ports.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index 09397af54..9e2713a5e 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -19,11 +19,16 @@ 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 new cobra command representing the ports command and sub commands func NewPortsCmd() *cobra.Command { opts := &PortsOptions{} @@ -50,6 +55,7 @@ func init() { rootCmd.AddCommand(NewPortsCmd()) } +// Ports accepts a PortOptions pointer and logs a list the list of available open ports found in a codespace func Ports(opts *PortsOptions) error { apiClient := api.New(os.Getenv("GITHUB_TOKEN")) ctx := context.Background() @@ -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 command representing the ports subcommand used +// to make a given port public func NewPortsPublicCmd() *cobra.Command { return &cobra.Command{ Use: "public ", @@ -180,6 +187,8 @@ func NewPortsPublicCmd() *cobra.Command { } } +// NewPortsPrivateCmd rturns a cobra command representing the ports subcommand used +// to make a given port private func NewPortsPrivateCmd() *cobra.Command { return &cobra.Command{ Use: "private ", @@ -239,6 +248,8 @@ func updatePortVisibility(log *output.Logger, codespaceName, sourcePort string, return nil } +// NewPortsForwardCmd returns a cobra command representing the ports subcommand used to forward +// ports from the codespace to localhost, it supports multiple ports to be forwarded at once func NewPortsForwardCmd() *cobra.Command { return &cobra.Command{ Use: "forward :", From 5dc923777be8c5ca232128c2b7924420b49b6bfd Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Fri, 27 Aug 2021 15:32:18 +0000 Subject: [PATCH 2/5] update docs, make ports private to be more consistent --- cmd/ghcs/ports.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index 9e2713a5e..2318097aa 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -21,14 +21,15 @@ import ( // PortOptions represents the options accepted by the ports command. type PortsOptions struct { - // CodespaceName is the name of the codespace, optional + // CodespaceName is the name of the codespace, optional. CodespaceName string - // AsJSON dictates whether the command returns a json output or not, optional + // AsJSON dictates whether the command returns a json output or not, optional. AsJSON bool } -// NewPortsCmd returns a new cobra command representing the ports command and sub commands +// NewPortsCmd returns a Cobra "ports" command that displays a table of available ports, +// according to the specified flags. func NewPortsCmd() *cobra.Command { opts := &PortsOptions{} @@ -37,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) }, } @@ -55,8 +56,7 @@ func init() { rootCmd.AddCommand(NewPortsCmd()) } -// Ports accepts a PortOptions pointer and logs a list the list of available open ports found in a codespace -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) @@ -173,7 +173,7 @@ func getDevContainer(ctx context.Context, apiClient *api.API, codespace *api.Cod return ch } -// NewPortsPublicCmd returns a cobra command representing the ports subcommand used +// 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{ @@ -187,8 +187,7 @@ func NewPortsPublicCmd() *cobra.Command { } } -// NewPortsPrivateCmd rturns a cobra command representing the ports subcommand used -// to make a given port private +// NewPortsPrivateCmd returns a Cobra "ports private" subcommand, which makes a given port private. func NewPortsPrivateCmd() *cobra.Command { return &cobra.Command{ Use: "private ", @@ -248,8 +247,8 @@ func updatePortVisibility(log *output.Logger, codespaceName, sourcePort string, return nil } -// NewPortsForwardCmd returns a cobra command representing the ports subcommand used to forward -// ports from the codespace to localhost, it supports multiple ports to be forwarded at once +// 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 :", From 8e95493872f31e953a33a86381e12ab93f7999f5 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Fri, 27 Aug 2021 15:46:40 +0000 Subject: [PATCH 3/5] period --- cmd/ghcs/ports.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/ghcs/ports.go b/cmd/ghcs/ports.go index 2318097aa..ea501b73e 100644 --- a/cmd/ghcs/ports.go +++ b/cmd/ghcs/ports.go @@ -174,7 +174,7 @@ func getDevContainer(ctx context.Context, apiClient *api.API, codespace *api.Cod } // NewPortsPublicCmd returns a Cobra "ports public" subcommand, which makes a given port public. -// to make a given port public +// to make a given port public. func NewPortsPublicCmd() *cobra.Command { return &cobra.Command{ Use: "public ", From 8b395b5ab5b86366ca6e2c6b5a46133cac703028 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 27 Aug 2021 15:53:55 -0400 Subject: [PATCH 4/5] ghcs code: improve vscode error --- cmd/ghcs/code.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/ghcs/code.go b/cmd/ghcs/code.go index 81dbdbb2c..fd71da74f 100644 --- a/cmd/ghcs/code.go +++ b/cmd/ghcs/code.go @@ -57,8 +57,9 @@ func Code(codespaceName string, useInsiders bool) error { codespaceName = codespace.Name } - if err := open.Run(vscodeProtocolURL(codespaceName, useInsiders)); err != nil { - return fmt.Errorf("error opening vscode URL") + url := vscodeProtocolURL(codespaceName, useInsiders) + if err := open.Run(url); err != nil { + return fmt.Errorf("error opening vscode URL %s: %s. (Is VSCode installed?)", url, err) } return nil From e423cb0ef953f4d6e02707547f1c02615f9a7cff Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 27 Aug 2021 16:09:02 -0400 Subject: [PATCH 5/5] display colon and cursor in survey prompts --- cmd/ghcs/create.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/ghcs/create.go b/cmd/ghcs/create.go index c3e8a24a1..7b33322be 100644 --- a/cmd/ghcs/create.go +++ b/cmd/ghcs/create.go @@ -126,11 +126,11 @@ func getRepoName() (string, error) { repoSurvey := []*survey.Question{ { Name: "repository", - Prompt: &survey.Input{Message: "Repository"}, + Prompt: &survey.Input{Message: "Repository:"}, Validate: survey.Required, }, } - err := survey.Ask(repoSurvey, &repo) + err := ask(repoSurvey, &repo) return repo, err } @@ -142,11 +142,11 @@ func getBranchName() (string, error) { branchSurvey := []*survey.Question{ { Name: "branch", - Prompt: &survey.Input{Message: "Branch"}, + Prompt: &survey.Input{Message: "Branch:"}, Validate: survey.Required, }, } - err := survey.Ask(branchSurvey, &branch) + err := ask(branchSurvey, &branch) return branch, err } @@ -198,7 +198,7 @@ func getMachineName(ctx context.Context, user *api.User, repo *api.Repository, l } skuAnswers := struct{ SKU string }{} - if err := survey.Ask(skuSurvey, &skuAnswers); err != nil { + if err := ask(skuSurvey, &skuAnswers); err != nil { return "", fmt.Errorf("error getting SKU: %v", err) } @@ -207,3 +207,8 @@ func getMachineName(ctx context.Context, user *api.User, repo *api.Repository, l return machine, nil } + +// ask asks survery questions using standard options. +func ask(qs []*survey.Question, response interface{}) error { + return survey.Ask(qs, response, survey.WithShowCursor(true)) +}