Skip to content

Commit

Permalink
🚩 Add port-forward port shell completion
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 26, 2022
1 parent 91afb16 commit a132b6d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions cmd/port_forward/port_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
)

var Command = &cobra.Command{
Use: "port-forward [local_port]",
Short: "set up a local port forward",
Args: cobra.MaximumNArgs(1),
RunE: run,
PreRunE: preRun,
Use: "port-forward [local_port]",
Short: "set up a local port forward",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: validArgs,
RunE: run,
PreRunE: preRun,
}

var conf config.PortForward
Expand All @@ -34,6 +35,21 @@ func init() {
flags.Address(Command, &conf.Addresses)
}

func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
err := preRun(cmd, args)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

defaultPort := uint64(conf.Grammar.DefaultPort())

return []string{
strconv.FormatUint(uint64(conf.LocalPort), 10),
strconv.FormatUint(defaultPort, 10),
strconv.FormatUint(defaultPort+1, 10),
}, cobra.ShellCompDirectiveNoFileComp
}

func preRun(cmd *cobra.Command, args []string) error {
err := util.DefaultSetup(cmd, &conf.Global)
if err != nil {
Expand Down

0 comments on commit a132b6d

Please sign in to comment.