Skip to content

Commit fac90c7

Browse files
committed
Add completion for buffer-size option.
Disable bash sorting, use pgBackRest output sorting.
1 parent ad87173 commit fac90c7

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

pgbackrest-completion.sh

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22
#
33
# Bash completion support for pgBackRest (https://pgbackrest.org/)
4+
# See 'allow-list' in https://github.com/pgbackrest/pgbackrest/blob/main/src/build/config/config.yaml
45

56
# For all executed commands stderr is sent to /dev/null.
67
# Errors are not needed for completion.
@@ -34,8 +35,28 @@ __pgbackrest_command_options_values_output() {
3435
echo "text"$'\n'"json"
3536
}
3637

37-
# If no stanza - return empty string; nothing to complete
38-
# May be some delays in getting stanza names
38+
# The '--buffer-size' displays values in the user friendly format starting from pgBackRest v2.37.
39+
# In earlier versions, values in bytes will be substituted.
40+
# https://github.com/pgbackrest/pgbackrest/pull/1557
41+
__pgbackrest_command_options_values_buffer_size(){
42+
local buffer_size_option_values
43+
# Regex for valid values like:
44+
# 16384,
45+
# 16777216.
46+
# 16KiB,
47+
# 16MiB.
48+
local size_regex="^[[:digit:]]+([[:alpha:]]+)?[[:punct:]]$"
49+
# Get full string with all values.
50+
local buffer_size_content=$(${script} help ${COMP_WORDS[1]} ${prev#--} 2>/dev/null | awk '/^Allowed values([[:graph:]]|[[:space:]])/ {print $0; getline; print $0}')
51+
# Parse string and add to array result.
52+
for line in ${buffer_size_content}; do
53+
[[ ${line} =~ ${size_regex} ]] && buffer_size_option_values+=(${line/[[:punct:]]/})
54+
done
55+
echo ${buffer_size_option_values[@]}
56+
}
57+
58+
# If no stanza - return empty string; nothing to complete.
59+
# May be some delays in getting stanza names.
3960
__pgbackrest_stanza_values() {
4061
local stanza_values=$(${script} info --output text 2>/dev/null | awk '/^stanza:/ {print $2}')
4162
echo ${stanza_values}
@@ -110,7 +131,7 @@ _pgbackrest() {
110131
return 0;;
111132
repo-ls | repo-get)
112133
# Because '--repo' flag not specified yet,
113-
# Get repo content from the highest priority repository (e.g. repo1)
134+
# get repo content from the highest priority repository (e.g. repo1).
114135
COMPREPLY=($(compgen -W "$(__pgbackrest_repo_content)" -- ${cur}))
115136
compopt -o nospace
116137
return 0;;
@@ -131,6 +152,9 @@ _pgbackrest() {
131152
--output)
132153
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values_output)" -- ${cur}))
133154
return 0;;
155+
--buffer-size)
156+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values_buffer_size)" -- ${cur}))
157+
return 0;;
134158
*)
135159
if [[ ${prev} =~ ${arg_regex} ]]; then
136160
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values)" -- ${cur}))
@@ -164,6 +188,9 @@ _pgbackrest() {
164188
--output)
165189
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values_output)" -- ${cur}))
166190
return 0;;
191+
--buffer-size)
192+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values_buffer_size)" -- ${cur}))
193+
return 0;;
167194
*)
168195
if [[ ${prev} =~ ${arg_regex} ]]; then
169196
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values)" -- ${cur}))
@@ -185,4 +212,5 @@ _pgbackrest() {
185212
esac
186213
}
187214

188-
complete -F _pgbackrest pgbackrest
215+
# -o nosort - disable bash sorting, use pgBackRest output sorting.
216+
complete -o nosort -F _pgbackrest pgbackrest

0 commit comments

Comments
 (0)