Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Auto TAB completion for bash using alias for occ.
Signed-off-by: Ronald Barnes <[email protected]>
  • Loading branch information
RonaldBarnes authored and nickvergessen committed Mar 1, 2023
commit 1d5e9386bd6453098b048a4a8610cb126ae23d74
330 changes: 330 additions & 0 deletions bash-add-alias.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
#!/bin/bash

## ###########################################################################
## Creates an alias to run `occ` as the appropriate web server user
##
## Optionally adds the alias to user's .bash_aliases file
## Optionally adds the alias to SUDO_USER's .bash_aliases file
## Verifies `occ` is owned by web server user
## Optionally copies bash completion script `complete.occ` to
## /etc/bash_completion.d/
##
## Ronald Barnes (c) 2022
## ###########################################################################


## Dubugging:
## set -x
## Errors exit immediately:
## set -e
## Catch undefined vars:
set -u


## Define colours:
function 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 value
unset httpdUser
unset user_name
unset home_dir
unset aliasExists
unset phpFound
unset answer
unset aliasString
unset addAlias
unset aliasExists

unset getHttpdUser_old
unset getHttpdUser
unset occOwner
unset occPath
unset getOccPath
unset script_found

unset green
unset yellow
unset red
unset default_colour

## Reset all trap signals:
trap - SIGINT
trap - SIGHUP
trap - SIGTERM
trap - SIGKILL
trap - EXIT
trap - QUIT
## If param was passed, i.e. "ALL", cleanup EVERYTHING, we're done:
if [[ ${#@} -ge 1 ]]; then
trap - RETURN
unset cleanup_vars
## Reset unbound var checking, else i.e. bash completion breaks, etc.
set +u
unset define_colours
fi
## Reset unbound var checking, else i.e. bash completion breaks, etc.
# set +u
## This hangs around if run via ". $this_script" and needs to be
## manually cleared.
## Putting *entire* script (after func defs) inside subshell fixed it:
# trap - RETURN
# trap -p
}

function getHttpdUser()
{
## Fetch possible httpd users (for sudo -u ...) into array:
##
## Params: regex / string of name(s) to search for
while read value; do
## Associative array:
## httpdUser[$value]=$value
## Normal, indexed array:
httpdUser+=($value)
done <<< $(grep \
--extended-regex \
--ignore-case \
--only-matching \
--max-count=1 \
"${1}" /etc/passwd \
)
}




function getOccPath()
{
read -ep "Path to file 'occ': " -i "/" occPath
if [[ ! -f ${occPath} ]] ; then
getOccPath
fi
## echo "occPath: \"${occPath}\""
}


function bash_aliases()
{
if [[ $# -eq 0 ]] ; then
## Expecting path to .bash_aliases directory
return 99
else
home_dir=${1}
fi

grep --no-messages "occ" ${home_dir}/.bash_aliases
aliasExists=$?
if [[ aliasExists -eq 0 ]]; then
echo "There is an \"occ\" alias in ${home_dir}.bash_aliases:"
grep "occ" ${home_dir}/.bash_aliases
elif [[ -w ${home_dir}/.bash_aliases ]]; then
echo -en "Add alias to ${yellow}${home_dir}/.bash_aliases${default_colour}?"
read -s -p " (y/N) " -n 1 answer
if [[ ${answer} =~ ^Y|y ]] ; then
echo "Y"
echo "${aliasString}" >> ${home_dir}/.bash_aliases
answer=$?
if [[ ${answer} -eq 0 ]] ; then
echo -ne "${green}Success${default_colour}: "
grep occ ${home_dir}/.bash_aliases
fi
else
## if [[ ${answer} != "" ]] ; then
echo "N"
fi
else
echo -ne "${yellow}NOTICE${default_colour}: Cannot access "
echo -e "${home_dir}/.bash_aliases"
fi
}




## Run EVERYTHING in a subshell so "trap ... RETURN" doesn't linger:
## (
## Cleanup all variables on exit:
## trap 'cleanup_vars' SIGINT SIGKILL SIGTERM
## Cleanup ALL variables on exit (including cleanup_vars() itself):
trap 'cleanup_vars ALL' RETURN EXIT QUIT SIGINT SIGKILL SIGTERM


## Handy red / yellow / green / default colour defs:
define_colours

## Store web server user name(s) from /etc/passwd as indexed array:
## declare -A httpdUser
declare -a httpdUser

## Find the web server user name:
getHttpdUser "httpd|www-data"
if [ ${#httpdUser[0]} -eq 0 ] ; then
## No standard httpd user found, try "nobody":
getHttpdUser "nobody"
fi


if [ ${#httpdUser[0]} -eq 0 ] ; then
echo -e "${red}ERROR${default_colour}: No web server user found."
## kill -s SIGINT $$
return 1
else
echo -ne "Web server user name: "
echo -e "\"${green}${httpdUser[0]}${default_colour}\"."
fi


## Looks for existing occ alias:
occPath=""
alias occ 2>/dev/null
aliasExists=$?
## USER=root, HOME=/root, SUDO_USER=me:
## user_name=${SUDO_USER:-$USER}
user_name=${USER:-$SUDO_USER}
if [ ${aliasExists} -eq 0 ] ; then
echo "Alias for occ found for user \"${user_name}\"."
aliasString=$(alias occ)
occPath=${aliasString##* }
else
echo "Alias for occ command not found for user \"${user_name}\"."
which php 2>&1 > /dev/null
phpFound=$?
if [ $phpFound -ne 0 ]; then
echo -e "${red}ERROR${default_colour}: php not found in path."
kill -s SIGKILL $$
fi
occPath="$(pwd)/occ"
if [[ -f ${occPath} ]] ; then
occPath=$(pwd)/occ
else
echo "Can't find \"occ\", not in current directory."
getOccPath
fi
occOwner=$(stat --format="%U" ${occPath})
if [[ ${occOwner} != ${httpdUser[0]} ]] ; then
echo -e "${red}ERROR${default_colour}: Owner of occ is not web server user:"
echo " ${occOwner} != ${httpdUser}"
## kill -s SIGKILL $$
kill -s SIGINT $$
trap - RETURN
return 99
fi

aliasString="occ='sudo --user ${httpdUser} php ${occPath}'"
echo -ne "Run \"${yellow}alias ${green}${aliasString}${default_colour}\""
read -s -p " (y/N)? " -n 1 answer
if [[ ${answer} =~ ^[Yy] ]] ; then
echo "Y"
eval alias "${aliasString}"
alias occ
## elif [[ ${answer} != "" ]] ; then
else
echo "N"
fi
fi

## Is there an occ alias in ~/.bash_aliases?
bash_aliases $HOME
home_dir=""
if [[ "${SUDO_USER}" != "" ]] ; then
## Find user-who-ran-sudo's home directory:
home_dir=$(grep ${SUDO_USER} /etc/passwd)
## Strip off colon->end-of-line
home_dir=${home_dir%:*}
## Strip off start-of-line->last colon:
home_dir=${home_dir##*:}
if [[ "$HOME" != "${home_dir}" ]] ; then
bash_aliases ${home_dir}
fi
fi


## Run complete.occ to handle bash auto completion?
script_found=1 ## aka False
## Strip "occ" from occPath:
occPath=${occPath%/*}
if [[ -f ${occPath}/complete.occ ]] ; then
script_found=0
echo -en "Run bash completion script "
echo -en "${green}complete.occ${default_colour}? "
read -sp " (Y/n) " -N 1 answer
if [[ ${answer} =~ ^[Nn] ]] ; then
echo "N"
else
echo "Y"
echo -n "Running ${occPath}/complete.occ ... "
## Do not run cleanup_vars() when complete.occ returns:
trap - RETURN
source ${occPath}/complete.occ
## Reset trap:
trap 'cleanup_vars ALL' RETURN
status=$?
if [[ ${status} -eq 0 ]] ; then
echo -e "${green}success${default_colour}."
else
echo -e "${red}Error${default_colour}."
fi
fi
fi


## Does `complete.occ` exist in /etc/bash_completion.d/?
## Does `complete.occ` exist in /usr/share/bash-completion/completions/?
## If neither, add it to /etc/bash_completion.d/?
if [[ -d /etc/bash_completion.d ]] ; then
if [[ -f /etc/bash_completion.d/complete.occ ]] ; then
script_found=0
echo "Found existing complete.occ in /etc/bash_completion.d/"
else
if [[ -f ${occPath}/complete.occ ]] ; then
echo -en "Add ${yellow}complete.occ${default_colour} "
echo -en "to /etc/bash_completion.d?"
read -sp " (y/N) " -n 1 answer
if [[ ${answer} =~ ^[Yy] ]] ; then
echo "Y"
# (
cp -v complete.occ /etc/bash_completion.d
if [[ $? -ne 0 ]] ; then
echo -ne "${red}ERROR${default_colour}: Could not "
echo -e "copy complete.occ to /etc/bash_completion.d/"
else
## Copy successful, set owner and permissions:
chown -v root:root /etc/bash_completion.d/complete.occ
chmod -v 0644 /etc/bash_completion.d/complete.occ
fi
# )
else
echo "N"
fi
fi
# elif [[ -d /usr/share/bash-completion ]] ; then
# if [[ -f /usr/share/bash-completion/completions/complete.occ ]] ; then
# script_found=0
# echo -n "Found existing complete.occ in "
# echo "/usr/share//bash-completion/completions"
# fi
# fi
fi
fi



## Cannot remove trap on RETURN inside a return trap catch, so do it here:
trap - RETURN
## Now clean all vars and remove all traps
cleanup_vars ALL
trap -p RETURN
echo "DONE."
## echo "Run \"trap -p\" to see if there's an existing trap on return"
## echo "If so, run \"trap - RETURN\" to clear it."
## ) ## end sub-shell
Loading