github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/cmd/genautocomplete/genautocomplete_zsh.go (about)

     1  package genautocomplete
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  
     7  	"github.com/rclone/rclone/cmd"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func init() {
    12  	completionDefinition.AddCommand(zshCommandDefinition)
    13  }
    14  
    15  var zshCommandDefinition = &cobra.Command{
    16  	Use:   "zsh [output_file]",
    17  	Short: `Output zsh completion script for rclone.`,
    18  	Long: `
    19  Generates a zsh autocompletion script for rclone.
    20  
    21  This writes to /usr/share/zsh/vendor-completions/_rclone by default so will
    22  probably need to be run with sudo or as root, eg
    23  
    24      sudo rclone genautocomplete zsh
    25  
    26  Logout and login again to use the autocompletion scripts, or source
    27  them directly
    28  
    29      autoload -U compinit && compinit
    30  
    31  If you supply a command line argument the script will be written
    32  there.
    33  `,
    34  	Run: func(command *cobra.Command, args []string) {
    35  		cmd.CheckArgs(0, 1, command, args)
    36  		out := "/usr/share/zsh/vendor-completions/_rclone"
    37  		if len(args) > 0 {
    38  			out = args[0]
    39  		}
    40  		outFile, err := os.Create(out)
    41  		if err != nil {
    42  			log.Fatal(err)
    43  		}
    44  		defer func() { _ = outFile.Close() }()
    45  		err = cmd.Root.GenZshCompletion(outFile)
    46  		if err != nil {
    47  			log.Fatal(err)
    48  		}
    49  	},
    50  }