Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix shell autocomplete evaluation #441

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ _fastly_bash_autocomplete() {
return 0
}
complete -F _fastly_bash_autocomplete fastly
`,
},
{
Name: "shell evaluate completion options",
Args: args("--completion-bash"),
WantOutput: `help
acl
acl-entry
auth-token
backend
compute
configure
dictionary
dictionaryitem
domain
healthcheck
ip-list
logging
logs
pops
purge
service
service-version
stats
update
user
vcl
version
whoami
`,
},
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/app/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func processCommandInput(
// error on things not already caught by ParseContext().
noargs := len(opts.Args) == 0
ctx, err := app.ParseContext(opts.Args)
if err != nil || noargs {
if err != nil && !cmd.IsCompletion(opts.Args) || noargs {
if noargs {
err = fmt.Errorf("command not specified")
}
Expand All @@ -359,7 +359,7 @@ func processCommandInput(
// completion flag, as that depends on kingpin.Parse() being called, and so
// the ctx is otherwise empty.
var found bool
if !cmd.IsHelpOnly(opts.Args) && !cmd.IsHelpFlagOnly(opts.Args) && !cmd.IsCompletion(opts.Args) {
if !cmd.IsHelpOnly(opts.Args) && !cmd.IsHelpFlagOnly(opts.Args) && !cmd.IsCompletion(opts.Args) && !cmd.IsCompletionScript(opts.Args) {
command, found = cmd.Select(ctx.SelectedCommand.FullCommand(), commands)
if !found {
return command, cmdName, help(vars, err)
Expand All @@ -385,7 +385,12 @@ func processCommandInput(
// command that we can safely append to the arguments and not have to worry
// about it getting removed accidentally in the future as we now have a test
// to validate the shell autocomplete behaviours.
if cmd.IsCompletion(opts.Args) {
//
// Lastly, we don't want to append our hidden shellcomplete command if the
// caller passes --completion-bash because adding a command to the arguments
// list in that scenario would cause Kingpin logic to fail (as it expects the
// flag to be used on its own).
if cmd.IsCompletionScript(opts.Args) {
opts.Args = append(opts.Args, "shellcomplete")
}

Expand All @@ -399,7 +404,7 @@ func processCommandInput(

// Kingpin generates shell completion as a side-effect of kingpin.Parse() so
// we allow it to call os.Exit, only if a completion flag is present.
if cmd.IsCompletion(opts.Args) {
if cmd.IsCompletion(opts.Args) || cmd.IsCompletionScript(opts.Args) {
app.Terminate(os.Exit)
return command, "shell-autocomplete", nil
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
)

var (
completionRegExp = regexp.MustCompile("completion-(?:script-)?(?:bash|zsh)$")
completionRegExp = regexp.MustCompile("completion-bash$")
completionScriptRegExp = regexp.MustCompile("completion-script-(?:bash|zsh)$")
)

// RegisterServiceIDFlag defines a --service-id flag that will attempt to
Expand Down Expand Up @@ -195,8 +196,21 @@ func ContextHasHelpFlag(ctx *kingpin.ParseContext) bool {
return ok
}

// IsCompletionScript determines whether the supplied command arguments are for
// shell completion output that is then eval()'ed by the user's shell.
func IsCompletionScript(args []string) bool {
var found bool
for _, arg := range args {
if completionScriptRegExp.MatchString(arg) {
found = true
}
}
return found
}

// IsCompletion determines whether the supplied command arguments are for
// bash/zsh completion output.
// shell completion (i.e. --completion-bash) that should produce output that
// the user's shell can utilise for handling autocomplete behaviour.
func IsCompletion(args []string) bool {
var found bool
for _, arg := range args {
Expand Down