Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 6f12672

Browse files
authored
Merge pull request #4300 from albers/completion-mount
Add bash completion for `docker-machine mount`
2 parents 40d1159 + cf9d44d commit 6f12672

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

contrib/completion/bash/docker-machine.bash

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ _docker_machine_map_key_of_current_option() {
8686
[[ ${words[$glob_pos]} == $glob ]] && echo "$key"
8787
}
8888

89+
# Finds the position of the first word that is neither option nor an option's argument.
90+
# If there are options that require arguments, you need to pass a glob describing
91+
# those options, e.g. "--option1|-o|--option2".
92+
# Use this function to restrict completions to exact positions after the options.
93+
_docker_machine_pos_first_nonflag() {
94+
local argument_flags=$1
95+
96+
local counter=$((${subcommand_pos:-${command_pos}} + 1))
97+
while [ "$counter" -le "$cword" ]; do
98+
if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
99+
(( counter++ ))
100+
# eat "=" in case of --option=arg syntax
101+
[ "${words[$counter]}" = "=" ] && (( counter++ ))
102+
else
103+
case "${words[$counter]}" in
104+
-*)
105+
;;
106+
*)
107+
break
108+
;;
109+
esac
110+
fi
111+
112+
# Bash splits words at "=", retaining "=" as a word, examples:
113+
# "--debug=false" => 3 words, "--log-opt syslog-facility=daemon" => 4 words
114+
while [ "${words[$counter + 1]}" = "=" ] ; do
115+
counter=$(( counter + 2))
116+
done
117+
118+
(( counter++ ))
119+
done
120+
121+
echo $counter
122+
}
89123
# --- completion functions ---------------------------------------------------
90124

91125
_docker_machine_active() {
@@ -208,6 +242,21 @@ _docker_machine_ls() {
208242
fi
209243
}
210244

245+
_docker_machine_mount() {
246+
if [[ "${cur}" == -* ]]; then
247+
COMPREPLY=($(compgen -W "--help --unmount -u" -- "${cur}"))
248+
else
249+
local pos=$(_docker_machine_pos_first_nonflag)
250+
if [ "$cword" -eq "$pos" ]; then
251+
# We can't complete remote filesystems. All we can do here is to complete the machine.
252+
COMPREPLY=($(compgen -W "$(_docker_machine_machines --filter state=Running)" -S: -- "${cur}"))
253+
_docker_machine_nospace
254+
elif [ "$cword" -eq "$((pos + 1))" ]; then
255+
_filedir -d
256+
fi
257+
fi
258+
}
259+
211260
_docker_machine_provision() {
212261
if [[ "${cur}" == -* ]]; then
213262
COMPREPLY=($(compgen -W "--help" -- "${cur}"))
@@ -329,7 +378,7 @@ _docker_machine_docker_machine() {
329378

330379
_docker_machine() {
331380
COMPREPLY=()
332-
local commands=(active config create env inspect ip kill ls provision regenerate-certs restart rm ssh scp start status stop upgrade url version help)
381+
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)
333382

334383
local flags=(--debug --native-ssh --github-api-token --bugsnag-api-token --help --version)
335384
local wants_dir=(--storage-path)
@@ -343,7 +392,7 @@ _docker_machine() {
343392
local cur prev words cword
344393
_get_comp_words_by_ref -n : cur prev words cword
345394
local i
346-
local command=docker-machine
395+
local command=docker-machine command_pos=0
347396

348397
for (( i=1; i < ${cword}; ++i)); do
349398
local word=${words[i]}
@@ -352,6 +401,7 @@ _docker_machine() {
352401
(( ++i ))
353402
elif [[ " ${commands[*]} " =~ " ${word} " ]]; then
354403
command=${word}
404+
command_pos=$i
355405
fi
356406
done
357407

0 commit comments

Comments
 (0)