Skip to content

Commit 9bca8f3

Browse files
author
Ma Shimiao
committed
Add bash-completion for oci-image-tool
Signed-off-by: Ma Shimiao <[email protected]>
1 parent 749082e commit 9bca8f3

File tree

2 files changed

+241
-1
lines changed

2 files changed

+241
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ install: man
4848
install -d -m 755 $(PREFIX)/share/man/man1
4949
install -m 644 *.1 $(PREFIX)/share/man/man1
5050
install -d -m 755 $(PREFIX)/share/bash-completion/completions
51-
install -m 644 completions/bash/oci-image-tool $(PREFIX)/share/bash-completion/completionsn
51+
install -m 644 completions/bash/oci-image-tool $(PREFIX)/share/bash-completion/completions
5252

5353
uninstall:
5454
rm -f $(BINDIR)/oci-image-tool

completions/bash/oci-image-tool

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
#!/bin/bash
2+
#
3+
# bash completion file for core oci-image-tool commands
4+
#
5+
# This script provides completion of:
6+
# - commands and their options
7+
# - filepaths
8+
#
9+
# To enable the completions either:
10+
# - place this file in /usr/share/bash-completion/completions
11+
# or
12+
# - copy this file to e.g. ~/.oci-image-tool-completion.sh and add the line
13+
# below to your .bashrc after bash completion features are loaded
14+
# . ~/.oci-image-tool-completion.sh
15+
#
16+
# Configuration:
17+
#
18+
19+
20+
# Note for developers:
21+
# Please arrange options sorted alphabetically by long name with the short
22+
# options immediately following their corresponding long form.
23+
# This order should be applied to lists, alternatives and code blocks.
24+
25+
__oci-image-tool_previous_extglob_setting=$(shopt -p extglob)
26+
shopt -s extglob
27+
28+
__oci-image-tool_pos_first_nonflag() {
29+
local argument_flags=$1
30+
31+
local counter=$((${subcommand_pos:-${command_pos}} + 1))
32+
while [ $counter -le $cword ]; do
33+
if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
34+
(( counter++ ))
35+
else
36+
case "${words[$counter]}" in
37+
-*)
38+
;;
39+
*)
40+
break
41+
;;
42+
esac
43+
fi
44+
(( counter++ ))
45+
done
46+
47+
echo $counter
48+
}
49+
50+
# Transforms a multiline list of strings into a single line string
51+
# with the words separated by "|".
52+
# This is used to prepare arguments to __oci-image-tool_pos_first_nonflag().
53+
__oci-image-tool_to_alternatives() {
54+
local parts=( $1 )
55+
local IFS='|'
56+
echo "${parts[*]}"
57+
}
58+
59+
# Transforms a multiline list of options into an extglob pattern
60+
# suitable for use in case statements.
61+
__oci-image-tool_to_extglob() {
62+
local extglob=$( __oci-image-tool_to_alternatives "$1" )
63+
echo "@($extglob)"
64+
}
65+
66+
# Subcommand processing.
67+
# Locates the first occurrence of any of the subcommands contained in the
68+
# first argument. In case of a match, calls the corresponding completion
69+
# function and returns 0.
70+
# If no match is found, 1 is returned. The calling function can then
71+
# continue processing its completion.
72+
#
73+
# TODO if the preceding command has options that accept arguments and an
74+
# argument is equal ot one of the subcommands, this is falsely detected as
75+
# a match.
76+
__oci-image-tool_subcommands() {
77+
local subcommands="$1"
78+
79+
local counter=$(($command_pos + 1))
80+
while [ $counter -lt $cword ]; do
81+
case "${words[$counter]}" in
82+
$(__oci-image-tool_to_extglob "$subcommands") )
83+
subcommand_pos=$counter
84+
local subcommand=${words[$counter]}
85+
local completions_func=_oci-image-tool_${command}_${subcommand}
86+
declare -F $completions_func >/dev/null && $completions_func
87+
return 0
88+
;;
89+
esac
90+
(( counter++ ))
91+
done
92+
return 1
93+
}
94+
95+
# suppress trailing whitespace
96+
__oci-image-tool_nospace() {
97+
# compopt is not available in ancient bash versions
98+
type compopt &>/dev/null && compopt -o nospace
99+
}
100+
101+
__oci-image-tool_complete_common_types() {
102+
# The list of types, ALL was added manually.
103+
COMPREPLY=( $( compgen -W "
104+
image
105+
imageLayout
106+
" -- "$cur" ) )
107+
}
108+
109+
__oci-image-tool_complete_validate_types() {
110+
# The list of types, ALL was added manually.
111+
COMPREPLY=( $( compgen -W "
112+
config
113+
image
114+
imageIndex
115+
imageLayout
116+
manifest
117+
" -- "$cur" ) )
118+
}
119+
120+
# global options that may appear after the oci-image-tool command
121+
_oci-image-tool_oci-image-tool() {
122+
local boolean_options="
123+
--debug
124+
--help -h
125+
--version -v
126+
"
127+
128+
local all_options="$boolean_options"
129+
130+
case "$cur" in
131+
-*)
132+
COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
133+
;;
134+
*)
135+
local counter=$( __oci-image-tool_pos_first_nonflag )
136+
if [ $cword -eq $counter ]; then
137+
COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
138+
fi
139+
;;
140+
esac
141+
}
142+
143+
_oci-image-tool_create() {
144+
case "$prev" in
145+
--type)
146+
__oci-image-tool_complete_common_types
147+
return
148+
;;
149+
esac
150+
151+
case "$cur" in
152+
-*)
153+
COMPREPLY=( $( compgen -W "--type --ref --rootfs --help -h" -- "$cur" ) )
154+
;;
155+
esac
156+
157+
}
158+
159+
_oci-image-tool_unpack() {
160+
case "$prev" in
161+
--type)
162+
__oci-image-tool_complete_common_types
163+
return
164+
;;
165+
esac
166+
167+
case "$cur" in
168+
-*)
169+
COMPREPLY=( $( compgen -W "--type --ref --help -h" -- "$cur" ) )
170+
;;
171+
esac
172+
173+
}
174+
175+
_oci-image-tool_validate() {
176+
case "$prev" in
177+
--type)
178+
__oci-image-tool_complete_validate_types
179+
return
180+
;;
181+
esac
182+
183+
case "$cur" in
184+
-*)
185+
COMPREPLY=( $( compgen -W "--type --ref --help -h" -- "$cur" ) )
186+
;;
187+
esac
188+
189+
}
190+
191+
_oci-image-tool_help() {
192+
local counter=$(__oci-image-tool_pos_first_nonflag)
193+
if [ $cword -eq $counter ]; then
194+
COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
195+
fi
196+
}
197+
198+
_oci-image-tool() {
199+
local previous_extglob_setting=$(shopt -p extglob)
200+
shopt -s extglob
201+
202+
local commands=(
203+
create
204+
validate
205+
unpack
206+
)
207+
208+
COMPREPLY=()
209+
local cur prev words cword
210+
_get_comp_words_by_ref -n : cur prev words cword
211+
212+
local command='oci-image-tool' command_pos=0 subcommand_pos
213+
local counter=1
214+
while [ $counter -lt $cword ]; do
215+
case "${words[$counter]}" in
216+
-*)
217+
;;
218+
=)
219+
(( counter++ ))
220+
;;
221+
*)
222+
command="${words[$counter]}"
223+
command_pos=$counter
224+
break
225+
;;
226+
esac
227+
(( counter++ ))
228+
done
229+
230+
local completions_func=_oci-image-tool_${command}
231+
declare -F $completions_func >/dev/null && $completions_func
232+
233+
eval "$previous_extglob_setting"
234+
return 0
235+
}
236+
237+
eval "$__oci-image-tool_previous_extglob_setting"
238+
unset __oci-image-tool_previous_extglob_setting
239+
240+
complete -F _oci-image-tool oci-image-tool

0 commit comments

Comments
 (0)