autocomplete.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package command
  2. import (
  3. "fmt"
  4. "github.com/posener/complete"
  5. completeinstall "github.com/posener/complete/cmd/install"
  6. flag "github.com/seaweedfs/seaweedfs/weed/util/fla9"
  7. "runtime"
  8. )
  9. func AutocompleteMain(commands []*Command) bool {
  10. subCommands := make(map[string]complete.Command)
  11. helpSubCommands := make(map[string]complete.Command)
  12. for _, cmd := range commands {
  13. flags := make(map[string]complete.Predictor)
  14. cmd.Flag.VisitAll(func(flag *flag.Flag) {
  15. flags["-"+flag.Name] = complete.PredictAnything
  16. })
  17. subCommands[cmd.Name()] = complete.Command{
  18. Flags: flags,
  19. }
  20. helpSubCommands[cmd.Name()] = complete.Command{}
  21. }
  22. subCommands["help"] = complete.Command{Sub: helpSubCommands}
  23. globalFlags := make(map[string]complete.Predictor)
  24. flag.VisitAll(func(flag *flag.Flag) {
  25. globalFlags["-"+flag.Name] = complete.PredictAnything
  26. })
  27. weedCmd := complete.Command{
  28. Sub: subCommands,
  29. Flags: globalFlags,
  30. GlobalFlags: complete.Flags{"-h": complete.PredictNothing},
  31. }
  32. cmp := complete.New("weed", weedCmd)
  33. return cmp.Complete()
  34. }
  35. func installAutoCompletion() bool {
  36. if runtime.GOOS == "windows" {
  37. fmt.Println("Windows is not supported")
  38. return false
  39. }
  40. err := completeinstall.Install("weed")
  41. if err != nil {
  42. fmt.Printf("install failed! %s\n", err)
  43. return false
  44. }
  45. fmt.Printf("autocompletion is enabled. Please restart your shell.\n")
  46. return true
  47. }
  48. func uninstallAutoCompletion() bool {
  49. if runtime.GOOS == "windows" {
  50. fmt.Println("Windows is not supported")
  51. return false
  52. }
  53. err := completeinstall.Uninstall("weed")
  54. if err != nil {
  55. fmt.Printf("uninstall failed! %s\n", err)
  56. return false
  57. }
  58. fmt.Printf("autocompletion is disabled. Please restart your shell.\n")
  59. return true
  60. }
  61. var cmdAutocomplete = &Command{
  62. Run: runAutocomplete,
  63. UsageLine: "autocomplete",
  64. Short: "install autocomplete",
  65. Long: `weed autocomplete is installed in the shell.
  66. Supported shells are bash, zsh, and fish.
  67. Windows is not supported.
  68. `,
  69. }
  70. func runAutocomplete(cmd *Command, args []string) bool {
  71. if len(args) != 0 {
  72. cmd.Usage()
  73. }
  74. return installAutoCompletion()
  75. }
  76. var cmdUnautocomplete = &Command{
  77. Run: runUnautocomplete,
  78. UsageLine: "autocomplete.uninstall",
  79. Short: "uninstall autocomplete",
  80. Long: `weed autocomplete is uninstalled in the shell.
  81. Windows is not supported.
  82. `,
  83. }
  84. func runUnautocomplete(cmd *Command, args []string) bool {
  85. if len(args) != 0 {
  86. cmd.Usage()
  87. }
  88. return uninstallAutoCompletion()
  89. }