Skip to content

Commit 72095f5

Browse files
committed
Merge pull request #639 from albers/bash-completion
Bash completion for fig command
2 parents 4827e60 + 2406a39 commit 72095f5

File tree

3 files changed

+351
-0
lines changed

3 files changed

+351
-0
lines changed

contrib/completion/bash/fig

+316
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
#!bash
2+
#
3+
# bash completion for fig commands
4+
#
5+
# This work is based on the completion for the docker command.
6+
#
7+
# This script provides completion of:
8+
# - commands and their options
9+
# - service names
10+
# - filepaths
11+
#
12+
# To enable the completions either:
13+
# - place this file in /etc/bash_completion.d
14+
# or
15+
# - copy this file and add the line below to your .bashrc after
16+
# bash completion features are loaded
17+
# . docker.bash
18+
#
19+
# Note:
20+
# Some completions require the current user to have sufficient permissions
21+
# to execute the docker command.
22+
23+
24+
# Extracts all service names from the figfile.
25+
___fig_all_services_in_figfile() {
26+
awk -F: '/^[a-zA-Z0-9]/{print $1}' "${fig_file:-fig.yml}"
27+
}
28+
29+
# All services, even those without an existing container
30+
__fig_services_all() {
31+
COMPREPLY=( $(compgen -W "$(___fig_all_services_in_figfile)" -- "$cur") )
32+
}
33+
34+
# All services that have an entry with the given key in their figfile section
35+
___fig_services_with_key() {
36+
# flatten sections to one line, then filter lines containing the key and return section name.
37+
awk '/^[a-zA-Z0-9]/{printf "\n"};{printf $0;next;}' fig.yml | awk -F: -v key=": +$1:" '$0 ~ key {print $1}'
38+
}
39+
40+
# All services that are defined by a Dockerfile reference
41+
__fig_services_from_build() {
42+
COMPREPLY=( $(compgen -W "$(___fig_services_with_key build)" -- "$cur") )
43+
}
44+
45+
# All services that are defined by an image
46+
__fig_services_from_image() {
47+
COMPREPLY=( $(compgen -W "$(___fig_services_with_key image)" -- "$cur") )
48+
}
49+
50+
# The services for which containers have been created, optionally filtered
51+
# by a boolean expression passed in as argument.
52+
__fig_services_with() {
53+
local containers names
54+
containers="$(fig 2>/dev/null ${fig_file:+-f $fig_file} ${fig_project:+-p $fig_project} ps -q)"
55+
names=( $(docker 2>/dev/null inspect --format "{{if ${1:-true}}} {{ .Name }} {{end}}" $containers) )
56+
names=( ${names[@]%_*} ) # strip trailing numbers
57+
names=( ${names[@]#*_} ) # strip project name
58+
COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") )
59+
}
60+
61+
# The services for which at least one running container exists
62+
__fig_services_running() {
63+
__fig_services_with '.State.Running'
64+
}
65+
66+
# The services for which at least one stopped container exists
67+
__fig_services_stopped() {
68+
__fig_services_with 'not .State.Running'
69+
}
70+
71+
72+
_fig_build() {
73+
case "$cur" in
74+
-*)
75+
COMPREPLY=( $( compgen -W "--no-cache" -- "$cur" ) )
76+
;;
77+
*)
78+
__fig_services_from_build
79+
;;
80+
esac
81+
}
82+
83+
84+
_fig_fig() {
85+
case "$prev" in
86+
--file|-f)
87+
_filedir
88+
return
89+
;;
90+
--project-name|-p)
91+
return
92+
;;
93+
esac
94+
95+
case "$cur" in
96+
-*)
97+
COMPREPLY=( $( compgen -W "--help -h --verbose --version --file -f --project-name -p" -- "$cur" ) )
98+
;;
99+
*)
100+
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
101+
;;
102+
esac
103+
}
104+
105+
106+
_fig_help() {
107+
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
108+
}
109+
110+
111+
_fig_kill() {
112+
case "$prev" in
113+
-s)
114+
COMPREPLY=( $( compgen -W "SIGHUP SIGINT SIGKILL SIGUSR1 SIGUSR2" -- "$(echo $cur | tr '[:lower:]' '[:upper:]')" ) )
115+
return
116+
;;
117+
esac
118+
119+
case "$cur" in
120+
-*)
121+
COMPREPLY=( $( compgen -W "-s" -- "$cur" ) )
122+
;;
123+
*)
124+
__fig_services_running
125+
;;
126+
esac
127+
}
128+
129+
130+
_fig_logs() {
131+
case "$cur" in
132+
-*)
133+
COMPREPLY=( $( compgen -W "--no-color" -- "$cur" ) )
134+
;;
135+
*)
136+
__fig_services_all
137+
;;
138+
esac
139+
}
140+
141+
142+
_fig_port() {
143+
case "$prev" in
144+
--protocol)
145+
COMPREPLY=( $( compgen -W "tcp udp" -- "$cur" ) )
146+
return;
147+
;;
148+
--index)
149+
return;
150+
;;
151+
esac
152+
153+
case "$cur" in
154+
-*)
155+
COMPREPLY=( $( compgen -W "--protocol --index" -- "$cur" ) )
156+
;;
157+
*)
158+
__fig_services_all
159+
;;
160+
esac
161+
}
162+
163+
164+
_fig_ps() {
165+
case "$cur" in
166+
-*)
167+
COMPREPLY=( $( compgen -W "-q" -- "$cur" ) )
168+
;;
169+
*)
170+
__fig_services_all
171+
;;
172+
esac
173+
}
174+
175+
176+
_fig_pull() {
177+
case "$cur" in
178+
-*)
179+
COMPREPLY=( $( compgen -W "--allow-insecure-ssl" -- "$cur" ) )
180+
;;
181+
*)
182+
__fig_services_from_image
183+
;;
184+
esac
185+
}
186+
187+
188+
_fig_restart() {
189+
__fig_services_running
190+
}
191+
192+
193+
_fig_rm() {
194+
case "$cur" in
195+
-*)
196+
COMPREPLY=( $( compgen -W "--force -v" -- "$cur" ) )
197+
;;
198+
*)
199+
__fig_services_stopped
200+
;;
201+
esac
202+
}
203+
204+
205+
_fig_run() {
206+
case "$prev" in
207+
-e)
208+
COMPREPLY=( $( compgen -e -- "$cur" ) )
209+
compopt -o nospace
210+
return
211+
;;
212+
--entrypoint)
213+
return
214+
;;
215+
esac
216+
217+
case "$cur" in
218+
-*)
219+
COMPREPLY=( $( compgen -W "--allow-insecure-ssl -d --entrypoint -e --no-deps --rm -T" -- "$cur" ) )
220+
;;
221+
*)
222+
__fig_services_all
223+
;;
224+
esac
225+
}
226+
227+
228+
_fig_scale() {
229+
case "$prev" in
230+
=)
231+
COMPREPLY=("$cur")
232+
;;
233+
*)
234+
COMPREPLY=( $(compgen -S "=" -W "$(___fig_all_services_in_figfile)" -- "$cur") )
235+
compopt -o nospace
236+
;;
237+
esac
238+
}
239+
240+
241+
_fig_start() {
242+
__fig_services_stopped
243+
}
244+
245+
246+
_fig_stop() {
247+
__fig_services_running
248+
}
249+
250+
251+
_fig_up() {
252+
case "$cur" in
253+
-*)
254+
COMPREPLY=( $( compgen -W "--allow-insecure-ssl -d --no-build --no-color --no-deps --no-recreate" -- "$cur" ) )
255+
;;
256+
*)
257+
__fig_services_all
258+
;;
259+
esac
260+
}
261+
262+
263+
_fig() {
264+
local commands=(
265+
build
266+
help
267+
kill
268+
logs
269+
port
270+
ps
271+
pull
272+
restart
273+
rm
274+
run
275+
scale
276+
start
277+
stop
278+
up
279+
)
280+
281+
COMPREPLY=()
282+
local cur prev words cword
283+
_get_comp_words_by_ref -n : cur prev words cword
284+
285+
# search subcommand and invoke its handler.
286+
# special treatment of some top-level options
287+
local command='fig'
288+
local counter=1
289+
local fig_file fig_project
290+
while [ $counter -lt $cword ]; do
291+
case "${words[$counter]}" in
292+
-f|--file)
293+
(( counter++ ))
294+
fig_file="${words[$counter]}"
295+
;;
296+
-p|--project-name)
297+
(( counter++ ))
298+
fig_project="${words[$counter]}"
299+
;;
300+
-*)
301+
;;
302+
*)
303+
command="${words[$counter]}"
304+
break
305+
;;
306+
esac
307+
(( counter++ ))
308+
done
309+
310+
local completions_func=_fig_${command}
311+
declare -F $completions_func >/dev/null && $completions_func
312+
313+
return 0
314+
}
315+
316+
complete -F _fig fig

