github.com/secman-team/gh-api@v1.8.2/pkg/cmd/completion/completion.go (about)

     1  package completion
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/MakeNowJust/heredoc"
     8  	"github.com/secman-team/gh-api/pkg/cmdutil"
     9  	"github.com/secman-team/gh-api/pkg/iostreams"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  func NewCmdCompletion(io *iostreams.IOStreams) *cobra.Command {
    14  	var shellType string
    15  
    16  	cmd := &cobra.Command{
    17  		Use:   "completion -s <shell>",
    18  		Short: "Generate shell completion scripts",
    19  		Long: heredoc.Docf(`
    20  			Generate shell completion scripts for GitHub CLI commands.
    21  
    22  			When installing GitHub CLI through a package manager, it's possible that
    23  			no additional shell configuration is necessary to gain completion support. For
    24  			Homebrew, see https://docs.brew.sh/Shell-Completion
    25  
    26  			If you need to set up completions manually, follow the instructions below. The exact
    27  			config file locations might vary based on your system. Make sure to restart your
    28  			shell before testing whether completions are working.
    29  
    30  			### bash
    31  
    32  			Add this to your %[1]s~/.bash_profile%[1]s:
    33  
    34  				eval "$(gh completion -s bash)"
    35  			
    36  			### zsh
    37  
    38  			Generate a %[1]s_gh%[1]s completion script and put it somewhere in your %[1]s$fpath%[1]s:
    39  
    40  				gh completion -s zsh > /usr/local/share/zsh/site-functions/_gh
    41  
    42  			Ensure that the following is present in your %[1]s~/.zshrc%[1]s:
    43  
    44  				autoload -U compinit
    45  				compinit -i
    46  			
    47  			Zsh version 5.7 or later is recommended.
    48  
    49  			### fish
    50  
    51  			Generate a %[1]sgh.fish%[1]s completion script:
    52  
    53  				gh completion -s fish > ~/.config/fish/completions/gh.fish
    54  		`, "`"),
    55  		RunE: func(cmd *cobra.Command, args []string) error {
    56  			if shellType == "" {
    57  				if io.IsStdoutTTY() {
    58  					return &cmdutil.FlagError{Err: errors.New("error: the value for `--shell` is required")}
    59  				}
    60  				shellType = "bash"
    61  			}
    62  
    63  			w := io.Out
    64  			rootCmd := cmd.Parent()
    65  
    66  			switch shellType {
    67  			case "bash":
    68  				return rootCmd.GenBashCompletion(w)
    69  			case "zsh":
    70  				return rootCmd.GenZshCompletion(w)
    71  			case "powershell":
    72  				return rootCmd.GenPowerShellCompletion(w)
    73  			case "fish":
    74  				return rootCmd.GenFishCompletion(w, true)
    75  			default:
    76  				return fmt.Errorf("unsupported shell type %q", shellType)
    77  			}
    78  		},
    79  		DisableFlagsInUseLine: true,
    80  	}
    81  
    82  	cmdutil.DisableAuthCheck(cmd)
    83  
    84  	cmd.Flags().StringVarP(&shellType, "shell", "s", "", "Shell type: {bash|zsh|fish|powershell}")
    85  
    86  	return cmd
    87  }