trpc.group/trpc-go/trpc-cmdline@v1.0.9/cmd/completion/completion.go (about)

     1  // Tencent is pleased to support the open source community by making tRPC available.
     2  //
     3  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     4  // All rights reserved.
     5  //
     6  // If you have downloaded a copy of the tRPC source code from Tencent,
     7  // please note that tRPC source code is licensed under the  Apache 2.0 License,
     8  // A copy of the Apache 2.0 License is included in this file.
     9  
    10  // Package completion provides completion command.
    11  package completion
    12  
    13  import (
    14  	"os"
    15  
    16  	"github.com/spf13/cobra"
    17  
    18  	"trpc.group/trpc-go/trpc-cmdline/util/log"
    19  )
    20  
    21  // CMD returns completion command.
    22  // Users can run `trpc completion your_shell_name` to get script for auto-complete.
    23  // To append to command to your shell environment, run
    24  //
    25  //	`trpc completion your_shell_name >> your_shell_init_file`
    26  func CMD() *cobra.Command {
    27  	completionCmd := &cobra.Command{
    28  		Use:   "completion [bash|zsh|fish|powershell]",
    29  		Short: "Generate shell autocompletion scripts",
    30  		Long: `Generate shell autocompletion scripts. Usage example:
    31  
    32  Bash:
    33  
    34  $ source <(yourprogram completion bash)
    35  
    36  # To load completions for each session, execute once:
    37  Linux:
    38  	$ yourprogram completion bash > /etc/bash_completion.d/yourprogram
    39  MacOS:
    40  	$ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram
    41  
    42  Zsh:
    43  
    44  # If shell completion is not already enabled in your environment you will need
    45  # to enable it.  You can execute the following once:
    46  
    47  $ echo "autoload -U compinit; compinit" >> ~/.zshrc
    48  
    49  # To load completions for each session, execute once:
    50  $ yourprogram completion zsh > "${fpath[1]}/_yourprogram"
    51  
    52  # You will need to start a new shell for this Setup to take effect.
    53  
    54  Fish:
    55  
    56  $ yourprogram completion fish | source
    57  
    58  # To load completions for each session, execute once:
    59  $ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish
    60  `,
    61  		DisableFlagsInUseLine: true,
    62  		ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
    63  		Args:                  cobra.ExactValidArgs(1),
    64  		Run: func(cmd *cobra.Command, args []string) {
    65  			switch args[0] {
    66  			case "bash":
    67  				cmd.Root().GenBashCompletion(os.Stdout)
    68  			case "zsh":
    69  				cmd.Root().GenZshCompletion(os.Stdout)
    70  			case "fish":
    71  				cmd.Root().GenFishCompletion(os.Stdout, true)
    72  			case "powershell":
    73  				cmd.Root().GenPowerShellCompletion(os.Stdout)
    74  			default:
    75  				log.Error("%s is not supported", args[0])
    76  			}
    77  		},
    78  	}
    79  	return completionCmd
    80  }