docs/completion.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
layout: default
3+
title: Command Completion
4+
---
5+
6+
Command Completion
7+
==================
8+
9+
Fig comes with [command completion](http://en.wikipedia.org/wiki/Command-line_completion)
10+
for the bash shell.
11+
12+
Installing Command Completion
13+
-----------------------------
14+
15+
Make sure bash completion is installed. If you use a current Linux in a non-minimal installation, bash completion should be available.
16+
On a Mac, install with `brew install bash-completion`
17+
18+
Place the completion script in `/etc/bash_completion.d/` (`/usr/local/etc/bash_completion.d/` on a Mac), using e.g.
19+
20+
curl -L https://raw.githubusercontent.com/docker/fig/master/contrib/completion/bash/fig > /etc/bash_completion.d/fig
21+
22+
Completion will be available upon next login.
23+
24+
Available completions
25+
---------------------
26+
Depending on what you typed on the command line so far, it will complete
27+
28+
- available fig commands
29+
- options that are available for a particular command
30+
- service names that make sense in a given context (e.g. services with running or stopped instances or services based on images vs. services based on Dockerfiles). For `fig scale`, completed service names will automatically have "=" appended.
31+
- arguments for selected options, e.g. `fig kill -s` will complete some signals like SIGHUP and SIGUSR1.
32+
33+
Enjoy working with fig faster and with less typos!

docs/install.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Next, install Fig:
2020

2121
curl -L https://github.com/docker/fig/releases/download/1.0.1/fig-`uname -s`-`uname -m` > /usr/local/bin/fig; chmod +x /usr/local/bin/fig
2222

23+
Optionally, install [command completion](completion.html) for the bash shell.
24+
2325
Releases are available for OS X and 64-bit Linux. Fig is also available as a Python package if you're on another platform (or if you prefer that sort of thing):
2426

2527
$ sudo pip install -U fig

0 commit comments

Comments
 (0)