codespace flag, deprecate argument

This commit is contained in:
Jose Garcia 2021-09-03 12:33:47 -04:00
parent 88146a16a0
commit 9dbf267e54

View file

@ -18,31 +18,25 @@ import (
"github.com/spf13/cobra"
)
// portOptions represents the options accepted by the ports command.
type portsOptions struct {
// CodespaceName is the name of the codespace, optional.
codespaceName string
// 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{}
var (
codespace string
asJSON bool
)
portsCmd := &cobra.Command{
Use: "ports",
Short: "List ports in a codespace",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return ports(opts)
return ports(codespace, asJSON)
},
}
portsCmd.Flags().StringVarP(&opts.codespaceName, "codespace", "c", "", "The `name` of the codespace to use")
portsCmd.Flags().BoolVar(&opts.asJSON, "json", false, "Output as JSON")
portsCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace")
portsCmd.Flags().BoolVar(&asJSON, "json", false, "Output as JSON")
portsCmd.AddCommand(newPortsPublicCmd())
portsCmd.AddCommand(newPortsPrivateCmd())
@ -55,17 +49,17 @@ func init() {
rootCmd.AddCommand(newPortsCmd())
}
func ports(opts *portsOptions) error {
func ports(codespaceName string, asJSON bool) error {
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
ctx := context.Background()
log := output.NewLogger(os.Stdout, os.Stderr, opts.asJSON)
log := output.NewLogger(os.Stdout, os.Stderr, asJSON)
user, err := apiClient.GetUser(ctx)
if err != nil {
return fmt.Errorf("error getting user: %v", err)
}
codespace, token, err := codespaces.GetOrChooseCodespace(ctx, apiClient, user, opts.codespaceName)
codespace, token, err := codespaces.GetOrChooseCodespace(ctx, apiClient, user, codespaceName)
if err != nil {
if err == codespaces.ErrNoCodespaces {
return err
@ -92,7 +86,7 @@ func ports(opts *portsOptions) error {
_, _ = log.Errorf("Failed to get port names: %v\n", devContainerResult.err.Error())
}
table := output.NewTable(os.Stdout, opts.asJSON)
table := output.NewTable(os.Stdout, asJSON)
table.SetHeader([]string{"Label", "Port", "Public", "Browse URL"})
for _, port := range ports {
sourcePort := strconv.Itoa(port.SourcePort)
@ -161,28 +155,54 @@ func getDevContainer(ctx context.Context, apiClient *api.API, codespace *api.Cod
// newPortsPublicCmd returns a Cobra "ports public" subcommand, which makes a given port public.
func newPortsPublicCmd() *cobra.Command {
return &cobra.Command{
Use: "public <codespace> <port>",
var codespace string
newPortsPublicCmd := &cobra.Command{
Use: "public <port>",
Short: "Mark port as public",
Args: cobra.ExactArgs(2),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
log := output.NewLogger(os.Stdout, os.Stderr, false)
return updatePortVisibility(log, args[0], args[1], true)
port := args[0]
if len(args) > 1 {
log.Println("<codespace> argument is deprecated. Use --codespace instead.")
codespace, port = args[0], args[1]
}
return updatePortVisibility(log, codespace, port, true)
},
}
newPortsPublicCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace")
return newPortsPublicCmd
}
// newPortsPrivateCmd returns a Cobra "ports private" subcommand, which makes a given port private.
func newPortsPrivateCmd() *cobra.Command {
return &cobra.Command{
Use: "private <codespace> <port>",
var codespace string
newPortsPrivateCmd := &cobra.Command{
Use: "private <port>",
Short: "Mark port as private",
Args: cobra.ExactArgs(2),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
log := output.NewLogger(os.Stdout, os.Stderr, false)
return updatePortVisibility(log, args[0], args[1], false)
port := args[0]
if len(args) > 1 {
log.Println("<codespace> argument is deprecated. Use --codespace instead.")
codespace, port = args[0], args[1]
}
return updatePortVisibility(log, codespace, port, false)
},
}
newPortsPrivateCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace")
return newPortsPrivateCmd
}
func updatePortVisibility(log *output.Logger, codespaceName, sourcePort string, public bool) error {
@ -230,15 +250,30 @@ func updatePortVisibility(log *output.Logger, codespaceName, sourcePort string,
// 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 <codespace> <remote-port>:<local-port>",
var codespace string
newPortsForwardCmd := &cobra.Command{
Use: "forward <remote-port>:<local-port>...",
Short: "Forward ports",
Args: cobra.MinimumNArgs(2),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
log := output.NewLogger(os.Stdout, os.Stderr, false)
return forwardPorts(log, args[0], args[1:])
ports := args[0:]
if len(args) > 1 && !strings.Contains(args[0], ":") {
// assume this is a codespace name
log.Println("<codespace> argument is deprecated. Use --codespace instead.")
codespace = args[0]
ports = args[1:]
}
return forwardPorts(log, codespace, ports)
},
}
newPortsForwardCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace")
return newPortsForwardCmd
}
func forwardPorts(log *output.Logger, codespaceName string, ports []string) error {