-
Notifications
You must be signed in to change notification settings - Fork 2
/
bash-completion
180 lines (166 loc) · 5.43 KB
/
bash-completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#
# Copyright 2017-2020,2023-2024 Gaël PORTAY
#
# SPDX-License-Identifier: LGPL-2.1-or-later
#
#
# Copyright (c) 2013-2023 Docker, Inc.
# 2017,2023 Gaël PORTAY
#
# SPDX-License-Identifier: Apache-2.0
#
__docker_q() {
docker ${host:+--host "$host"} ${config:+--config "$config"} ${context:+--context "$context"} 2>/dev/null "$@"
}
# __docker_containers returns a list of containers. Additional options to
# `docker ps` may be specified in order to filter the list, e.g.
# `__docker_containers --filter status=running`
# By default, only names are returned.
# Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs.
# An optional first option `--id|--name` may be used to limit the
# output to the IDs or names of matching items. This setting takes
# precedence over the environment setting.
__docker_containers() {
local format
if [ "${1-}" = "--id" ] ; then
format='{{.ID}}'
shift
elif [ "${1-}" = "--name" ] ; then
format='{{.Names}}'
shift
elif [ "${DOCKER_COMPLETION_SHOW_CONTAINER_IDS-}" = yes ] ; then
format='{{.ID}} {{.Names}}'
else
format='{{.Names}}'
fi
__docker_q ps --format "$format" "$@"
}
# __docker_complete_containers applies completion of containers based on the current
# value of `$cur` or the value of the optional first option `--cur`, if given.
# Additional filters may be appended, see `__docker_containers`.
__docker_complete_containers() {
local current="$cur"
if [ "${1-}" = "--cur" ] ; then
current="$2"
shift 2
fi
COMPREPLY=( $(compgen -W "$(__docker_containers "$@")" -- "$current") )
}
__docker_complete_containers_removable() {
__docker_complete_containers "$@" --filter status=created --filter status=exited
}
__docker_complete_containers_running() {
__docker_complete_containers "$@" --filter status=running
}
_docker_shell() {
_dosh
}
#
# Copyright © 2006-2008, Ian Macdonald <[email protected]>
# © 2009-2016, Bash Completion Maintainers
# 2017, Gaël PORTAY
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
_dosh()
{
local cur prev words cword
_init_completion || return
local shoptname="allexport errexit ignoreeof monitor noclobber noglob noexec nolog notify nounset verbose vi xtrace"
local bashoptname="autocd cdable_vars cdspell checkhash checkjobs checkwinsize cmdhist compat31 compat32 compat40 compat41 compat42 compat43 complete_fullquote direxpand dirspell dotglob execfail expand_aliases extdebug extglob extquote failglob force_fignore globasciiranges globstar gnu_errfmt histappend histreedit histverify hostcomplete huponexit inherit_errexit interactive_comments lastpipe lithist login_shell mailwarn no_empty_cmd_completion nocaseglob nocasematch nullglob progcomp promptvars restricted_shell shift_verbose sourcepath xpg_echo"
case $prev in
[-+]o)
COMPREPLY=( $( compgen -W "$shoptname" -- "$cur" ) )
return
;;
[-+]O)
COMPREPLY=( $( compgen -W "$bashoptname" -- "$cur" ) )
return
;;
-c)
COMPREPLY=( $(compgen -c "$cur") )
return
;;
--shell)
if [[ $COMPLETION_NODOSH ]]; then
_filedir
return
fi
local shells dockerfile directory
dockerfile=Dockerfile
directory=.
# before we scan for shells, see if a path to dockerfile was
# specified with --dockerfile and if the directory is to be
# changed with --directory
for (( i=0; i < ${#words[@]}; i++ )); do
if [[ ${words[i]} == --dockerfile ]]; then
# eval for tilde expansion
eval dockerfile=( "${words[i+1]}" )
elif [[ ${words[i]} == --directory ]]; then
# eval for tilde expansion
eval directory=( "${words[i+1]}" )
fi
done
shells="$(DOSH_NOBUILD=1 dosh --directory "$directory" --dockerfile "$dockerfile" --shell /bin/sh -c 'grep -v \# /etc/shells' 2>/dev/null)"
COMPREPLY=( $( compgen -W "$shells" -- "$cur" ) )
return
;;
--mount-options)
COMPREPLY=( $( compgen -W "rw ro z Z shared slage private delegated cached consistent nocopy" -- "$cur" ) )
return
;;
--dockerfile)
_filedir
return
;;
--context)
_filedir
return
;;
--directory|--working-directory)
_filedir -d
return
;;
--exec|--attach|--kill)
__docker_complete_containers_running
return
;;
--rm)
__docker_complete_containers_removable
return
;;
esac
local shopts="-a -b -e -f -h -m -n -u -v -x -C"
local short_opts="-c -s -i -o"
local long_opts="--dockerfile --context --no-auto-context --no-extra-options --no-doshprofile --no-doshrc --directory --working-directory --root --dind --home --mount-options --shell --ls --gc --build-only --build --rebuild --rmi --detach --exec --attach --kill --rm --tag --dry-run --verbose --version --help"
local shell="${DOSHELL:-/bin/sh}"
for (( i=0; i < ${#words[@]}; i++ )); do
if [[ ${words[i]} == --shell ]]; then
# eval for tilde expansion
eval shell="${words[i+1]}"
fi
done
case "${shell##*/}" in
bash)
shopts+=" -O -k -l -p -r -t -B -D -E -H -I -P -T"
;;
dash)
shopts+=" -p -q -E -I -V"
;;
zsh)
shopts+=" -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -d -g -k -l -p -r -t -w -y -B -D -E -F -G -H -I -J -K -L -M -N -O -P -Q -R -S -T -U -V -W -X -Y -Z"
;;
esac
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W "$short_opts $long_opts $shopts" -- "$cur" ) )
return
elif [[ "$cur" == +* ]]; then
COMPREPLY=( $( compgen -W "${shopts//-/+}" -- "$cur" ) )
return
fi
local args ext=
_count_args "" "@(-c|[-+]o|--dockerfile|--context|--directory|--working-directory|--mount-options|--shell|--exec|--attach|--kill|--rm)"
((args == 1)) && ext="*sh"
_filedir $ext
} &&
complete -F _dosh dosh posh zdosh