Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Commit

Permalink
fix shell completion after update to urfave/cli@2.0.0
Browse files Browse the repository at this point in the history
Depends on urfave/cli#946
  • Loading branch information
Roberto Hidalgo committed Dec 2, 2019
1 parent e16523d commit 92b70c7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ BUILD_HOST := $(shell uname -s | tr '[[:upper:]]' '[[:lower:]]')
BINARY := dist/$(BUILD_HOST)/go-config-yourself
export GO111MODULE=on

complete: build-local
mv dist/local/gcy dist/local/asdf
rm /usr/local/share/zsh/site-functions/_asdf || true
cp bin/autocomplete/completion.zsh /usr/local/share/zsh/site-functions/_asdf
rm ~/.zcompdump*
#now run rehash && compinit

# --------------
# Dev setup
# --------------
Expand Down
2 changes: 1 addition & 1 deletion bin/autocomplete/completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _cli_bash_autocomplete() {
local cur opts;
COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}";
opts=$( CUR=$cur ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-completion );
opts=$( CUR=$cur ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion );
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );
return 0;
};
Expand Down
28 changes: 16 additions & 12 deletions bin/autocomplete/completion.zsh
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#compdef gcy
#autoload
# shellcheck shell=bash
autoload -U compinit && compinit;
autoload -U bashcompinit && bashcompinit;

_cli_bash_autocomplete() {
local cur opts;
COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}";
opts=$( CUR="$cur" "${COMP_WORDS[@]:0:$COMP_CWORD}" --generate-completion );
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );
return 0;
};
_gcy_zsh_autocomplete () {

_gcy () {
complete -o nospace -o default -F _cli_bash_autocomplete gcy
local -a opts
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")

exit_code="$?"
if [[ $exit_code -gt 0 ]]; then
_path_files
[[ $exit_code == 1 ]]; return
fi

_describe 'gcy' opts

return
}

compdef _gcy_zsh_autocomplete gcy
36 changes: 31 additions & 5 deletions cmd/autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ func CommandAutocomplete(ctx *cli.Context) {
}

for _, cmd := range ctx.App.VisibleCommands() {
if strings.HasPrefix(cmd.Name, firstArg) {
fmt.Println(cmd.Name)
if firstArg == "" || strings.HasPrefix(cmd.Name, firstArg) {
if os.Getenv("_CLI_ZSH_AUTOCOMPLETE_HACK") == "1" {
fmt.Printf("%s:%s\n", cmd.Name, cmd.Usage)
} else {
fmt.Println(cmd.Name)
}
}
}
os.Exit(0)
}

//ListKeys lists keys at a given keypath
Expand Down Expand Up @@ -91,22 +96,43 @@ func ListProviderFlags(ctx *cli.Context) (keepGoing bool) {
// ListAllFlags all possible flags
func ListAllFlags(ctx *cli.Context) {
var flags []cli.Flag
if ctx.Command != nil {
if ctx.Command != nil && ctx.Command.Name != "" {
flags = ctx.Command.VisibleFlags()
} else {
flags = ctx.App.VisibleFlags()
}

isZSH := os.Getenv("_CLI_ZSH_AUTOCOMPLETE_HACK")
for _, f := range flags {
name := f.Names()[0]
if name == "init-completion" {
continue
}

description := ""
if isZSH == "1" {
switch typedFlag := f.(type) {
case *cli.StringFlag:
description = typedFlag.Usage
case *cli.BoolFlag:
description = typedFlag.Usage
case *cli.GenericFlag:
description = typedFlag.Usage
case *cli.StringSliceFlag:
description = typedFlag.Usage
default:
log.Warningf("%s: %T", name, typedFlag)
}
}

_, isRepeatable := f.(*cli.StringSliceFlag)

if isRepeatable || !ctx.IsSet(name) {
fmt.Println(fmt.Sprintf("--%s", name))
if isZSH == "1" {
fmt.Println(fmt.Sprintf("--%s:%s", name, description))
} else {
fmt.Println(fmt.Sprintf("--%s", name))
}
}
}
}
Expand Down Expand Up @@ -139,7 +165,7 @@ func LastFlagIs(flagName string) (query string, ok bool) {

func validArgs() (validArgs []string) {
for _, arg := range os.Args {
if arg == "--" || arg == "--generate-completion" {
if arg == "--" || arg == "--generate-bash-completion" {
break
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/autocomplete/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestCommandAutoComplete(t *testing.T) {
comp.CommandAutocomplete(fx.MockCliCtx(nil))
output := mockedStdOut()
options := strings.Split(output, "\n")
expected := []string{"command", ""}
expected := []string{"--verbose", "--version", "command", ""}

if !diff.Equal(options, expected) {
t.Fatalf("Invalid output, got: %v, expected %v", options, expected)
Expand Down

0 comments on commit 92b70c7

Please sign in to comment.