github.com/jaylevin/jenkins-library@v1.230.4/cmd/completions.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/spf13/cobra"
     5  	"os"
     6  )
     7  
     8  // CommandLineCompletionCommand allows to generate convenience scripts for using the piper cli in a shell.
     9  // See https://github.com/spf13/cobra/blob/master/shell_completions.md for docs on the subject.
    10  func CommandLineCompletionCommand() *cobra.Command {
    11  	return &cobra.Command{
    12  		Use:   "completion [bash|zsh|fish|powershell]",
    13  		Short: "Generate completion script",
    14  		Long: `To load completions:
    15  
    16  Bash:
    17  
    18  $ source <(piper completion bash)
    19  
    20  # To load completions for each session, execute once:
    21  Linux:
    22    $ piper completion bash > /etc/bash_completion.d/piper
    23  MacOS:
    24    $ piper completion bash > /usr/local/etc/bash_completion.d/piper
    25  
    26  Zsh:
    27  
    28  # If shell completion is not already enabled in your environment you will need
    29  # to enable it.  You can execute the following once:
    30  
    31  $ echo "autoload -U compinit; compinit" >> ~/.zshrc
    32  
    33  # To load completions for each session, execute once:
    34  $ piper completion zsh > "${fpath[1]}/_piper"
    35  
    36  # You will need to start a new shell for this setup to take effect.
    37  
    38  Fish:
    39  
    40  $ piper completion fish | source
    41  
    42  # To load completions for each session, execute once:
    43  $ piper completion fish > ~/.config/fish/completions/piper.fish
    44  `,
    45  		DisableFlagsInUseLine: true,
    46  		ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
    47  		Args:                  cobra.ExactValidArgs(1),
    48  		Run: func(cmd *cobra.Command, args []string) {
    49  			switch args[0] {
    50  			case "bash":
    51  				cmd.Root().GenBashCompletion(os.Stdout)
    52  			case "zsh":
    53  				cmd.Root().GenZshCompletion(os.Stdout)
    54  			case "fish":
    55  				cmd.Root().GenFishCompletion(os.Stdout, true)
    56  			case "powershell":
    57  				cmd.Root().GenPowerShellCompletion(os.Stdout)
    58  			}
    59  		},
    60  	}
    61  }