Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash completion #35451

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1d5e938
Auto TAB completion for bash using alias for occ.
RonaldBarnes Nov 27, 2022
18f445d
Updated copyright per Contribution guidelines.
RonaldBarnes Nov 27, 2022
102052e
Resolved requested changes to PR.
RonaldBarnes Nov 28, 2022
8e87648
Resolved one more comment deletion, per PR change request(s).
RonaldBarnes Nov 28, 2022
56dfa43
Changed getHttpdUser to searchHttpdUser through-out, per PR change re…
RonaldBarnes Nov 28, 2022
8de4ac1
Revised per pull request comment https://github.com/nextcloud/server/…
RonaldBarnes Dec 1, 2022
8e99a60
Reset COMP_WORDBREAKS to original value when finished.
RonaldBarnes Dec 1, 2022
2d091e2
Add quotes around COMP_WORDBREAKS, else unexpected behaviour.
RonaldBarnes Dec 1, 2022
2aa4940
Fixed adding '-vv' option to array: remnant overlooked in commit a86a…
RonaldBarnes Dec 2, 2022
2751eaa
Added more web server user names.
RonaldBarnes Dec 2, 2022
8ea5a98
Add user_id to completions.
RonaldBarnes Dec 5, 2022
7825a8f
Web server lighthttp user name is fixed to lighttpd.
RonaldBarnes Dec 5, 2022
aed5edd
Added Usage: and remove unused debugging.
RonaldBarnes Dec 5, 2022
c3a8470
Added user_id, app, lang, and file system completions.
RonaldBarnes Dec 5, 2022
d9ae83d
Changed all functions to have '_occ_' prefix.
RonaldBarnes Dec 5, 2022
3955590
Specify type of entity being 'unset'.
RonaldBarnes Dec 6, 2022
e8e8e78
Changed ownership test to file config/config.php, not ./occ
RonaldBarnes Dec 6, 2022
7eaeb9d
Added colour to filename when ownership mis-match.
RonaldBarnes Dec 6, 2022
0d650f7
If no ~/.bash_aliases, then offer to create /etc/profile.d/occ instead.
RonaldBarnes Dec 6, 2022
6ddaf0f
Offer to add alias to /etc/bash.bashrc as well as ~/.bash_aliases
RonaldBarnes Dec 6, 2022
efa1dc3
Option to add completion script to ~/.local/share/bash-completion/com…
RonaldBarnes Dec 7, 2022
3fa70e7
If no ~/.bash_aliases, offer to append to ~/.bashrc instead of /etc/b…
RonaldBarnes Dec 7, 2022
b23f709
Updated documentation / usage.
RonaldBarnes Dec 7, 2022
b9a4081
Added more web server user names, per https://docs.nextcloud.com/serv…
RonaldBarnes Dec 7, 2022
161c000
Added @author, reformatted @copyright.
RonaldBarnes Dec 7, 2022
f720c72
Refactored: removed all global persistence options, stores alias to ~…
RonaldBarnes Dec 10, 2022
f6bbefa
Changed completion script destination directory to /etc/bash_completi…
RonaldBarnes Dec 10, 2022
a3c98c0
Renamed complete.occ to occ.bash
RonaldBarnes Dec 10, 2022
23fb524
Typo in usage section.
RonaldBarnes Dec 11, 2022
c78dc3c
Restored chown and chmod on /etc/bash_completion.d/occ.bash
RonaldBarnes Dec 11, 2022
0faf097
NextCloud to Nextcloud, camel case to snake case: phpFound to php_found
RonaldBarnes Dec 11, 2022
7eb851a
Switched back to ~/.local/share/bash-completion/completions/ as the t…
RonaldBarnes Dec 13, 2022
191e1d9
Cleaned up TRAPs; works better with 'read' aka 'readline'.
RonaldBarnes Dec 13, 2022
4b302d1
Added support for 'occ config:...': handles 'app' and 'system', 'get'…
RonaldBarnes Dec 13, 2022
653ded6
Option to regenerate alias if existing one found (helpful if multiple…
RonaldBarnes Dec 25, 2022
cfb1720
Consistent use of double vs single quotes.
RonaldBarnes Dec 27, 2022
525f737
Changed target for occ.bash to user's ~/.local/share/bash-completion/…
RonaldBarnes Jan 4, 2023
f53b3d6
Allow occ.bash to operate from multiple aliases for admins with multi…
RonaldBarnes Jan 19, 2023
8be0fbc
Check for uid as well as user_id (dav:list-calendars: uid; files-scan…
RonaldBarnes Jan 19, 2023
0074f5b
Removed line of debug code.
RonaldBarnes Jan 19, 2023
a8248da
Changed mode to non-executable: must be sourced for alias to persist …
RonaldBarnes Jan 29, 2023
295dfc7
Renamed script to more descriptive name and updated internal usage.
RonaldBarnes Jan 29, 2023
06761f4
Handle 'dav:move-calendar name sourceuid destinationuid'.
RonaldBarnes Feb 3, 2023
0f4dcb1
Added two script files to build/files-checker.php
RonaldBarnes Feb 14, 2023
5b6b3c3
Changed references to 'bash-add-alias.sh' to 'bash-tab-completion-occ…
RonaldBarnes Feb 14, 2023
a9eba83
Indentation was off; spaces not tabs.
RonaldBarnes Feb 15, 2023
aa0962d
Updated documentation for multi-host, multi-alias environments.
RonaldBarnes Mar 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 310 additions & 0 deletions bash-tab-completion-occ.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,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."
2 changes: 2 additions & 0 deletions build/files-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
'autotest-js.sh',
'autotest.sh',
'babel.config.js',
'bash-tab-completion-occ.sh',
'build',
'composer.json',
'composer.lock',
Expand All @@ -76,6 +77,7 @@
'jest.config.js',
'lib',
'occ',
'occ.bash',
'ocm-provider',
'ocs',
'ocs-provider',
Expand Down
Loading