2
2
#
3
3
# Bash completion support for pgBackRest (https://pgbackrest.org/)
4
4
5
- _pgbackrest_commands () {
5
+ __pgbackrest_commands () {
6
6
local commands=$( ${script} | awk ' /^[[:space:]]+/ {print $1}' | grep -v ${script} ) ;
7
7
echo ${commands}
8
8
}
9
9
10
- _pgbackrest_command_options () {
10
+ __pgbackrest_command_options () {
11
11
local command_options=$( ${script} help ${COMP_WORDS[1]} | awk ' /^([[:space:]]+)--/ {print $1}' )
12
12
echo ${command_options}
13
13
}
14
14
15
- _pgbackrest_command_options_names () {
15
+ __pgbackrest_command_options_names () {
16
16
local command_options_names=$( ${script} help ${COMP_WORDS[2]} | awk ' /^([[:space:]]+)--/ {gsub("--",""); print $1}' )
17
17
echo ${command_options_names}
18
18
}
19
19
20
- _pgbackrest_command_options_values () {
20
+ __pgbackrest_command_options_values () {
21
21
local command_options_values=$( ${script} help ${COMP_WORDS[1]} ${prev# --} | awk ' /^\*[[:space:]]/ {print $2}' )
22
22
echo ${command_options_values}
23
23
}
24
24
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 () {
28
36
local stanza_values=$( ${script} info --output text | awk ' /^stanza:/ {print $2}' )
29
37
echo ${stanza_values}
30
38
}
31
39
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
+
32
72
_pgbackrest () {
33
73
local script cur prev arg_regex
34
74
COMPREPLY=()
@@ -40,64 +80,85 @@ _pgbackrest() {
40
80
41
81
case $COMP_CWORD in
42
82
1)
43
- COMPREPLY=($( compgen -W " $( _pgbackrest_commands ) " -- ${cur} ) )
83
+ COMPREPLY=($( compgen -W " $( __pgbackrest_commands ) " -- ${cur} ) )
44
84
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} ) )
49
89
return 0;;
50
90
* )
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
54
98
return 0;;
55
99
* )
56
- return 1;;
100
+ return 1;;
57
101
esac ;;
58
102
esac ;;
59
103
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} ) )
63
107
return 0;;
64
108
* )
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} ) )
68
115
return 0;;
69
116
* )
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 ;;
82
133
esac ;;
83
134
esac ;;
84
135
* )
85
136
# Completing the fourth, etc args.
86
137
case ${cur} in
87
138
-* )
88
- COMPREPLY=($( compgen -W " $( _pgbackrest_command_options ) " -- ${cur} ) )
139
+ COMPREPLY=($( compgen -W " $( __pgbackrest_command_options ) " -- ${cur} ) )
89
140
return 0;;
90
141
* )
91
142
case ${prev} in
92
143
--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} ) )
94
148
return 0;;
95
149
* )
96
150
if [[ ${prev} =~ ${arg_regex} ]]; then
97
- COMPREPLY=($( compgen -W " $( _pgbackrest_command_options_values ) " -- ${cur} ) )
151
+ COMPREPLY=($( compgen -W " $( __pgbackrest_command_options_values ) " -- ${cur} ) )
98
152
return 0
99
153
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
101
162
fi ;;
102
163
esac ;;
103
164
esac ;;
0 commit comments