testing out moving off bash
Signed-off-by: James Strong <james.strong@chainguard.dev>
This commit is contained in:
parent
5bfc56618e
commit
3a8cccc469
5 changed files with 233 additions and 0 deletions
66
ingressctl/cmd/root.go
Normal file
66
ingressctl/cmd/root.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "ingressctl",
|
||||
Short: "CLI",
|
||||
Long: `CLI for managing ingress-nginx repo`,
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(newCmdCompletion(os.Stdout))
|
||||
rootCmd.SetOut(os.Stdout)
|
||||
rootCmd.SetErr(os.Stderr)
|
||||
}
|
||||
|
||||
const completionExample = `
|
||||
# Installing bash completion on macOS using homebrew
|
||||
## If running Bash 3.2 included with macOS
|
||||
brew install bash-completion
|
||||
## or, if running Bash 4.1+
|
||||
brew install bash-completion@2
|
||||
`
|
||||
|
||||
func newCmdCompletion(out io.Writer) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "completion [shell]",
|
||||
Short: "Output shell completion code",
|
||||
Long: ``,
|
||||
Example: completionExample,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runCompletion(out, cmd, args)
|
||||
},
|
||||
ValidArgs: []string{"bash", "zsh", "fish"},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runCompletion(out io.Writer, cmd *cobra.Command, args []string) error {
|
||||
if len(args) > 1 {
|
||||
return fmt.Errorf("too many arguments; expected only the shell type: %s", args)
|
||||
}
|
||||
|
||||
if len(args) == 0 || args[0] == "bash" {
|
||||
return cmd.Root().GenBashCompletion(out)
|
||||
} else if args[0] == "zsh" {
|
||||
return cmd.Root().GenZshCompletion(out)
|
||||
} else if args[0] == "fish" {
|
||||
return cmd.Root().GenFishCompletion(out, true)
|
||||
}
|
||||
return fmt.Errorf("unsupported shell: %s", args[0])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue