-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Bash completion #35451
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
Open
RonaldBarnes
wants to merge
47
commits into
nextcloud:master
Choose a base branch
from
RonaldBarnes:bash-completion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Bash completion #35451
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 18f445d
Updated copyright per Contribution guidelines.
RonaldBarnes 102052e
Resolved requested changes to PR.
RonaldBarnes 8e87648
Resolved one more comment deletion, per PR change request(s).
RonaldBarnes 56dfa43
Changed getHttpdUser to searchHttpdUser through-out, per PR change re…
RonaldBarnes 8de4ac1
Revised per pull request comment https://github.com/nextcloud/server/…
RonaldBarnes 8e99a60
Reset COMP_WORDBREAKS to original value when finished.
RonaldBarnes 2d091e2
Add quotes around COMP_WORDBREAKS, else unexpected behaviour.
RonaldBarnes 2aa4940
Fixed adding '-vv' option to array: remnant overlooked in commit a86a…
RonaldBarnes 2751eaa
Added more web server user names.
RonaldBarnes 8ea5a98
Add user_id to completions.
RonaldBarnes 7825a8f
Web server lighthttp user name is fixed to lighttpd.
RonaldBarnes aed5edd
Added Usage: and remove unused debugging.
RonaldBarnes c3a8470
Added user_id, app, lang, and file system completions.
RonaldBarnes d9ae83d
Changed all functions to have '_occ_' prefix.
RonaldBarnes 3955590
Specify type of entity being 'unset'.
RonaldBarnes e8e8e78
Changed ownership test to file config/config.php, not ./occ
RonaldBarnes 7eaeb9d
Added colour to filename when ownership mis-match.
RonaldBarnes 0d650f7
If no ~/.bash_aliases, then offer to create /etc/profile.d/occ instead.
RonaldBarnes 6ddaf0f
Offer to add alias to /etc/bash.bashrc as well as ~/.bash_aliases
RonaldBarnes efa1dc3
Option to add completion script to ~/.local/share/bash-completion/com…
RonaldBarnes 3fa70e7
If no ~/.bash_aliases, offer to append to ~/.bashrc instead of /etc/b…
RonaldBarnes b23f709
Updated documentation / usage.
RonaldBarnes b9a4081
Added more web server user names, per https://docs.nextcloud.com/serv…
RonaldBarnes 161c000
Added @author, reformatted @copyright.
RonaldBarnes f720c72
Refactored: removed all global persistence options, stores alias to ~…
RonaldBarnes f6bbefa
Changed completion script destination directory to /etc/bash_completi…
RonaldBarnes a3c98c0
Renamed complete.occ to occ.bash
RonaldBarnes 23fb524
Typo in usage section.
RonaldBarnes c78dc3c
Restored chown and chmod on /etc/bash_completion.d/occ.bash
RonaldBarnes 0faf097
NextCloud to Nextcloud, camel case to snake case: phpFound to php_found
RonaldBarnes 7eb851a
Switched back to ~/.local/share/bash-completion/completions/ as the t…
RonaldBarnes 191e1d9
Cleaned up TRAPs; works better with 'read' aka 'readline'.
RonaldBarnes 4b302d1
Added support for 'occ config:...': handles 'app' and 'system', 'get'…
RonaldBarnes 653ded6
Option to regenerate alias if existing one found (helpful if multiple…
RonaldBarnes cfb1720
Consistent use of double vs single quotes.
RonaldBarnes 525f737
Changed target for occ.bash to user's ~/.local/share/bash-completion/…
RonaldBarnes f53b3d6
Allow occ.bash to operate from multiple aliases for admins with multi…
RonaldBarnes 8be0fbc
Check for uid as well as user_id (dav:list-calendars: uid; files-scan…
RonaldBarnes 0074f5b
Removed line of debug code.
RonaldBarnes a8248da
Changed mode to non-executable: must be sourced for alias to persist …
RonaldBarnes 295dfc7
Renamed script to more descriptive name and updated internal usage.
RonaldBarnes 06761f4
Handle 'dav:move-calendar name sourceuid destinationuid'.
RonaldBarnes 0f4dcb1
Added two script files to build/files-checker.php
RonaldBarnes 5b6b3c3
Changed references to 'bash-add-alias.sh' to 'bash-tab-completion-occ…
RonaldBarnes a9eba83
Indentation was off; spaces not tabs.
RonaldBarnes aa0962d
Updated documentation for multi-host, multi-alias environments.
RonaldBarnes File filter
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
commit 1d5e9386bd6453098b048a4a8610cb126ae23d74
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}\"" | ||
RonaldBarnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
|
|
||
| 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 | ||
RonaldBarnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
artonge marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| ## 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 | ||
RonaldBarnes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.