-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathbash-tab-completion-occ.sh
More file actions
310 lines (268 loc) · 8.11 KB
/
Copy pathbash-tab-completion-occ.sh
File metadata and controls
310 lines (268 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
## ###########################################################################
## Creates an alias to run `occ` as the appropriate user:
## the owner of nextcloud/config/config.php
## or
## a common web server user name found in /etc/passwd
##
## Optionally adds the alias to user's .bash_aliases file, if found
## If not found, optionally add alias to ~/.bashrc
##
## Optionally copies bash completion script `occ.bash` to
## ~/.local/share/bash-completion/completions/
##
## @author Ronald Barnes
## @copyright Copyright 2022, Ronald Barnes ron@ronaldbarnes.ca
## ###########################################################################
##
## Usage:
## . bash-tab-completion-occ.sh
## or:
## source bash-tab-completion-occ.sh
## ###########################################################################
## Save original "catch undefined vars" setting:
_occ_orig_set_u=$-
if [[ $_occ_orig_set_u =~ u ]] ; then
_occ_orig_set_u=0
else
_occ_orig_set_u=1
fi
## Catch undefined vars:
set -u
## Define colours:
function _occ_define_colours()
{
green="\e[1;32m"
yellow="\e[1;33m"
red="\e[1;31m"
default_colour="\e[00m"
}
## Leave no trace after exit (except alias(es)):
function cleanup_vars()
{
unset -v value
unset -v user_name
unset -v home_dir
unset -v _occ_alias_exists
unset -v php_found
unset -v answer
unset -v _occ_alias_string
unset -v _occ_alias_exists
unset -v _occ_nc_path
unset -f _occ_get_nc_path
unset -v _occ_completion_script
unset -v script_found
unset -v _occ_script_installed
unset -v _occ_alias_installed
unset -v _occ_status
unset -v _occ_sudo_user
unset -f _occ_create_alias
unset -v green
unset -v yellow
unset -v red
unset -v default_colour
## Reset all trap signals:
trap - SIGINT
trap - SIGHUP
trap - SIGTERM
trap - SIGKILL
trap - EXIT
trap - QUIT
unset -f cleanup_vars
## Reset unbound var checking to original state:
if [[ ! _occ_orig_set_u -eq 0 ]] ; then
set +u
fi
unset -v _occ_orig_set_u
unset -f _occ_define_colours
unset -f _occ_bash_aliases
## End this program:
kill -s SIGINT $$
## End this program, second time if CTRL+C pressed in `read` / `readline`:
kill -s SIGINT $$
}
function _occ_get_nc_path()
{
read -ep "Path to Nextcloud directory? " -i "/" _occ_nc_path
if [[ ! -f ${_occ_nc_path}/occ ]] ; then
_occ_get_nc_path
fi
}
function _occ_bash_aliases()
{
if [[ $# -ne 2 ]] ; then
## Expecting path and file, i.e. ~ and .bash_aliases
return 99
else
local home_dir=${1}
local alias_file=${2}
fi
if [[ ! -w ${home_dir}/${alias_file} ]] ; then
return
fi
grep --quiet --no-messages "occ" ${home_dir}/${alias_file}
_occ_alias_exists=$?
if [[ _occ_alias_exists -eq 0 ]]; then
echo "There is an \"occ\" alias in ${home_dir}/${alias_file}:"
echo -n " --> "
grep --color=auto "occ" ${home_dir}/${alias_file}
_occ_alias_installed=0
elif [[ -w ${home_dir}/${alias_file} ]]; then
echo -en "Add alias to "
echo -en "${yellow}${home_dir}/${alias_file}${default_colour}?"
read -s -p " (y/N) " -n 1 answer
if [[ ${answer} =~ ^Y|y ]] ; then
echo "Y"
echo "" >> ${home_dir}/${alias_file}
echo "## tab completion for Nextcloud:" >> ${home_dir}/${alias_file}
echo "alias occ=${_occ_alias_string}" >> ${home_dir}/${alias_file}
answer=$?
if [[ ${answer} -eq 0 ]] ; then
echo -ne "${green}Success${default_colour}: "
grep --color=auto occ ${home_dir}/${alias_file}
_occ_alias_installed=0
fi
else
echo "N"
fi
fi
}
function _occ_create_alias()
{
## Note: `which` command not always installed, see if `php` exists this way:
php --version 1>/dev/null 2>/dev/null
php_found=$?
if [ $php_found -ne 0 ]; then
echo -e "${red}ERROR${default_colour}: php not found in path."
return 99
fi
_occ_alias_string="'sudo --user ${_occ_sudo_user} php ${_occ_nc_path}/occ'"
echo -ne "Run \"${yellow}alias occ="
echo -ne "${green}${_occ_alias_string}${default_colour}\""
read -s -p " (Y/n)? " -n 1 answer
if [[ ${answer} =~ ^[Nn] ]] ; then
echo "N"
else
echo "Y"
eval alias "occ=${_occ_alias_string}"
alias occ
fi
}
## Capture exit conditions to clean up all variables:
trap 'cleanup_vars ALL' EXIT QUIT SIGINT SIGKILL SIGTERM
## Handy red / yellow / green / default colour defs:
_occ_define_colours
user_name=$(whoami)
_occ_completion_script="occ.bash"
## Find Nextcloud installation directory
_occ_nc_path=$(pwd)
if [[ ! -f ${_occ_nc_path}/occ ]] ; then
echo -e "Can't find ${yellow}occ${default_colour} in current directory."
_occ_get_nc_path
## Strip trailing "/" from path:
_occ_nc_path=${_occ_nc_path%/}
fi
## Find owner of config/config.php, should be the web server user, per:
## https:/docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html#http-user-label
## but in shared hosting, might not be(?)
_occ_sudo_user=""
if [[ -f ${_occ_nc_path}/config/config.php ]] ; then
_occ_sudo_user=$(stat --format="%U" ${_occ_nc_path}/config/config.php)
else
echo -en "${red}WARNING${default_colour}: "
echo -en "Cannot locate ${yellow}${_occ_nc_path}/config/config.php"
echo -e "${default_colour}"
## return 100
read _occ_sudo_user <<< $(grep --only-matching --extended-regex \
"www-data|httpd|nginx|lighttpd|apache|http|wwwrun" \
/etc/passwd
)
fi
_occ_alias_string=""
## Looks for existing occ alias:
_occ_alias_string=$(alias occ 2>/dev/null)
_occ_alias_exists=$?
if [ ${_occ_alias_exists} -eq 0 ] ; then
echo "occ alias found for user \"${user_name}\":"
echo -e " --> ${green}$(alias occ)${default_colour}"
echo -en "Generate new alias? "
read -sp "(y/N) " -N 1 answer
if [[ ${answer} =~ ^[Yy] ]] ; then
echo "Y"
unalias occ
_occ_create_alias
else
echo "N"
fi
else
echo "No occ alias found for user \"${user_name}\"."
_occ_create_alias
fi
_occ_alias_installed=1
## Is there an occ alias in ~/.bash_aliases?
_occ_bash_aliases ${HOME} ".bash_aliases"
## If no alias installed into ~/bash_aliases file, try ~/.bashrc:
if [[ $_occ_alias_installed -ne 0 ]] ; then
_occ_bash_aliases ${HOME} ".bashrc"
fi
## Run ${_occ_completion_script} to handle bash auto completion?
script_found=1 ## aka False
if [[ -f ${_occ_nc_path}/${_occ_completion_script} ]] ; then
script_found=0
echo -en "Run bash completion script "
echo -en "${green}${_occ_completion_script}${default_colour}? "
read -sp " (Y/n) " -N 1 answer
if [[ ${answer} =~ ^[Nn] ]] ; then
echo "N"
else
echo "Y"
echo -n "Running ${_occ_nc_path}/${_occ_completion_script} ... "
source ${_occ_nc_path}/${_occ_completion_script}
_occ_status=$?
if [[ ${_occ_status} -eq 0 ]] ; then
echo -e "${green}success${default_colour}."
else
echo -e "${red}Error${default_colour}."
fi
fi
else
echo -en "${red}WARNING${default_colour}: "
echo -en "Cannot find ${yellow}${_occ_nc_path}/${_occ_completion_script}"
echo -e "${default_colour}"
fi
## Does ${_occ_completion_script} exist in...
## ~/.local/share/bash-completion/completions/?
_occ_script_installed=1
if [[ -f ${_occ_nc_path}/${_occ_completion_script} ]] ; then
if [[ -r ~/.local/share/bash-completion/completions/${_occ_completion_script} ]] ; then
echo -en "Found ${yellow}~/.local/share/bash-completion/completions/"
echo -e "${_occ_completion_script}${default_colour}."
else
echo -en "Copy ${yellow}${_occ_completion_script}${default_colour} to "
echo -en "${yellow}~/.local/share/bash-completion/completions/"
echo -en "${default_colour}?"
read -sp " (y/N) " -n 1 answer
if [[ ${answer} =~ ^[Yy] ]] ; then
echo "Y"
mkdir -vp ~/.local/share/bash-completion/completions/
## File name MUST have name of command / alias it operates on when
## it is in this location, i.e. occ, _occ, or occ.bash:
cp --verbose \
--archive \
--preserve=all \
--interactive \
${_occ_nc_path}/${_occ_completion_script} \
~/.local/share/bash-completion/completions/
## If copy worked, chown and chmod for safety:
if [[ $? -eq 0 ]] ; then
chown -v root:root ~/.local/share/bash-completion/completions/occ.bash
chmod 0644 ~/.local/share/bash-completion/completions/occ.bash
fi
else
echo "N"
fi
fi
fi
## Now clean all vars and remove all traps
cleanup_vars ALL
echo "DONE."