Skip to content

Commit

Permalink
update completion scripts for cobra v1.7.0
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Apr 6, 2023
1 parent 45b1099 commit b4e3d3b
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 76 deletions.
65 changes: 39 additions & 26 deletions completions/bash/podman
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__podman_debug()
{
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
fi
}
Expand All @@ -29,7 +29,7 @@ __podman_get_completion_results() {
lastChar=${lastParam:$((${#lastParam}-1)):1}
__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)
# We add an extra empty parameter so we can indicate this to the go method.
__podman_debug "Adding extra empty parameter"
Expand All @@ -39,7 +39,7 @@ __podman_get_completion_results() {
# When completing a flag with an = (e.g., podman -n=<TAB>)
# bash focuses on the part after the =, so we need to remove
# the flag part from $cur
if [[ "${cur}" == -*=* ]]; then
if [[ ${cur} == -*=* ]]; then
cur="${cur#*=}"
fi

Expand All @@ -51,7 +51,7 @@ __podman_get_completion_results() {
directive=${out##*:}
# Remove the directive
out=${out%:*}
if [ "${directive}" = "${out}" ]; then
if [[ ${directive} == "${out}" ]]; then
# There is not directive specified
directive=0
fi
Expand All @@ -65,22 +65,36 @@ __podman_process_completion_results() {
local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32

if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
if (((directive & shellCompDirectiveError) != 0)); then
# Error code. No completion.
__podman_debug "Received error from custom completion go code"
return
else
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
if (((directive & shellCompDirectiveNoSpace) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__podman_debug "Activating no space"
compopt -o nospace
else
__podman_debug "No space directive not supported in this version of bash"
fi
fi
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
if (((directive & shellCompDirectiveKeepOrder) != 0)); 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"
compopt +o default
else
Expand All @@ -94,7 +108,7 @@ __podman_process_completion_results() {
local activeHelp=()
__podman_extract_activeHelp

if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
if (((directive & shellCompDirectiveFilterFileExt) != 0)); then
# File extension filtering
local fullFilter filter filteringCmd

Expand All @@ -107,13 +121,12 @@ __podman_process_completion_results() {
filteringCmd="_filedir $fullFilter"
__podman_debug "File filtering command: $filteringCmd"
$filteringCmd
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
elif (((directive & shellCompDirectiveFilterDirs) != 0)); then
# File completion for directories only

# Use printf to strip any trailing newline
local subdir
subdir=$(printf "%s" "${completions[0]}")
if [ -n "$subdir" ]; then
subdir=${completions[0]}
if [[ -n $subdir ]]; then
__podman_debug "Listing directories in $subdir"
pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
else
Expand All @@ -128,7 +141,7 @@ __podman_process_completion_results() {
__podman_handle_special_char "$cur" =

# Print the activeHelp statements before we finish
if [ ${#activeHelp[*]} -ne 0 ]; then
if ((${#activeHelp[*]} != 0)); then
printf "\n";
printf "%s\n" "${activeHelp[@]}"
printf "\n"
Expand All @@ -152,17 +165,17 @@ __podman_extract_activeHelp() {
local endIndex=${#activeHelpMarker}

while IFS='' read -r comp; do
if [ "${comp:0:endIndex}" = "$activeHelpMarker" ]; then
if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
comp=${comp:endIndex}
__podman_debug "ActiveHelp found: $comp"
if [ -n "$comp" ]; then
if [[ -n $comp ]]; then
activeHelp+=("$comp")
fi
else
# Not an activeHelp line but a normal completion
completions+=("$comp")
fi
done < <(printf "%s\n" "${out}")
done <<<"${out}"
}

__podman_handle_completion_types() {
Expand Down Expand Up @@ -218,7 +231,7 @@ __podman_handle_standard_completion_case() {
done < <(printf "%s\n" "${completions[@]}")

# 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]}"
comp="${COMPREPLY[0]%%$tab*}"
__podman_debug "Removed description from single completion, which is now: ${comp}"
Expand All @@ -235,8 +248,8 @@ __podman_handle_special_char()
if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
local word=${comp%"${comp##*${char}}"}
local idx=${#COMPREPLY[*]}
while [[ $((--idx)) -ge 0 ]]; do
COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"}
while ((--idx >= 0)); do
COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}
done
fi
}
Expand All @@ -262,7 +275,7 @@ __podman_format_comp_descriptions()

# Make sure we can fit a description of at least 8 characters
# 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
for ((i = ${#comp} ; i < longest ; i++)); do
comp+=" "
Expand All @@ -274,8 +287,8 @@ __podman_format_comp_descriptions()

# If there is enough space for any description text,
# truncate the descriptions that are too long for the shell width
if [ $maxdesclength -gt 0 ]; then
if [ ${#desc} -gt $maxdesclength ]; then
if ((maxdesclength > 0)); then
if ((${#desc} > maxdesclength)); then
desc=${desc:0:$(( maxdesclength - 1 ))}
desc+=""
fi
Expand All @@ -296,9 +309,9 @@ __start_podman()
# Call _init_completion from the bash-completion package
# to prepare the arguments properly
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
_init_completion -n =: || return
else
__podman_init_completion -n "=:" || return
__podman_init_completion -n =: || return
fi

__podman_debug
Expand Down
65 changes: 39 additions & 26 deletions completions/bash/podman-remote
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__podman-remote_debug()
{
if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then
echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
fi
}
Expand All @@ -29,7 +29,7 @@ __podman-remote_get_completion_results() {
lastChar=${lastParam:$((${#lastParam}-1)):1}
__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)
# We add an extra empty parameter so we can indicate this to the go method.
__podman-remote_debug "Adding extra empty parameter"
Expand All @@ -39,7 +39,7 @@ __podman-remote_get_completion_results() {
# When completing a flag with an = (e.g., podman-remote -n=<TAB>)
# bash focuses on the part after the =, so we need to remove
# the flag part from $cur
if [[ "${cur}" == -*=* ]]; then
if [[ ${cur} == -*=* ]]; then
cur="${cur#*=}"
fi

Expand All @@ -51,7 +51,7 @@ __podman-remote_get_completion_results() {
directive=${out##*:}
# Remove the directive
out=${out%:*}
if [ "${directive}" = "${out}" ]; then
if [[ ${directive} == "${out}" ]]; then
# There is not directive specified
directive=0
fi
Expand All @@ -65,22 +65,36 @@ __podman-remote_process_completion_results() {
local shellCompDirectiveNoFileComp=4
local shellCompDirectiveFilterFileExt=8
local shellCompDirectiveFilterDirs=16
local shellCompDirectiveKeepOrder=32

if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
if (((directive & shellCompDirectiveError) != 0)); then
# Error code. No completion.
__podman-remote_debug "Received error from custom completion go code"
return
else
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
if (((directive & shellCompDirectiveNoSpace) != 0)); then
if [[ $(type -t compopt) == builtin ]]; then
__podman-remote_debug "Activating no space"
compopt -o nospace
else
__podman-remote_debug "No space directive not supported in this version of bash"
fi
fi
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
if [[ $(type -t compopt) = "builtin" ]]; then
if (((directive & shellCompDirectiveKeepOrder) != 0)); 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"
compopt +o default
else
Expand All @@ -94,7 +108,7 @@ __podman-remote_process_completion_results() {
local activeHelp=()
__podman-remote_extract_activeHelp

if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
if (((directive & shellCompDirectiveFilterFileExt) != 0)); then
# File extension filtering
local fullFilter filter filteringCmd

Expand All @@ -107,13 +121,12 @@ __podman-remote_process_completion_results() {
filteringCmd="_filedir $fullFilter"
__podman-remote_debug "File filtering command: $filteringCmd"
$filteringCmd
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
elif (((directive & shellCompDirectiveFilterDirs) != 0)); then
# File completion for directories only

# Use printf to strip any trailing newline
local subdir
subdir=$(printf "%s" "${completions[0]}")
if [ -n "$subdir" ]; then
subdir=${completions[0]}
if [[ -n $subdir ]]; then
__podman-remote_debug "Listing directories in $subdir"
pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
else
Expand All @@ -128,7 +141,7 @@ __podman-remote_process_completion_results() {
__podman-remote_handle_special_char "$cur" =

# Print the activeHelp statements before we finish
if [ ${#activeHelp[*]} -ne 0 ]; then
if ((${#activeHelp[*]} != 0)); then
printf "\n";
printf "%s\n" "${activeHelp[@]}"
printf "\n"
Expand All @@ -152,17 +165,17 @@ __podman-remote_extract_activeHelp() {
local endIndex=${#activeHelpMarker}

while IFS='' read -r comp; do
if [ "${comp:0:endIndex}" = "$activeHelpMarker" ]; then
if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then
comp=${comp:endIndex}
__podman-remote_debug "ActiveHelp found: $comp"
if [ -n "$comp" ]; then
if [[ -n $comp ]]; then
activeHelp+=("$comp")
fi
else
# Not an activeHelp line but a normal completion
completions+=("$comp")
fi
done < <(printf "%s\n" "${out}")
done <<<"${out}"
}

__podman-remote_handle_completion_types() {
Expand Down Expand Up @@ -218,7 +231,7 @@ __podman-remote_handle_standard_completion_case() {
done < <(printf "%s\n" "${completions[@]}")

# 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]}"
comp="${COMPREPLY[0]%%$tab*}"
__podman-remote_debug "Removed description from single completion, which is now: ${comp}"
Expand All @@ -235,8 +248,8 @@ __podman-remote_handle_special_char()
if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then
local word=${comp%"${comp##*${char}}"}
local idx=${#COMPREPLY[*]}
while [[ $((--idx)) -ge 0 ]]; do
COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"}
while ((--idx >= 0)); do
COMPREPLY[idx]=${COMPREPLY[idx]#"$word"}
done
fi
}
Expand All @@ -262,7 +275,7 @@ __podman-remote_format_comp_descriptions()

# Make sure we can fit a description of at least 8 characters
# 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
for ((i = ${#comp} ; i < longest ; i++)); do
comp+=" "
Expand All @@ -274,8 +287,8 @@ __podman-remote_format_comp_descriptions()

# If there is enough space for any description text,
# truncate the descriptions that are too long for the shell width
if [ $maxdesclength -gt 0 ]; then
if [ ${#desc} -gt $maxdesclength ]; then
if ((maxdesclength > 0)); then
if ((${#desc} > maxdesclength)); then
desc=${desc:0:$(( maxdesclength - 1 ))}
desc+=""
fi
Expand All @@ -296,9 +309,9 @@ __start_podman-remote()
# Call _init_completion from the bash-completion package
# to prepare the arguments properly
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
_init_completion -n =: || return
else
__podman-remote_init_completion -n "=:" || return
__podman-remote_init_completion -n =: || return
fi

__podman-remote_debug
Expand Down
Loading

0 comments on commit b4e3d3b

Please sign in to comment.