update completion scripts for cobra v1.7.0

[NO NEW TESTS NEEDED]

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
pull/18061/head
Paul Holzinger 2023-04-05 14:00:41 +02:00
parent 45b1099eff
commit b858c7e7fa
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
8 changed files with 262 additions and 76 deletions

View File

@ -2,7 +2,7 @@
__podman_debug() __podman_debug()
{ {
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
echo "$*" >> "${BASH_COMP_DEBUG_FILE}" echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
fi fi
} }
@ -29,7 +29,7 @@ __podman_get_completion_results() {
lastChar=${lastParam:$((${#lastParam}-1)):1} lastChar=${lastParam:$((${#lastParam}-1)):1}
__podman_debug "lastParam ${lastParam}, lastChar ${lastChar}" __podman_debug "lastParam ${lastParam}, lastChar ${lastChar}"
if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then if [[ -z ${cur} && ${lastChar} != = ]]; then
# If the last parameter is complete (there is a space following it) # If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method. # We add an extra empty parameter so we can indicate this to the go method.
__podman_debug "Adding extra empty parameter" __podman_debug "Adding extra empty parameter"
@ -39,7 +39,7 @@ __podman_get_completion_results() {
# When completing a flag with an = (e.g., podman -n=<TAB>) # When completing a flag with an = (e.g., podman -n=<TAB>)
# bash focuses on the part after the =, so we need to remove # bash focuses on the part after the =, so we need to remove
# the flag part from $cur # the flag part from $cur
if [[ "${cur}" == -*=* ]]; then if [[ ${cur} == -*=* ]]; then
cur="${cur#*=}" cur="${cur#*=}"
fi fi
@ -51,7 +51,7 @@ __podman_get_completion_results() {
directive=${out##*:} directive=${out##*:}
# Remove the directive # Remove the directive
out=${out%:*} out=${out%:*}
if [ "${directive}" = "${out}" ]; then if [[ ${directive} == "${out}" ]]; then
# There is not directive specified # There is not directive specified
directive=0 directive=0
fi fi
@ -65,22 +65,36 @@ __podman_process_completion_results() {
local shellCompDirectiveNoFileComp=4 local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8 local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16 local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then if (((directive & shellCompDirectiveError) != 0)); then
# Error code. No completion. # Error code. No completion.
__podman_debug "Received error from custom completion go code" __podman_debug "Received error from custom completion go code"
return return
else else
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then if (((directive & shellCompDirectiveNoSpace) != 0)); then
if [[ $(type -t compopt) = "builtin" ]]; then if [[ $(type -t compopt) == builtin ]]; then
__podman_debug "Activating no space" __podman_debug "Activating no space"
compopt -o nospace compopt -o nospace
else else
__podman_debug "No space directive not supported in this version of bash" __podman_debug "No space directive not supported in this version of bash"
fi fi
fi fi
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then if (((directive & shellCompDirectiveKeepOrder) != 0)); then
if [[ $(type -t compopt) = "builtin" ]]; then if [[ $(type -t compopt) == builtin ]]; then
# no sort isn't supported for bash less than < 4.4
if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then
__podman_debug "No sort directive not supported in this version of bash"
else
__podman_debug "Activating keep order"
compopt -o nosort
fi
else
__podman_debug "No sort directive not supported in this version of bash"
fi
fi
if (((directive & shellCompDirectiveNoFileComp) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__podman_debug "Activating no file completion" __podman_debug "Activating no file completion"
compopt +o default compopt +o default
else else
@ -94,7 +108,7 @@ __podman_process_completion_results() {
local activeHelp=() local activeHelp=()
__podman_extract_activeHelp __podman_extract_activeHelp
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then if (((directive & shellCompDirectiveFilterFileExt) != 0)); then
# File extension filtering # File extension filtering
local fullFilter filter filteringCmd local fullFilter filter filteringCmd
@ -107,13 +121,12 @@ __podman_process_completion_results() {
filteringCmd="_filedir $fullFilter" filteringCmd="_filedir $fullFilter"
__podman_debug "File filtering command: $filteringCmd" __podman_debug "File filtering command: $filteringCmd"
$filteringCmd $filteringCmd
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then elif (((directive & shellCompDirectiveFilterDirs) != 0)); then
# File completion for directories only # File completion for directories only
# Use printf to strip any trailing newline
local subdir local subdir
subdir=$(printf "%s" "${completions[0]}") subdir=${completions[0]}
if [ -n "$subdir" ]; then if [[ -n $subdir ]]; then
__podman_debug "Listing directories in $subdir" __podman_debug "Listing directories in $subdir"
pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
else else
@ -128,7 +141,7 @@ __podman_process_completion_results() {
__podman_handle_special_char "$cur" = __podman_handle_special_char "$cur" =
# Print the activeHelp statements before we finish # Print the activeHelp statements before we finish
if [ ${#activeHelp[*]} -ne 0 ]; then if ((${#activeHelp[*]} != 0)); then
printf "\n"; printf "\n";
printf "%s\n" "${activeHelp[@]}" printf "%s\n" "${activeHelp[@]}"
printf "\n" printf "\n"
@ -152,17 +165,17 @@ __podman_extract_activeHelp() {
local endIndex=${#activeHelpMarker} local endIndex=${#activeHelpMarker}
while IFS='' read -r comp; do while IFS='' read -r comp; do
if [ "${comp:0:endIndex}" = "$activeHelpMarker" ]; then if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
comp=${comp:endIndex} comp=${comp:endIndex}
__podman_debug "ActiveHelp found: $comp" __podman_debug "ActiveHelp found: $comp"
if [ -n "$comp" ]; then if [[ -n $comp ]]; then
activeHelp+=("$comp") activeHelp+=("$comp")
fi fi
else else
# Not an activeHelp line but a normal completion # Not an activeHelp line but a normal completion
completions+=("$comp") completions+=("$comp")
fi fi
done < <(printf "%s\n" "${out}") done <<<"${out}"
} }
__podman_handle_completion_types() { __podman_handle_completion_types() {
@ -218,7 +231,7 @@ __podman_handle_standard_completion_case() {
done < <(printf "%s\n" "${completions[@]}") done < <(printf "%s\n" "${completions[@]}")
# If there is a single completion left, remove the description text # If there is a single completion left, remove the description text
if [ ${#COMPREPLY[*]} -eq 1 ]; then if ((${#COMPREPLY[*]} == 1)); then
__podman_debug "COMPREPLY[0]: ${COMPREPLY[0]}" __podman_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
comp="${COMPREPLY[0]%%$tab*}" comp="${COMPREPLY[0]%%$tab*}"
__podman_debug "Removed description from single completion, which is now: ${comp}" __podman_debug "Removed description from single completion, which is now: ${comp}"
@ -235,8 +248,8 @@ __podman_handle_special_char()
if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
local word=${comp%"${comp##*${char}}"} local word=${comp%"${comp##*${char}}"}
local idx=${#COMPREPLY[*]} local idx=${#COMPREPLY[*]}
while [[ $((--idx)) -ge 0 ]]; do while ((--idx >= 0)); do
COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"} COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}
done done
fi fi
} }
@ -262,7 +275,7 @@ __podman_format_comp_descriptions()
# Make sure we can fit a description of at least 8 characters # Make sure we can fit a description of at least 8 characters
# if we are to align the descriptions. # if we are to align the descriptions.
if [[ $maxdesclength -gt 8 ]]; then if ((maxdesclength > 8)); then
# Add the proper number of spaces to align the descriptions # Add the proper number of spaces to align the descriptions
for ((i = ${#comp} ; i < longest ; i++)); do for ((i = ${#comp} ; i < longest ; i++)); do
comp+=" " comp+=" "
@ -274,8 +287,8 @@ __podman_format_comp_descriptions()
# If there is enough space for any description text, # If there is enough space for any description text,
# truncate the descriptions that are too long for the shell width # truncate the descriptions that are too long for the shell width
if [ $maxdesclength -gt 0 ]; then if ((maxdesclength > 0)); then
if [ ${#desc} -gt $maxdesclength ]; then if ((${#desc} > maxdesclength)); then
desc=${desc:0:$(( maxdesclength - 1 ))} desc=${desc:0:$(( maxdesclength - 1 ))}
desc+="…" desc+="…"
fi fi
@ -296,9 +309,9 @@ __start_podman()
# Call _init_completion from the bash-completion package # Call _init_completion from the bash-completion package
# to prepare the arguments properly # to prepare the arguments properly
if declare -F _init_completion >/dev/null 2>&1; then if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return _init_completion -n =: || return
else else
__podman_init_completion -n "=:" || return __podman_init_completion -n =: || return
fi fi
__podman_debug __podman_debug

View File

@ -2,7 +2,7 @@
__podman-remote_debug() __podman-remote_debug()
{ {
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
echo "$*" >> "${BASH_COMP_DEBUG_FILE}" echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
fi fi
} }
@ -29,7 +29,7 @@ __podman-remote_get_completion_results() {
lastChar=${lastParam:$((${#lastParam}-1)):1} lastChar=${lastParam:$((${#lastParam}-1)):1}
__podman-remote_debug "lastParam ${lastParam}, lastChar ${lastChar}" __podman-remote_debug "lastParam ${lastParam}, lastChar ${lastChar}"
if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then if [[ -z ${cur} && ${lastChar} != = ]]; then
# If the last parameter is complete (there is a space following it) # If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method. # We add an extra empty parameter so we can indicate this to the go method.
__podman-remote_debug "Adding extra empty parameter" __podman-remote_debug "Adding extra empty parameter"
@ -39,7 +39,7 @@ __podman-remote_get_completion_results() {
# When completing a flag with an = (e.g., podman-remote -n=<TAB>) # When completing a flag with an = (e.g., podman-remote -n=<TAB>)
# bash focuses on the part after the =, so we need to remove # bash focuses on the part after the =, so we need to remove
# the flag part from $cur # the flag part from $cur
if [[ "${cur}" == -*=* ]]; then if [[ ${cur} == -*=* ]]; then
cur="${cur#*=}" cur="${cur#*=}"
fi fi
@ -51,7 +51,7 @@ __podman-remote_get_completion_results() {
directive=${out##*:} directive=${out##*:}
# Remove the directive # Remove the directive
out=${out%:*} out=${out%:*}
if [ "${directive}" = "${out}" ]; then if [[ ${directive} == "${out}" ]]; then
# There is not directive specified # There is not directive specified
directive=0 directive=0
fi fi
@ -65,22 +65,36 @@ __podman-remote_process_completion_results() {
local shellCompDirectiveNoFileComp=4 local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8 local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16 local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then if (((directive & shellCompDirectiveError) != 0)); then
# Error code. No completion. # Error code. No completion.
__podman-remote_debug "Received error from custom completion go code" __podman-remote_debug "Received error from custom completion go code"
return return
else else
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then if (((directive & shellCompDirectiveNoSpace) != 0)); then
if [[ $(type -t compopt) = "builtin" ]]; then if [[ $(type -t compopt) == builtin ]]; then
__podman-remote_debug "Activating no space" __podman-remote_debug "Activating no space"
compopt -o nospace compopt -o nospace
else else
__podman-remote_debug "No space directive not supported in this version of bash" __podman-remote_debug "No space directive not supported in this version of bash"
fi fi
fi fi
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then if (((directive & shellCompDirectiveKeepOrder) != 0)); then
if [[ $(type -t compopt) = "builtin" ]]; then if [[ $(type -t compopt) == builtin ]]; then
# no sort isn't supported for bash less than < 4.4
if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then
__podman-remote_debug "No sort directive not supported in this version of bash"
else
__podman-remote_debug "Activating keep order"
compopt -o nosort
fi
else
__podman-remote_debug "No sort directive not supported in this version of bash"
fi
fi
if (((directive & shellCompDirectiveNoFileComp) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__podman-remote_debug "Activating no file completion" __podman-remote_debug "Activating no file completion"
compopt +o default compopt +o default
else else
@ -94,7 +108,7 @@ __podman-remote_process_completion_results() {
local activeHelp=() local activeHelp=()
__podman-remote_extract_activeHelp __podman-remote_extract_activeHelp
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then if (((directive & shellCompDirectiveFilterFileExt) != 0)); then
# File extension filtering # File extension filtering
local fullFilter filter filteringCmd local fullFilter filter filteringCmd
@ -107,13 +121,12 @@ __podman-remote_process_completion_results() {
filteringCmd="_filedir $fullFilter" filteringCmd="_filedir $fullFilter"
__podman-remote_debug "File filtering command: $filteringCmd" __podman-remote_debug "File filtering command: $filteringCmd"
$filteringCmd $filteringCmd
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then elif (((directive & shellCompDirectiveFilterDirs) != 0)); then
# File completion for directories only # File completion for directories only
# Use printf to strip any trailing newline
local subdir local subdir
subdir=$(printf "%s" "${completions[0]}") subdir=${completions[0]}
if [ -n "$subdir" ]; then if [[ -n $subdir ]]; then
__podman-remote_debug "Listing directories in $subdir" __podman-remote_debug "Listing directories in $subdir"
pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
else else
@ -128,7 +141,7 @@ __podman-remote_process_completion_results() {
__podman-remote_handle_special_char "$cur" = __podman-remote_handle_special_char "$cur" =
# Print the activeHelp statements before we finish # Print the activeHelp statements before we finish
if [ ${#activeHelp[*]} -ne 0 ]; then if ((${#activeHelp[*]} != 0)); then
printf "\n"; printf "\n";
printf "%s\n" "${activeHelp[@]}" printf "%s\n" "${activeHelp[@]}"
printf "\n" printf "\n"
@ -152,17 +165,17 @@ __podman-remote_extract_activeHelp() {
local endIndex=${#activeHelpMarker} local endIndex=${#activeHelpMarker}
while IFS='' read -r comp; do while IFS='' read -r comp; do
if [ "${comp:0:endIndex}" = "$activeHelpMarker" ]; then if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
comp=${comp:endIndex} comp=${comp:endIndex}
__podman-remote_debug "ActiveHelp found: $comp" __podman-remote_debug "ActiveHelp found: $comp"
if [ -n "$comp" ]; then if [[ -n $comp ]]; then
activeHelp+=("$comp") activeHelp+=("$comp")
fi fi
else else
# Not an activeHelp line but a normal completion # Not an activeHelp line but a normal completion
completions+=("$comp") completions+=("$comp")
fi fi
done < <(printf "%s\n" "${out}") done <<<"${out}"
} }
__podman-remote_handle_completion_types() { __podman-remote_handle_completion_types() {
@ -218,7 +231,7 @@ __podman-remote_handle_standard_completion_case() {
done < <(printf "%s\n" "${completions[@]}") done < <(printf "%s\n" "${completions[@]}")
# If there is a single completion left, remove the description text # If there is a single completion left, remove the description text
if [ ${#COMPREPLY[*]} -eq 1 ]; then if ((${#COMPREPLY[*]} == 1)); then
__podman-remote_debug "COMPREPLY[0]: ${COMPREPLY[0]}" __podman-remote_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
comp="${COMPREPLY[0]%%$tab*}" comp="${COMPREPLY[0]%%$tab*}"
__podman-remote_debug "Removed description from single completion, which is now: ${comp}" __podman-remote_debug "Removed description from single completion, which is now: ${comp}"
@ -235,8 +248,8 @@ __podman-remote_handle_special_char()
if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
local word=${comp%"${comp##*${char}}"} local word=${comp%"${comp##*${char}}"}
local idx=${#COMPREPLY[*]} local idx=${#COMPREPLY[*]}
while [[ $((--idx)) -ge 0 ]]; do while ((--idx >= 0)); do
COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"} COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}
done done
fi fi
} }
@ -262,7 +275,7 @@ __podman-remote_format_comp_descriptions()
# Make sure we can fit a description of at least 8 characters # Make sure we can fit a description of at least 8 characters
# if we are to align the descriptions. # if we are to align the descriptions.
if [[ $maxdesclength -gt 8 ]]; then if ((maxdesclength > 8)); then
# Add the proper number of spaces to align the descriptions # Add the proper number of spaces to align the descriptions
for ((i = ${#comp} ; i < longest ; i++)); do for ((i = ${#comp} ; i < longest ; i++)); do
comp+=" " comp+=" "
@ -274,8 +287,8 @@ __podman-remote_format_comp_descriptions()
# If there is enough space for any description text, # If there is enough space for any description text,
# truncate the descriptions that are too long for the shell width # truncate the descriptions that are too long for the shell width
if [ $maxdesclength -gt 0 ]; then if ((maxdesclength > 0)); then
if [ ${#desc} -gt $maxdesclength ]; then if ((${#desc} > maxdesclength)); then
desc=${desc:0:$(( maxdesclength - 1 ))} desc=${desc:0:$(( maxdesclength - 1 ))}
desc+="…" desc+="…"
fi fi
@ -296,9 +309,9 @@ __start_podman-remote()
# Call _init_completion from the bash-completion package # Call _init_completion from the bash-completion package
# to prepare the arguments properly # to prepare the arguments properly
if declare -F _init_completion >/dev/null 2>&1; then if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return _init_completion -n =: || return
else else
__podman-remote_init_completion -n "=:" || return __podman-remote_init_completion -n =: || return
fi fi
__podman-remote_debug __podman-remote_debug

View File

@ -55,6 +55,60 @@ function __podman_remote_perform_completion
printf "%s\n" "$directiveLine" printf "%s\n" "$directiveLine"
end end
# this function limits calls to __podman_remote_perform_completion, by caching the result behind $__podman_remote_perform_completion_once_result
function __podman_remote_perform_completion_once
__podman_remote_debug "Starting __podman_remote_perform_completion_once"
if test -n "$__podman_remote_perform_completion_once_result"
__podman_remote_debug "Seems like a valid result already exists, skipping __podman_remote_perform_completion"
return 0
end
set --global __podman_remote_perform_completion_once_result (__podman_remote_perform_completion)
if test -z "$__podman_remote_perform_completion_once_result"
__podman_remote_debug "No completions, probably due to a failure"
return 1
end
__podman_remote_debug "Performed completions and set __podman_remote_perform_completion_once_result"
return 0
end
# this function is used to clear the $__podman_remote_perform_completion_once_result variable after completions are run
function __podman_remote_clear_perform_completion_once_result
__podman_remote_debug ""
__podman_remote_debug "========= clearing previously set __podman_remote_perform_completion_once_result variable =========="
set --erase __podman_remote_perform_completion_once_result
__podman_remote_debug "Succesfully erased the variable __podman_remote_perform_completion_once_result"
end
function __podman_remote_requires_order_preservation
__podman_remote_debug ""
__podman_remote_debug "========= checking if order preservation is required =========="
__podman_remote_perform_completion_once
if test -z "$__podman_remote_perform_completion_once_result"
__podman_remote_debug "Error determining if order preservation is required"
return 1
end
set -l directive (string sub --start 2 $__podman_remote_perform_completion_once_result[-1])
__podman_remote_debug "Directive is: $directive"
set -l shellCompDirectiveKeepOrder 32
set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2)
__podman_remote_debug "Keeporder is: $keeporder"
if test $keeporder -ne 0
__podman_remote_debug "This does require order preservation"
return 0
end
__podman_remote_debug "This doesn't require order preservation"
return 1
end
# This function does two things: # This function does two things:
# - Obtain the completions and store them in the global __podman_remote_comp_results # - Obtain the completions and store them in the global __podman_remote_comp_results
# - Return false if file completion should be performed # - Return false if file completion should be performed
@ -65,17 +119,17 @@ function __podman_remote_prepare_completions
# Start fresh # Start fresh
set --erase __podman_remote_comp_results set --erase __podman_remote_comp_results
set -l results (__podman_remote_perform_completion) __podman_remote_perform_completion_once
__podman_remote_debug "Completion results: $results" __podman_remote_debug "Completion results: $__podman_remote_perform_completion_once_result"
if test -z "$results" if test -z "$__podman_remote_perform_completion_once_result"
__podman_remote_debug "No completion, probably due to a failure" __podman_remote_debug "No completion, probably due to a failure"
# Might as well do file completion, in case it helps # Might as well do file completion, in case it helps
return 1 return 1
end end
set -l directive (string sub --start 2 $results[-1]) set -l directive (string sub --start 2 $__podman_remote_perform_completion_once_result[-1])
set --global __podman_remote_comp_results $results[1..-2] set --global __podman_remote_comp_results $__podman_remote_perform_completion_once_result[1..-2]
__podman_remote_debug "Completions are: $__podman_remote_comp_results" __podman_remote_debug "Completions are: $__podman_remote_comp_results"
__podman_remote_debug "Directive is: $directive" __podman_remote_debug "Directive is: $directive"
@ -171,9 +225,13 @@ end
# Remove any pre-existing completions for the program since we will be handling all of them. # Remove any pre-existing completions for the program since we will be handling all of them.
complete -c podman-remote -e complete -c podman-remote -e
# this will get called after the two calls below and clear the $__podman_remote_perform_completion_once_result global
complete -c podman-remote -n '__podman_remote_clear_perform_completion_once_result'
# The call to __podman_remote_prepare_completions will setup __podman_remote_comp_results # The call to __podman_remote_prepare_completions will setup __podman_remote_comp_results
# which provides the program's completion choices. # which provides the program's completion choices.
complete -c podman-remote -n '__podman_remote_prepare_completions' -f -a '$__podman_remote_comp_results' # If this doesn't require order preservation, we don't use the -k flag
complete -c podman-remote -n 'not __podman_remote_requires_order_preservation && __podman_remote_prepare_completions' -f -a '$__podman_remote_comp_results'
# otherwise we use the -k flag
complete -k -c podman-remote -n '__podman_remote_requires_order_preservation && __podman_remote_prepare_completions' -f -a '$__podman_remote_comp_results'
# This file is generated with "podman-remote completion"; see: podman-completion(1) # This file is generated with "podman-remote completion"; see: podman-completion(1)

View File

@ -55,6 +55,60 @@ function __podman_perform_completion
printf "%s\n" "$directiveLine" printf "%s\n" "$directiveLine"
end end
# this function limits calls to __podman_perform_completion, by caching the result behind $__podman_perform_completion_once_result
function __podman_perform_completion_once
__podman_debug "Starting __podman_perform_completion_once"
if test -n "$__podman_perform_completion_once_result"
__podman_debug "Seems like a valid result already exists, skipping __podman_perform_completion"
return 0
end
set --global __podman_perform_completion_once_result (__podman_perform_completion)
if test -z "$__podman_perform_completion_once_result"
__podman_debug "No completions, probably due to a failure"
return 1
end
__podman_debug "Performed completions and set __podman_perform_completion_once_result"
return 0
end
# this function is used to clear the $__podman_perform_completion_once_result variable after completions are run
function __podman_clear_perform_completion_once_result
__podman_debug ""
__podman_debug "========= clearing previously set __podman_perform_completion_once_result variable =========="
set --erase __podman_perform_completion_once_result
__podman_debug "Succesfully erased the variable __podman_perform_completion_once_result"
end
function __podman_requires_order_preservation
__podman_debug ""
__podman_debug "========= checking if order preservation is required =========="
__podman_perform_completion_once
if test -z "$__podman_perform_completion_once_result"
__podman_debug "Error determining if order preservation is required"
return 1
end
set -l directive (string sub --start 2 $__podman_perform_completion_once_result[-1])
__podman_debug "Directive is: $directive"
set -l shellCompDirectiveKeepOrder 32
set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) % 2)
__podman_debug "Keeporder is: $keeporder"
if test $keeporder -ne 0
__podman_debug "This does require order preservation"
return 0
end
__podman_debug "This doesn't require order preservation"
return 1
end
# This function does two things: # This function does two things:
# - Obtain the completions and store them in the global __podman_comp_results # - Obtain the completions and store them in the global __podman_comp_results
# - Return false if file completion should be performed # - Return false if file completion should be performed
@ -65,17 +119,17 @@ function __podman_prepare_completions
# Start fresh # Start fresh
set --erase __podman_comp_results set --erase __podman_comp_results
set -l results (__podman_perform_completion) __podman_perform_completion_once
__podman_debug "Completion results: $results" __podman_debug "Completion results: $__podman_perform_completion_once_result"
if test -z "$results" if test -z "$__podman_perform_completion_once_result"
__podman_debug "No completion, probably due to a failure" __podman_debug "No completion, probably due to a failure"
# Might as well do file completion, in case it helps # Might as well do file completion, in case it helps
return 1 return 1
end end
set -l directive (string sub --start 2 $results[-1]) set -l directive (string sub --start 2 $__podman_perform_completion_once_result[-1])
set --global __podman_comp_results $results[1..-2] set --global __podman_comp_results $__podman_perform_completion_once_result[1..-2]
__podman_debug "Completions are: $__podman_comp_results" __podman_debug "Completions are: $__podman_comp_results"
__podman_debug "Directive is: $directive" __podman_debug "Directive is: $directive"
@ -171,9 +225,13 @@ end
# Remove any pre-existing completions for the program since we will be handling all of them. # Remove any pre-existing completions for the program since we will be handling all of them.
complete -c podman -e complete -c podman -e
# this will get called after the two calls below and clear the $__podman_perform_completion_once_result global
complete -c podman -n '__podman_clear_perform_completion_once_result'
# The call to __podman_prepare_completions will setup __podman_comp_results # The call to __podman_prepare_completions will setup __podman_comp_results
# which provides the program's completion choices. # which provides the program's completion choices.
complete -c podman -n '__podman_prepare_completions' -f -a '$__podman_comp_results' # If this doesn't require order preservation, we don't use the -k flag
complete -c podman -n 'not __podman_requires_order_preservation && __podman_prepare_completions' -f -a '$__podman_comp_results'
# otherwise we use the -k flag
complete -k -c podman -n '__podman_requires_order_preservation && __podman_prepare_completions' -f -a '$__podman_comp_results'
# This file is generated with "podman completion"; see: podman-completion(1) # This file is generated with "podman completion"; see: podman-completion(1)

View File

@ -40,6 +40,7 @@ filter __podman-remote_escapeStringWithSpecialChars {
$ShellCompDirectiveNoFileComp=4 $ShellCompDirectiveNoFileComp=4
$ShellCompDirectiveFilterFileExt=8 $ShellCompDirectiveFilterFileExt=8
$ShellCompDirectiveFilterDirs=16 $ShellCompDirectiveFilterDirs=16
$ShellCompDirectiveKeepOrder=32
# Prepare the command to request completions for the program. # Prepare the command to request completions for the program.
# Split the command at the first space to separate the program and arguments. # Split the command at the first space to separate the program and arguments.
@ -69,8 +70,17 @@ filter __podman-remote_escapeStringWithSpecialChars {
# If the last parameter is complete (there is a space following it) # If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method. # We add an extra empty parameter so we can indicate this to the go method.
__podman-remote_debug "Adding extra empty parameter" __podman-remote_debug "Adding extra empty parameter"
# We need to use `"`" to pass an empty argument a "" or '' does not work!!! # PowerShell 7.2+ changed the way how the arguments are passed to executables,
$RequestComp="$RequestComp" + ' `"`"' # so for pre-7.2 or when Legacy argument passing is enabled we need to use
# `"`" to pass an empty argument, a "" or '' does not work!!!
if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or
($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or
(($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and
$PSNativeCommandArgumentPassing -eq 'Legacy')) {
$RequestComp="$RequestComp" + ' `"`"'
} else {
$RequestComp="$RequestComp" + ' ""'
}
} }
__podman-remote_debug "Calling $RequestComp" __podman-remote_debug "Calling $RequestComp"
@ -100,7 +110,7 @@ filter __podman-remote_escapeStringWithSpecialChars {
} }
$Longest = 0 $Longest = 0
$Values = $Out | ForEach-Object { [Array]$Values = $Out | ForEach-Object {
#Split the output in name and description #Split the output in name and description
$Name, $Description = $_.Split("`t",2) $Name, $Description = $_.Split("`t",2)
__podman-remote_debug "Name: $Name Description: $Description" __podman-remote_debug "Name: $Name Description: $Description"
@ -145,6 +155,11 @@ filter __podman-remote_escapeStringWithSpecialChars {
} }
} }
# we sort the values in ascending order by name if keep order isn't passed
if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) {
$Values = $Values | Sort-Object -Property Name
}
if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) { if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
__podman-remote_debug "ShellCompDirectiveNoFileComp is called" __podman-remote_debug "ShellCompDirectiveNoFileComp is called"

View File

@ -40,6 +40,7 @@ filter __podman_escapeStringWithSpecialChars {
$ShellCompDirectiveNoFileComp=4 $ShellCompDirectiveNoFileComp=4
$ShellCompDirectiveFilterFileExt=8 $ShellCompDirectiveFilterFileExt=8
$ShellCompDirectiveFilterDirs=16 $ShellCompDirectiveFilterDirs=16
$ShellCompDirectiveKeepOrder=32
# Prepare the command to request completions for the program. # Prepare the command to request completions for the program.
# Split the command at the first space to separate the program and arguments. # Split the command at the first space to separate the program and arguments.
@ -69,8 +70,17 @@ filter __podman_escapeStringWithSpecialChars {
# If the last parameter is complete (there is a space following it) # If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method. # We add an extra empty parameter so we can indicate this to the go method.
__podman_debug "Adding extra empty parameter" __podman_debug "Adding extra empty parameter"
# We need to use `"`" to pass an empty argument a "" or '' does not work!!! # PowerShell 7.2+ changed the way how the arguments are passed to executables,
$RequestComp="$RequestComp" + ' `"`"' # so for pre-7.2 or when Legacy argument passing is enabled we need to use
# `"`" to pass an empty argument, a "" or '' does not work!!!
if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or
($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or
(($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and
$PSNativeCommandArgumentPassing -eq 'Legacy')) {
$RequestComp="$RequestComp" + ' `"`"'
} else {
$RequestComp="$RequestComp" + ' ""'
}
} }
__podman_debug "Calling $RequestComp" __podman_debug "Calling $RequestComp"
@ -100,7 +110,7 @@ filter __podman_escapeStringWithSpecialChars {
} }
$Longest = 0 $Longest = 0
$Values = $Out | ForEach-Object { [Array]$Values = $Out | ForEach-Object {
#Split the output in name and description #Split the output in name and description
$Name, $Description = $_.Split("`t",2) $Name, $Description = $_.Split("`t",2)
__podman_debug "Name: $Name Description: $Description" __podman_debug "Name: $Name Description: $Description"
@ -145,6 +155,11 @@ filter __podman_escapeStringWithSpecialChars {
} }
} }
# we sort the values in ascending order by name if keep order isn't passed
if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) {
$Values = $Values | Sort-Object -Property Name
}
if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) { if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
__podman_debug "ShellCompDirectiveNoFileComp is called" __podman_debug "ShellCompDirectiveNoFileComp is called"

View File

@ -1,4 +1,5 @@
#compdef podman #compdef podman
compdef _podman podman
# zsh completion for podman -*- shell-script -*- # zsh completion for podman -*- shell-script -*-
@ -17,8 +18,9 @@ _podman()
local shellCompDirectiveNoFileComp=4 local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8 local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16 local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
local -a completions local -a completions
__podman_debug "\n========= starting completion logic ==========" __podman_debug "\n========= starting completion logic =========="
@ -136,6 +138,11 @@ _podman()
noSpace="-S ''" noSpace="-S ''"
fi fi
if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
__podman_debug "Activating keep order."
keepOrder="-V"
fi
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
# File extension filtering # File extension filtering
local filteringCmd local filteringCmd
@ -171,7 +178,7 @@ _podman()
return $result return $result
else else
__podman_debug "Calling _describe" __podman_debug "Calling _describe"
if eval _describe "completions" completions $flagPrefix $noSpace; then if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then
__podman_debug "_describe found some completions" __podman_debug "_describe found some completions"
# Return the success of having called _describe # Return the success of having called _describe

View File

@ -1,4 +1,5 @@
#compdef podman-remote #compdef podman-remote
compdef _podman-remote podman-remote
# zsh completion for podman-remote -*- shell-script -*- # zsh completion for podman-remote -*- shell-script -*-
@ -17,8 +18,9 @@ _podman-remote()
local shellCompDirectiveNoFileComp=4 local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8 local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16 local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
local -a completions local -a completions
__podman-remote_debug "\n========= starting completion logic ==========" __podman-remote_debug "\n========= starting completion logic =========="
@ -136,6 +138,11 @@ _podman-remote()
noSpace="-S ''" noSpace="-S ''"
fi fi
if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
__podman-remote_debug "Activating keep order."
keepOrder="-V"
fi
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
# File extension filtering # File extension filtering
local filteringCmd local filteringCmd
@ -171,7 +178,7 @@ _podman-remote()
return $result return $result
else else
__podman-remote_debug "Calling _describe" __podman-remote_debug "Calling _describe"
if eval _describe "completions" completions $flagPrefix $noSpace; then if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then
__podman-remote_debug "_describe found some completions" __podman-remote_debug "_describe found some completions"
# Return the success of having called _describe # Return the success of having called _describe