genautocomplete_bash.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package genautocomplete
  2. import (
  3. "log"
  4. "os"
  5. "github.com/rclone/rclone/cmd"
  6. "github.com/spf13/cobra"
  7. )
  8. func init() {
  9. completionDefinition.AddCommand(bashCommandDefinition)
  10. }
  11. var bashCommandDefinition = &cobra.Command{
  12. Use: "bash [output_file]",
  13. Short: `Output bash completion script for rclone.`,
  14. Long: `
  15. Generates a bash shell autocompletion script for rclone.
  16. By default, when run without any arguments,
  17. rclone genautocomplete bash
  18. the generated script will be written to
  19. /etc/bash_completion.d/rclone
  20. and so rclone will probably need to be run as root, or with sudo.
  21. If you supply a path to a file as the command line argument, then
  22. the generated script will be written to that file, in which case
  23. you should not need root privileges.
  24. If output_file is "-", then the output will be written to stdout.
  25. If you have installed the script into the default location, you
  26. can logout and login again to use the autocompletion script.
  27. Alternatively, you can source the script directly
  28. . /path/to/my_bash_completion_scripts/rclone
  29. and the autocompletion functionality will be added to your
  30. current shell.
  31. `,
  32. Run: func(command *cobra.Command, args []string) {
  33. cmd.CheckArgs(0, 1, command, args)
  34. out := "/etc/bash_completion.d/rclone"
  35. if len(args) > 0 {
  36. if args[0] == "-" {
  37. err := cmd.Root.GenBashCompletionV2(os.Stdout, false)
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. return
  42. }
  43. out = args[0]
  44. }
  45. err := cmd.Root.GenBashCompletionFileV2(out, false)
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. },
  50. }