Skip to content

Commit 1998540

Browse files
authored
Merge pull request #8 from woblerr/use_backrest_custom_config
Add completion of parameters depending on the specified --config* options.
2 parents 617c0b5 + 80bad3b commit 1998540

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020-2022 woblerr
3+
Copyright (c) 2020-2023 woblerr
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

pgbackrest-completion.sh

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ __pgbackrest_command_options_values_type_info() {
7070
# If no stanza - return empty string; nothing to complete.
7171
# May be some delays in getting stanza names.
7272
__pgbackrest_stanza_values() {
73-
local stanza_values=$(${script} info --output text 2>/dev/null | awk '/^stanza:/ {print $2}')
73+
# Basic command for 'info' command.
74+
local info_command="${script} info --output text"
75+
[[ ${config_params} != '' ]] && info_command="${info_command} ${config_params}"
76+
local stanza_values=$(${info_command} 2>/dev/null | awk '/^stanza:/ {print $2}')
7477
echo ${stanza_values}
7578
}
7679

@@ -90,15 +93,16 @@ __pgbackrest_repo_content() {
9093
# archive/dem
9194
# archive/demo/arch
9295
[[ ${cur} =~ ${folder_regex} || ${cur} =~ ${path_regex} ]] && cur_value=${cur%/*} && substr_path="true"
96+
# Basic command for 'repo-ls' command.
97+
local repo_ls_command="${script} repo-ls --output json"
98+
# Add config params, if they exists.
99+
[[ ${config_params} != '' ]] && repo_ls_command="${repo_ls_command} ${config_params}"
100+
# For compatibility with versions < v2.33.
101+
[[ ${repo_params} != '' ]] && repo_ls_command="${repo_ls_command} ${repo_params}"
93102
# Get repo content by using 'repo-ls' in json format.
94103
# For 'repo-get', the content is also obtained via 'repo-ls'.
95104
# The logic for type 'link' is equivalent to type 'path'.
96-
if [[ ${repo_key} == '' ]]; then
97-
# For compatibility with versions < v2.33.
98-
raw_content=$(${script} repo-ls --output json ${cur_value} 2>/dev/null)
99-
else
100-
raw_content=$(${script} repo-ls --repo ${repo_key} --output json ${cur_value} 2>/dev/null)
101-
fi
105+
raw_content=$(${repo_ls_command} ${cur_value} 2>/dev/null)
102106
# When incorrect value for '--repo' is used (e.g. '--repo 300'),
103107
# the command above returns an error, which is discarded, and an empty result.
104108
# The completion will not show anything.
@@ -123,10 +127,12 @@ _pgbackrest() {
123127
script=${COMP_WORDS[0]}
124128
# Repo id allowed values: 1-256.
125129
# https://pgbackrest.org/command.html#command-repo-ls
126-
# Defaul value ''.
127-
local repo_key=''
130+
# If --repo parameter is not set, use the default parameter for pgBackRest or from env.
131+
local repo_params=''
128132
# Regex for check previous argument.
129133
arg_regex="^--([[:alnum:][:punct:]])+$"
134+
# If --config* parameters are not set, use the default parameters for pgBackRest or from env.
135+
local config_params=''
130136
case $COMP_CWORD in
131137
1)
132138
COMPREPLY=($(compgen -W "$(__pgbackrest_commands)" -- ${cur}))
@@ -216,6 +222,25 @@ _pgbackrest() {
216222
COMPREPLY=($(compgen -W "$(__pgbackrest_command_options)" -- ${cur}))
217223
return 0;;
218224
*)
225+
local i
226+
# 0 - script name (pgbackrest), 1 - command (info).
227+
# There is no need to check them.
228+
# Example:
229+
# pgbackrest info --config /tmp/pgbackrest.conf --stanza <TAB>
230+
for (( i=2; i<${#COMP_WORDS[@]}-1; i++)); do
231+
case ${COMP_WORDS[$i]} in
232+
# Checking whether --config* parameters are set.
233+
--config)
234+
config_params="${config_params} --config ${COMP_WORDS[$i+1]}";;
235+
--config-include-path)
236+
config_params="${config_params} --config-include-path ${COMP_WORDS[$i+1]}";;
237+
--config-path)
238+
config_params="${config_params} --config-path ${COMP_WORDS[$i+1]}";;
239+
# Checking whether --repo parameter is set.
240+
--repo)
241+
repo_params="${repo_params} --repo ${COMP_WORDS[$i+1]}";;
242+
esac
243+
done
219244
case ${prev} in
220245
--stanza)
221246
COMPREPLY=($(compgen -W "$(__pgbackrest_stanza_values)" -- ${cur}))
@@ -250,8 +275,6 @@ _pgbackrest() {
250275
else
251276
case ${COMP_WORDS[1]} in
252277
repo-ls | repo-get)
253-
# Check construction like '--repo 2'.
254-
[[ ${COMP_WORDS[COMP_CWORD - 2]} == "--repo" ]] && repo_key=${prev}
255278
COMPREPLY=($(compgen -W "$(__pgbackrest_repo_content)" -- ${cur}))
256279
compopt -o nospace
257280
return 0;;

0 commit comments

Comments
 (0)