Working completion app and server

This commit is contained in:
3wc 2020-10-30 17:50:20 +02:00
parent c6b841de6c
commit 3e0b9e9475
2 changed files with 112 additions and 45 deletions

12
abra
View File

@ -842,18 +842,6 @@ sub_volume() {
docker volume $@ docker volume $@
} }
#######################################
# Bash completion helpers
#######################################
sub_command_ls() {
docopt_exit 2>&1 | \
tail -n+2 | \
sed -s -e 's/^\s\+//g' -e 's/(\([^)]*\)|.*)/\1/' -e 's/\[options\]//g' \
-e 's/ <[^>]\+>//g' -e 's/\.\.\.//g' -e 's/^abra\s*//' -e '/^$/d' | \
sort -u
}
####################################### #######################################
# Main # Main
####################################### #######################################

View File

@ -1,37 +1,116 @@
#!/env bash #!/usr/bin/env bash
#/usr/bin/env bash _abra_basename()
_abra_completions()
{ {
local CUR PREV COMMANDS_LIST COMMANDS echo "${1##*/}"
CUR=${COMP_WORDS[COMP_CWORD]}
PREV=${COMP_WORDS[COMP_CWORD-1]}
mapfile -t COMMANDS_LIST < <(abra command_ls)
echo "${COMMANDS_LIST[@]}"
case ${COMP_CWORD} in
1)
COMPREPLY=($(compgen -W "${COMMANDS_LIST[*]%% *}" -- ${CUR}))
;;
2)
SUB_COMMANDS=()
for COMMAND in "${COMMANDS_LIST[@]}"; do
echo $PREV
if [[ COMMAND =~ $PREV ]]; then
echo "FOUND $PREV $COMMAND"
SUB_COMMANDS+=("$COMMAND")
fi
done
COMPREPLY=($(compgen -W "${COMMANDS[*]#* }" -- ${CUR}))
;;
*)
echo $COMP_CWORD
COMPREPLY=()
;;
esac
} }
complete -F _abra_completions abra _abra_servers()
{
# FIXME 3wc: copied from abra/get_servers()
shopt -s nullglob dotglob
local SERVERS=(~/.abra/servers/*)
shopt -u nullglob dotglob
for SERVER in "${SERVERS[@]}"; do
_abra_basename "${SERVER}"
done
}
_abra_complete_servers()
{
mapfile -t COMPREPLY < <(compgen -W "$(_abra_servers)" -- "$1")
}
_abra_apps()
{
shopt -s nullglob dotglob
local APPS=(~/.abra/servers/*/*.env)
shopt -u nullglob dotglob
for APP in "${APPS[@]}"; do
_abra_basename "${APP%.env}"
done
}
_abra_complete_apps()
{
mapfile -t COMPREPLY < <(compgen -W "$(_abra_apps)" -- "$1")
}
_abra_complete()
{
compopt +o default +o nospace
local -r cmds='
app
server
'
local -r short_opts='-e -h -s -v'
local -r long_opts='--env --help --stack --version'
# Scan through the command line and find the abra command
# (if present), as well as its expected position.
local cmd
local cmd_index=1 # Expected index of the command token.
local i
for (( i = 1; i < ${#COMP_WORDS[@]}; i++ )); do
local word="${COMP_WORDS[i]}"
case "$word" in
-*)
((cmd_index++))
;;
*)
cmd="$word"
break
;;
esac
done
local cur="${COMP_WORDS[COMP_CWORD]}"
if (( COMP_CWORD < cmd_index )); then
# Offer option completions.
case "$cur" in
--*)
mapfile -t COMPREPLY < <(compgen -W "$long_opts" -- "$cur")
;;
-*)
mapfile -t COMPREPLY < <(compgen -W "$short_opts" -- "$cur")
;;
*)
# Skip completion; we should never get here.
;;
esac
elif (( COMP_CWORD == cmd_index )); then
# Offer command name completions.
mapfile -t COMPREPLY < <(compgen -W "$cmds" -- "$cur")
else
# Offer command argument completions.
case "$cmd" in
server)
# Offer exactly one server name completion.
if (( COMP_CWORD == cmd_index + 2 )); then
_abra_complete_servers "$cur"
fi
;;
app)
# Offer exactly one app completion.
if (( COMP_CWORD == cmd_index + 1 )); then
_abra_complete_apps "$cur"
fi
;;
#help)
# # Offer exactly one command name completion.
# if (( COMP_CWORD == cmd_index + 1 )); then
# COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
# fi
# ;;
*)
# Unknown command or unknowable argument.
;;
esac
fi
}
complete -o default -F _abra_complete abra