github.com/SAP/jenkins-library@v1.362.0/cmd/completions.go (about)

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