1
0
mirror of https://github.com/TheLocehiliosan/yadm synced 2026-03-02 03:49:29 +00:00

Add Bash completion script (#60)

* Add completion script
* Add introspection
This commit is contained in:
Tim Byrne
2017-04-06 07:30:28 -05:00
parent eabf9091fb
commit 356c47a19f
6 changed files with 285 additions and 16 deletions

19
completion/README.md Normal file
View File

@@ -0,0 +1,19 @@
# Prerequisites
**yadm** completion only works if Git completions are also enabled.
# Installation
## Homebrew
If using `homebrew` to install **yadm**, completions should automatically be handled if you also install `brew install bash-completion`. This might require you to include the main completion script in your own bashrc file like this:
```
[ -f /usr/local/etc/bash_completion ] && source /usr/local/etc/bash_completion
```
## Manual installation
Copy the completion script locally, and add this to you bashrc:
```
[ -f /full/path/to/yadm.bash_completion ] && source /full/path/to/yadm.bash_completion
```

View File

@@ -0,0 +1,85 @@
# test if git completion is missing, but loader exists, attempt to load
if ! declare -F _git > /dev/null && declare -F _completion_loader > /dev/null; then
_completion_loader git
fi
# only operate if git completion is present
if declare -F _git > /dev/null; then
_yadm() {
local current=${COMP_WORDS[COMP_CWORD]}
local penultimate=${COMP_WORDS[COMP_CWORD-1]}
local antepenultimate=${COMP_WORDS[COMP_CWORD-2]}
local GIT_DIR
# shellcheck disable=SC2034
GIT_DIR="$(yadm introspect repo 2>/dev/null)"
case "$penultimate" in
bootstrap)
COMPREPLY=()
return 0
;;
config)
COMPREPLY=( $(compgen -W "$(yadm introspect configs 2>/dev/null)") )
return 0
;;
decrypt)
COMPREPLY=( $(compgen -W "-l" -- "$current") )
return 0
;;
init)
COMPREPLY=( $(compgen -W "-f -w" -- "$current") )
return 0
;;
introspect)
COMPREPLY=( $(compgen -W "commands configs repo switches" -- "$current") )
return 0
;;
help)
COMPREPLY=() # no specific help yet
return 0
;;
list)
COMPREPLY=( $(compgen -W "-a" -- "$current") )
return 0
;;
esac
case "$antepenultimate" in
clone)
COMPREPLY=( $(compgen -W "-f -w --bootstrap --no-bootstrap" -- "$current") )
return 0
;;
esac
# this condition is so files are completed properly for --yadm-xxx options
if [[ ! "$penultimate" =~ ^- ]]; then
# TODO: somehow solve the problem with [--yadm-xxx option] being
# incompatible with what git expects, namely [--arg=option]
_git
fi
if [[ "$current" =~ ^- ]]; then
local matching
matching=$(compgen -W "$(yadm introspect switches 2>/dev/null)" -- "$current")
__gitcompappend "$matching"
fi
if [ "$COMP_CWORD" == 1 ] || [[ "$antepenultimate" =~ ^- ]] ; then
local matching
matching=$(compgen -W "$(yadm introspect commands 2>/dev/null)" -- "$current")
__gitcompappend "$matching"
fi
# remove duplicates found in COMPREPLY (a native bash way could be better)
if [ -n "${COMPREPLY[*]}" ]; then
COMPREPLY=($(echo "${COMPREPLY[@]}" | sort -u))
fi
}
complete -o bashdefault -o default -F _yadm yadm 2>/dev/null \
|| complete -o default -F _yadm yadm
fi