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 plugin completion results parsing for ShellCompDirectiveFilterFileExt #4136

Merged
merged 1 commit into from
Apr 4, 2023
Merged
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
15 changes: 14 additions & 1 deletion contrib/completion/bash/docker
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,20 @@ __docker_complete_plugin() {
resultArray+=( "$value" )
fi
done
local result=$(eval "${resultArray[*]}" 2> /dev/null | grep -v '^:[0-9]*$')
local rawResult=$(eval "${resultArray[*]}" 2> /dev/null)
local result=$(grep -v '^:[0-9]*$' <<< "$rawResult")

# Compose V2 completions sometimes returns returns `:8` (ShellCompDirectiveFilterFileExt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone else reviewing this was unfamiliar with what this means, it's a Cobra thing, and https://github.com/spf13/cobra/blob/4dd4b25de38418174a6e859e8a32eaccca32dccc/completions_test.go#L1046-L1047 is the best reference I could find for it being :8 (there's probably a better one, but that's what my search brought me to 😅).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'd thought about adding some link explaining it better and then totally forgot :|

# with the expected file extensions (such as `yml`, `yaml`) to indicate that the shell should
# provide autocompletions for files with matching extensions
local completionFlag=$(tail -1 <<< "$rawResult")
if [ "$completionFlag" == ":8" ]; then
# format a valid glob pattern for the provided file extensions
local filePattern=$(tr '\n' '|' <<< "$result")

_filedir "$filePattern"
return
fi

# if result empty, just use filename completion as fallback
if [ -z "$result" ]; then
Expand Down