|
|
@ -2,6 +2,8 @@ package command |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"os" |
|
|
|
|
|
"path/filepath" |
|
|
"github.com/posener/complete" |
|
|
"github.com/posener/complete" |
|
|
completeinstall "github.com/posener/complete/cmd/install" |
|
|
completeinstall "github.com/posener/complete/cmd/install" |
|
|
flag "github.com/seaweedfs/seaweedfs/weed/util/fla9" |
|
|
flag "github.com/seaweedfs/seaweedfs/weed/util/fla9" |
|
|
@ -39,6 +41,40 @@ func AutocompleteMain(commands []*Command) bool { |
|
|
return cmp.Complete() |
|
|
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 { |
|
|
func installAutoCompletion() bool { |
|
|
if runtime.GOOS == "windows" { |
|
|
if runtime.GOOS == "windows" { |
|
|
fmt.Println("Windows is not supported") |
|
|
fmt.Println("Windows is not supported") |
|
|
@ -71,9 +107,25 @@ func uninstallAutoCompletion() bool { |
|
|
|
|
|
|
|
|
var cmdAutocomplete = &Command{ |
|
|
var cmdAutocomplete = &Command{ |
|
|
Run: runAutocomplete, |
|
|
Run: runAutocomplete, |
|
|
UsageLine: "autocomplete", |
|
|
|
|
|
Short: "install autocomplete", |
|
|
|
|
|
Long: `weed autocomplete is installed in the shell. |
|
|
|
|
|
|
|
|
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. |
|
|
Supported shells are bash, zsh, and fish. |
|
|
Windows is not supported. |
|
|
Windows is not supported. |
|
|
@ -82,11 +134,23 @@ var cmdAutocomplete = &Command{ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func runAutocomplete(cmd *Command, args []string) bool { |
|
|
func runAutocomplete(cmd *Command, args []string) bool { |
|
|
if len(args) != 0 { |
|
|
|
|
|
|
|
|
if len(args) == 0 { |
|
|
|
|
|
// Default behavior: install
|
|
|
|
|
|
return installAutoCompletion() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(args) > 1 { |
|
|
cmd.Usage() |
|
|
cmd.Usage() |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
shell := args[0] |
|
|
|
|
|
if shell == "install" { |
|
|
|
|
|
return installAutoCompletion() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return installAutoCompletion() |
|
|
|
|
|
|
|
|
// Print the autocomplete script for the specified shell
|
|
|
|
|
|
return printAutocompleteScript(shell) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var cmdUnautocomplete = &Command{ |
|
|
var cmdUnautocomplete = &Command{ |
|
|
|