aboutsummaryrefslogtreecommitdiff
path: root/weed/command
diff options
context:
space:
mode:
authorbyunghwa.yun <combine@combineads.co.kr>2021-08-03 00:25:44 +0900
committerbyunghwa.yun <combine@combineads.co.kr>2021-08-03 02:56:52 +0900
commitbdc7730fdb160ea59efa72ff946cd329d5e1c59f (patch)
tree72c8cac450dc37b67a9155ffbcd3e2bd0af163c0 /weed/command
parent2ae9705442b513af60e26c786c612867ea36015d (diff)
downloadseaweedfs-bdc7730fdb160ea59efa72ff946cd329d5e1c59f.tar.xz
seaweedfs-bdc7730fdb160ea59efa72ff946cd329d5e1c59f.zip
Add autocomplete
Diffstat (limited to 'weed/command')
-rw-r--r--weed/command/autocomplete.go109
-rw-r--r--weed/command/command.go2
2 files changed, 111 insertions, 0 deletions
diff --git a/weed/command/autocomplete.go b/weed/command/autocomplete.go
new file mode 100644
index 000000000..b98c16cf3
--- /dev/null
+++ b/weed/command/autocomplete.go
@@ -0,0 +1,109 @@
+package command
+
+import (
+ "fmt"
+ flag "github.com/chrislusf/seaweedfs/weed/util/fla9"
+ "github.com/posener/complete"
+ completeinstall "github.com/posener/complete/cmd/install"
+ "runtime"
+)
+
+func AutocompleteMain(commands []*Command) bool {
+ subCommands := make(map[string]complete.Command)
+ helpSubCommands := make(map[string]complete.Command)
+ for _, cmd := range commands {
+ flags := make(map[string]complete.Predictor)
+ cmd.Flag.VisitAll(func(flag *flag.Flag) {
+ flags["-"+flag.Name] = complete.PredictAnything
+ })
+
+ subCommands[cmd.Name()] = complete.Command{
+ Flags: flags,
+ }
+ helpSubCommands[cmd.Name()] = complete.Command{}
+ }
+ subCommands["help"] = complete.Command{Sub: helpSubCommands}
+
+ globalFlags := make(map[string]complete.Predictor)
+ flag.VisitAll(func(flag *flag.Flag) {
+ globalFlags["-"+flag.Name] = complete.PredictAnything
+ })
+
+ weedCmd := complete.Command{
+ Sub: subCommands,
+ Flags: globalFlags,
+ GlobalFlags: complete.Flags{"-h": complete.PredictNothing},
+ }
+ cmp := complete.New("weed", weedCmd)
+
+ return cmp.Complete()
+}
+
+func installAutoCompletion() bool {
+ if runtime.GOOS == "windows" {
+ fmt.Printf("windows is not supported")
+ return false
+ }
+
+ err := completeinstall.Install("weed")
+ if err != nil {
+ fmt.Printf("install failed! %s\n", err)
+ return false
+ }
+ fmt.Printf("autocompletion is enabled. Please restart your shell.\n")
+ return true
+}
+
+func uninstallAutoCompletion() bool {
+ if runtime.GOOS == "windows" {
+ fmt.Printf("windows is not supported")
+ return false
+ }
+
+ err := completeinstall.Uninstall("weed")
+ if err != nil {
+ fmt.Printf("uninstall failed! %s\n", err)
+ return false
+ }
+ fmt.Printf("autocompletion is disable. Please restart your shell.\n")
+ return true
+}
+
+var cmdAutocomplete = &Command{
+ Run: runAutocomplete,
+ UsageLine: "autocomplete",
+ Short: "install autocomplete",
+ Long: `weed autocomplete is installed in the shell.
+
+ Supported shells are bash, zsh, and fish.
+ Windows is not supported.
+
+`,
+}
+
+func runAutocomplete(cmd *Command, args []string) bool {
+ if len(args) != 0 {
+ cmd.Usage()
+ }
+
+ return installAutoCompletion()
+}
+
+var cmdUnautocomplete = &Command{
+ Run: runUnautocomplete,
+ UsageLine: "unautocomplete",
+ Short: "uninstall autocomplete",
+ Long: `weed autocomplete is uninstalled in the shell.
+
+ Windows is not supported.
+
+`,
+}
+
+func runUnautocomplete(cmd *Command, args []string) bool {
+ if len(args) != 0 {
+ cmd.Usage()
+ }
+
+ return uninstallAutoCompletion()
+}
diff --git a/weed/command/command.go b/weed/command/command.go
index 0bac56442..022aba194 100644
--- a/weed/command/command.go
+++ b/weed/command/command.go
@@ -36,6 +36,8 @@ var Commands = []*Command{
cmdVersion,
cmdVolume,
cmdWebDav,
+ cmdAutocomplete,
+ cmdUnautocomplete,
}
type Command struct {