Skip to content

Commit e001384

Browse files
authored
add completion for repo-ls/repo-get commands
2 parents a12c8b1 + 533214f commit e001384

File tree

1 file changed

+99
-38
lines changed

1 file changed

+99
-38
lines changed

pgbackrest-completion.sh

Lines changed: 99 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,73 @@
22
#
33
# Bash completion support for pgBackRest (https://pgbackrest.org/)
44

5-
_pgbackrest_commands() {
5+
__pgbackrest_commands() {
66
local commands=$(${script} | awk '/^[[:space:]]+/ {print $1}' | grep -v ${script});
77
echo ${commands}
88
}
99

10-
_pgbackrest_command_options() {
10+
__pgbackrest_command_options() {
1111
local command_options=$(${script} help ${COMP_WORDS[1]} | awk '/^([[:space:]]+)--/ {print $1}')
1212
echo ${command_options}
1313
}
1414

15-
_pgbackrest_command_options_names() {
15+
__pgbackrest_command_options_names() {
1616
local command_options_names=$(${script} help ${COMP_WORDS[2]} | awk '/^([[:space:]]+)--/ {gsub("--",""); print $1}')
1717
echo ${command_options_names}
1818
}
1919

20-
_pgbackrest_command_options_values() {
20+
__pgbackrest_command_options_values() {
2121
local command_options_values=$(${script} help ${COMP_WORDS[1]} ${prev#--} | awk '/^\*[[:space:]]/ {print $2}')
2222
echo ${command_options_values}
2323
}
2424

25-
_pgbackrest_stanza_values() {
26-
# If no stanza - return empty string; nothing to complete
27-
# May be some delays in getting stanza names
25+
# The '--output' option is available for 2 commands ('repo-ls' and 'info') with the same values.
26+
# For 'repo-ls' command displayed additional information in the same format.
27+
# To simplify the solution, the option values are specified directly.
28+
# If the values for different commands will be different, this code must be reviewed.
29+
__pgbackrest_command_options_values_output() {
30+
echo "text"$'\n'"json"
31+
}
32+
33+
# If no stanza - return empty string; nothing to complete
34+
# May be some delays in getting stanza names
35+
__pgbackrest_stanza_values() {
2836
local stanza_values=$(${script} info --output text | awk '/^stanza:/ {print $2}')
2937
echo ${stanza_values}
3038
}
3139

40+
# List repo content
41+
__pgbackrest_repo_content() {
42+
local repo_content content position substr_path tail_value cur_line_value
43+
# Regex: the ${cur}'s tail ends with '/'.
44+
local folder_regex="^([[:graph:]])+\/$"
45+
# Regex: get full path to last '/'.
46+
local path_regex="^(([[:graph:]])+\/)+([[:graph:]])+$"
47+
# By default, do not substitute the full path.
48+
local substr_path="false"
49+
# Check that ${cur} already contains a directory.
50+
# If true - need to add the last directory full path.
51+
# Valid example:
52+
# archive/
53+
# archive/dem
54+
# archive/demo/arch
55+
[[ ${cur} =~ ${folder_regex} || ${cur%\/*} =~ ${path_regex} ]] && cur_value=${cur%/*} && substr_path="true"
56+
# Get repo content by using 'repo-ls' in json format.
57+
# For 'repo-get', the content is also obtained via 'repo-ls'.
58+
# The logic for type 'link' is equivalent to type 'path'.
59+
content=$(${script} repo-ls --output json ${cur_value} | grep -o '"[^"]*":{"type":"[^"]*"' |awk '{gsub("\"|{|}",""); print}' | grep -v -E "\.:type:(path|link)")
60+
for line in ${content}; do
61+
# By default, don't contain '/' at the end.
62+
tail_value=""
63+
# By default, don't contain full path.
64+
line_value="${line}"
65+
[[ ${substr_path} == "true" ]] && line_value="${cur%/*}/${line}"
66+
[[ "$(echo ${line} | awk -F':' '{print $3}')" =~ ^("path"|"link")$ ]] && tail_value="/"
67+
repo_content+="$(echo ${line_value} | awk -F':' '{print $1}')${tail_value}"$'\n'
68+
done
69+
echo ${repo_content}
70+
}
71+
3272
_pgbackrest() {
3373
local script cur prev arg_regex
3474
COMPREPLY=()
@@ -40,64 +80,85 @@ _pgbackrest() {
4080

4181
case $COMP_CWORD in
4282
1)
43-
COMPREPLY=($(compgen -W "$(_pgbackrest_commands)" -- ${cur}))
83+
COMPREPLY=($(compgen -W "$(__pgbackrest_commands)" -- ${cur}))
4484
return 0;;
45-
2)
46-
case ${COMP_WORDS[1]} in
47-
help)
48-
COMPREPLY=($(compgen -W "$(_pgbackrest_commands)" -- ${cur}))
85+
2)
86+
case ${cur} in
87+
-*)
88+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options)" -- ${cur}))
4989
return 0;;
5090
*)
51-
case ${cur} in
52-
-*)
53-
COMPREPLY=($(compgen -W "$(_pgbackrest_command_options)" -- ${cur}))
91+
case ${COMP_WORDS[1]} in
92+
help)
93+
COMPREPLY=($(compgen -W "$(__pgbackrest_commands)" -- ${cur}))
94+
return 0;;
95+
repo-ls | repo-get)
96+
COMPREPLY=($(compgen -W "$(__pgbackrest_repo_content)" -- ${cur}))
97+
compopt -o nospace
5498
return 0;;
5599
*)
56-
return 1;;
100+
return 1;;
57101
esac;;
58102
esac;;
59103
3)
60-
case ${COMP_WORDS[1]} in
61-
help)
62-
COMPREPLY=($(compgen -W "$(_pgbackrest_command_options_names)" -- ${cur}))
104+
case ${cur} in
105+
-*)
106+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options)" -- ${cur}))
63107
return 0;;
64108
*)
65-
case ${cur} in
66-
-*)
67-
COMPREPLY=($(compgen -W "$(_pgbackrest_command_options)" -- ${cur}))
109+
case ${prev} in
110+
--stanza)
111+
COMPREPLY=($(compgen -W "$(__pgbackrest_stanza_values)" -- ${cur}))
112+
return 0;;
113+
--output)
114+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values_output)" -- ${cur}))
68115
return 0;;
69116
*)
70-
case ${prev} in
71-
--stanza)
72-
COMPREPLY=($(compgen -W "$(_pgbackrest_stanza_values)" -- ${cur}))
73-
return 0;;
74-
*)
75-
if [[ ${prev} =~ ${arg_regex} ]]; then
76-
COMPREPLY=($(compgen -W "$(_pgbackrest_command_options_values)" -- ${cur}))
77-
return 0
78-
else
79-
return 1
80-
fi;;
81-
esac;;
117+
if [[ ${prev} =~ ${arg_regex} ]]; then
118+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values)" -- ${cur}))
119+
return 0
120+
else
121+
case ${COMP_WORDS[1]} in
122+
help)
123+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_names)" -- ${cur}))
124+
return 0;;
125+
repo-ls | repo-get)
126+
COMPREPLY=($(compgen -W "$(__pgbackrest_repo_content)" -- ${cur}))
127+
compopt -o nospace
128+
return 0;;
129+
*)
130+
return 1;;
131+
esac
132+
fi;;
82133
esac;;
83134
esac;;
84135
*)
85136
# Completing the fourth, etc args.
86137
case ${cur} in
87138
-*)
88-
COMPREPLY=($(compgen -W "$(_pgbackrest_command_options)" -- ${cur}))
139+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options)" -- ${cur}))
89140
return 0;;
90141
*)
91142
case ${prev} in
92143
--stanza)
93-
COMPREPLY=($(compgen -W "$(_pgbackrest_stanza_values)" -- ${cur}))
144+
COMPREPLY=($(compgen -W "$(__pgbackrest_stanza_values)" -- ${cur}))
145+
return 0;;
146+
--output)
147+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values_output)" -- ${cur}))
94148
return 0;;
95149
*)
96150
if [[ ${prev} =~ ${arg_regex} ]]; then
97-
COMPREPLY=($(compgen -W "$(_pgbackrest_command_options_values)" -- ${cur}))
151+
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options_values)" -- ${cur}))
98152
return 0
99153
else
100-
return 1
154+
case ${COMP_WORDS[1]} in
155+
repo-ls | repo-get)
156+
COMPREPLY=($(compgen -W "$(__pgbackrest_repo_content)" -- ${cur}))
157+
compopt -o nospace
158+
return 0;;
159+
*)
160+
return 1;;
161+
esac
101162
fi;;
102163
esac;;
103164
esac;;

0 commit comments

Comments
 (0)