You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
  1. package command
  2. import (
  3. "fmt"
  4. flag "github.com/chrislusf/seaweedfs/weed/util/fla9"
  5. "github.com/posener/complete"
  6. completeinstall "github.com/posener/complete/cmd/install"
  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 disable. 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. }