1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package command
import (
"fmt"
"github.com/posener/complete"
completeinstall "github.com/posener/complete/cmd/install"
flag "github.com/seaweedfs/seaweedfs/weed/util/fla9"
"os"
"path/filepath"
"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 printAutocompleteScript(shell string) bool {
bin, err := os.Executable()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get executable path: %s\n", err)
return false
}
binPath, err := filepath.Abs(bin)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get absolute path: %s\n", err)
return false
}
switch shell {
case "bash":
fmt.Printf("complete -C %q weed\n", binPath)
case "zsh":
fmt.Printf("autoload -U +X bashcompinit && bashcompinit\n")
fmt.Printf("complete -o nospace -C %q weed\n", binPath)
case "fish":
fmt.Printf(`function __complete_weed
set -lx COMP_LINE (commandline -cp)
test -z (commandline -ct)
and set COMP_LINE "$COMP_LINE "
%q
end
complete -f -c weed -a "(__complete_weed)"
`, binPath)
default:
fmt.Fprintf(os.Stderr, "unsupported shell: %s. Supported shells: bash, zsh, fish\n", shell)
return false
}
return true
}
func installAutoCompletion() bool {
if runtime.GOOS == "windows" {
fmt.Println("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.Println("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 disabled. Please restart your shell.\n")
return true
}
var cmdAutocomplete = &Command{
Run: runAutocomplete,
UsageLine: "autocomplete [shell]",
Short: "generate or install shell autocomplete script",
Long: `Generate shell autocomplete script or install it to your shell configuration.
Usage:
weed autocomplete [bash|zsh|fish] # print autocomplete script to stdout
weed autocomplete install # install to shell config files
When a shell name is provided, the autocomplete script is printed to stdout.
You can then add it to your shell configuration manually, e.g.:
# For bash:
weed autocomplete bash >> ~/.bashrc
# Or use eval in your shell config:
eval "$(weed autocomplete bash)"
When 'install' is provided (or no argument), the script is automatically
installed to your shell configuration files.
Supported shells are bash, zsh, and fish.
Windows is not supported.
`,
}
func runAutocomplete(cmd *Command, args []string) bool {
if len(args) == 0 {
// Default behavior: install
return installAutoCompletion()
}
if len(args) > 1 {
cmd.Usage()
return false
}
shell := args[0]
if shell == "install" {
return installAutoCompletion()
}
// Print the autocomplete script for the specified shell
return printAutocompleteScript(shell)
}
var cmdUnautocomplete = &Command{
Run: runUnautocomplete,
UsageLine: "autocomplete.uninstall",
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()
}
|