-
Notifications
You must be signed in to change notification settings - Fork 14
/
fast-vm.bash_completion
109 lines (100 loc) · 3.1 KB
/
fast-vm.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
# fast-vm completion -*- shell-script -*-
_fast-vm()
{
local cur prev words cword split vm_prefix
_init_completion -s || return
if [ -f /etc/fast-vm.conf ]; then
vm_prefix=$(grep VM_PREFIX /etc/fast-vm.conf|tail -1 |cut -d\= -f 2)
else
printf "\n/etc/fast-vm.conf not found. Run 'configure-fast-vm' to generate it first.\n"
return 0
fi
if [ "$cword" -eq 3 ] && [ ${COMP_WORDS[1]} == "start" ]; then
COMPREPLY=( $( compgen -W "console keydist ssh" -- "$cur" ) )
return 0
fi
if [ "$cword" -eq 3 ] && [ ${COMP_WORDS[1]} == "stop" ]; then
COMPREPLY=( $( compgen -W "graceful" -- "$cur" ) )
return 0
fi
if [ "$cword" -eq 3 ] && [ ${COMP_WORDS[1]} == "list" ]; then
COMPREPLY=( $( compgen -W "short" -- "$cur" ) )
return 0
fi
if [ "$cword" -eq 3 ] && [ ${COMP_WORDS[1]} == "create" ]; then
local number_used number_all number_available
number_used=$(mktemp)
number_all=$(mktemp)
virsh --connect qemu:///system list --all|awk '{print $2}'|grep -E "^${vm_prefix}.*-[0-9]+"|sed 's/.*-\([0-9]\+\)$/\1/'> $number_used
seq 20 220 > $number_all
number_available=$(comm -2 -3 <(sort $number_all) <(sort $number_used))
COMPREPLY=( $( compgen -W "base $number_available" -- "$cur" ) )
rm -f $number_all $number_used
return 0
fi
if [ "$cword" -eq 4 ] && [ ${COMP_WORDS[1]} == "create" ]; then
_filedir '@(xml)'
return 0
fi
if [ "$cword" -eq 5 ] && [ ${COMP_WORDS[1]} == "create" ]; then
_filedir '@(sh)'
return 0
fi
if [ "$cword" -eq 3 ] && [ ${COMP_WORDS[1]} == "delete" ]; then
_filedir '@(sh)'
return 0
fi
if [ "$cword" -gt 2 ] && [ ${COMP_WORDS[1]} == "scp" ]; then
# offer any file or keyword 'vm:'
COMPREPLY=( $( compgen -W "vm:" -f -- "$cur" ) )
return 0
fi
if [ "$cword" -gt 2 ] && [ ${COMP_WORDS[1]} == "rsync" ]; then
# offer any file or keyword 'vm:'
COMPREPLY=( $( compgen -W "vm:" -f -- "$cur" ) )
return 0
fi
case $prev in
create)
local image_name
local profile_name
image_name=$(fast-vm-image list short)
profile_name=$(fast-vm-image list_profiles short)
COMPREPLY=( $( compgen -W "$image_name $profile_name" -- "$cur" ) )
return 0
;;
list)
COMPREPLY=( $( compgen -W "all active inactive" -- "$cur" ) )
return 0
;;
start|stop|delete|console|keydist|scp|rsync|ssh|info|resize|edit_note|compact)
local vm_numbers
case $prev in
start|resize|compact)
vm_numbers=$(fast-vm list inactive short)
;;
delete|edit_note|info)
vm_numbers=$(fast-vm list all short)
;;
console|keydist|scp|rsync|ssh|stop)
vm_numbers=$(fast-vm list active short)
;;
esac
COMPREPLY=( $( compgen -W "$vm_numbers" -- "$cur" ) )
return 0
;;
disk_usage)
local vm_numbers
vm_numbers=$(fast-vm list all short)
COMPREPLY=( $( compgen -W "all $vm_numbers" -- "$cur" ) )
return 0
;;
esac
$split && return 0
if [ "$cword" -lt 2 ]; then
COMPREPLY=( $( compgen -W 'edit_note keydist create start stop delete resize console scp rsync ssh list info compact disk_usage' -- "$cur" ) )
return 0
fi
} &&
complete -F _fast-vm fast-vm
# ex: ts=4 sw=4 et filetype=sh