Permalink
Browse files

Add bash completion for `docker-machine mount`

Signed-off-by: Harald Albers <github@albersweb.de>
  • Loading branch information...
albers committed Oct 27, 2017
1 parent f748ac1 commit cf9d44d45ae7f99fbe3fa0320da6858ccc8f4e30
Showing with 52 additions and 2 deletions.
  1. +52 −2 contrib/completion/bash/docker-machine.bash
@@ -86,6 +86,40 @@ _docker_machine_map_key_of_current_option() {
[[ ${words[$glob_pos]} == $glob ]] && echo "$key"
}
# Finds the position of the first word that is neither option nor an option's argument.
# If there are options that require arguments, you need to pass a glob describing
# those options, e.g. "--option1|-o|--option2".
# Use this function to restrict completions to exact positions after the options.
_docker_machine_pos_first_nonflag() {
local argument_flags=$1
local counter=$((${subcommand_pos:-${command_pos}} + 1))
while [ "$counter" -le "$cword" ]; do
if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
(( counter++ ))
# eat "=" in case of --option=arg syntax
[ "${words[$counter]}" = "=" ] && (( counter++ ))
else
case "${words[$counter]}" in
-*)
;;
*)
break
;;
esac
fi
# Bash splits words at "=", retaining "=" as a word, examples:
# "--debug=false" => 3 words, "--log-opt syslog-facility=daemon" => 4 words
while [ "${words[$counter + 1]}" = "=" ] ; do
counter=$(( counter + 2))
done
(( counter++ ))
done
echo $counter
}
# --- completion functions ---------------------------------------------------
_docker_machine_active() {
@@ -208,6 +242,21 @@ _docker_machine_ls() {
fi
}
_docker_machine_mount() {
if [[ "${cur}" == -* ]]; then
COMPREPLY=($(compgen -W "--help --unmount -u" -- "${cur}"))
else
local pos=$(_docker_machine_pos_first_nonflag)
if [ "$cword" -eq "$pos" ]; then
# We can't complete remote filesystems. All we can do here is to complete the machine.
COMPREPLY=($(compgen -W "$(_docker_machine_machines --filter state=Running)" -S: -- "${cur}"))
_docker_machine_nospace
elif [ "$cword" -eq "$((pos + 1))" ]; then
_filedir -d
fi
fi
}
_docker_machine_provision() {
if [[ "${cur}" == -* ]]; then
COMPREPLY=($(compgen -W "--help" -- "${cur}"))
@@ -329,7 +378,7 @@ _docker_machine_docker_machine() {
_docker_machine() {
COMPREPLY=()
local commands=(active config create env inspect ip kill ls provision regenerate-certs restart rm ssh scp start status stop upgrade url version help)
local commands=(active config create env inspect ip kill ls mount provision regenerate-certs restart rm ssh scp start status stop upgrade url version help)
local flags=(--debug --native-ssh --github-api-token --bugsnag-api-token --help --version)
local wants_dir=(--storage-path)
@@ -343,7 +392,7 @@ _docker_machine() {
local cur prev words cword
_get_comp_words_by_ref -n : cur prev words cword
local i
local command=docker-machine
local command=docker-machine command_pos=0
for (( i=1; i < ${cword}; ++i)); do
local word=${words[i]}
@@ -352,6 +401,7 @@ _docker_machine() {
(( ++i ))
elif [[ " ${commands[*]} " =~ " ${word} " ]]; then
command=${word}
command_pos=$i
fi
done

0 comments on commit cf9d44d

Please sign in to comment.