-
Notifications
You must be signed in to change notification settings - Fork 916
/
govc_bash_completion
executable file
·107 lines (101 loc) · 4.34 KB
/
govc_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
#!/bin/bash
# govc Bash completion script
# place in etc/bash_completion.d/ or source on command line with "."
_govc_complete()
{
local IFS=$'\n'
local cur prev subcmd
prev=${COMP_WORDS[COMP_CWORD-1]}
cur=${COMP_WORDS[COMP_CWORD]}
subcmd=${COMP_WORDS[1]}
COMPREPLY=()
if [[ ${prev} == "govc" ]] ; then # show subcommands, no options
COMPREPLY=( $(compgen -W "$(govc -h | grep -v Usage | awk '{print $1}' )" -- ${cur}) )
return 0
elif [[ ${cur} == "-"* ]] ; then
: # drop out and show options
elif [[ ${subcmd} == "ls" ]] ; then # not completing an option, try for appropriate values
if [[ ${prev} == "-t" ]] ; then
COMPREPLY=( $(compgen -W "$(govc ls -l "/**" | awk '{print $2}' | sort -u | tr -d '()' )" -- ${cur}) )
else
COMPREPLY=( $(compgen -W "$(govc ls /\*\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) )
fi
elif [[ ${prev} == "-net" ]] ; then
CANDIDATES=( $(compgen -W "$(govc ls /\*/network/\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) )
if [ ${#CANDIDATES[*]} -eq 0 ]; then
COMPREPLY=()
else
COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}"))
fi
elif [[ ${prev} == "-host" ]] ; then
COMPREPLY=( $(compgen -W "$(govc ls /\*/host/\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) )
elif [[ ${prev} == "-ds" ]] ; then
CANDIDATES=( $(compgen -W "$(govc ls /\*/datastore/\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) )
if [ ${#CANDIDATES[*]} -eq 0 ]; then
COMPREPLY=()
else
COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}"))
fi
elif [[ ${prev} == "-pool" ]] ; then
CANDIDATES=( $(compgen -W "$(govc find -type p)" -- ${cur}) )
if [ ${#CANDIDATES[*]} -eq 0 ]; then
COMPREPLY=()
else
COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}"))
fi
elif [[ ${subcmd} == "vm.network.change" ]] ; then
# Example of sub command
#govc vm.network.change -vm linuxserver1 -net Pvt_1289_Server ethernet-0
# Get device
local i=1
local vm=""
while [[ "$i" -lt "$COMP_CWORD" ]]; do
if [[ ${COMP_WORDS[i]} == "-vm" ]]; then
local vm=${COMP_WORDS[((i+1))]}
fi
(( i++ ))
done
COMPREPLY=( $(compgen -W "$(govc device.ls -vm $vm eth* | awk '{print $1}')" -- ${cur}))
elif [[ ${prev} == "-disk.label" ]] ; then
local i=1
local vm=""
while [[ "$i" -lt "$COMP_CWORD" ]]; do
if [[ ${COMP_WORDS[i]} == "-vm" ]]; then
local vm=${COMP_WORDS[((i+1))]}
fi
(( i++ ))
done
CANDIDATES=( $(compgen -W "$(govc device.info -json -vm $vm disk-* | jq '.devices[].deviceInfo.label' | sed 's/"//g')" -- ${cur}))
if [ ${#CANDIDATES[*]} -eq 0 ]; then
COMPREPLY=()
else
COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}"))
fi
elif [[ ${prev} == "-vm" || ${subcmd} =~ vm.(console|destroy|guest.tools|info|ip|markastemplate|markasvm|migrate|power|vnc) ]] ; then
#CANDIDATES=( $(compgen -W "$(govc find -type VirtualMachine -name=${cur}\* | rev | cut -d'/' -f1 | rev )" -- ${cur}) )
# -- find is too slow ( # of objects to retreive = 450 )
# govc find -type VirtualMachine : 8.5s
# govc ls /SDDC-Datacenter/vm/* : 0.45
# govc ls is also weird that it doesn't show the complete list of vm just by doing ls
# Check cache freshness
CACHE_DIR="~/govc/cache"
VM_CACHE="$CACHE_DIR/$GOVC_URL-vmcache"
if [[ $(find $VM_CACHE -mmin -1 -type f -print 2>/dev/null | wc -l) -lt 1 ]]
then
mkdir -p $CACHE_DIR
govc ls /\*/vm/\* | rev | cut -d'/' -f1 | rev | sort > $VM_CACHE
fi
CANDIDATES=( $(compgen -W "$(cat $VM_CACHE)" -- ${cur}) )
if [ ${#CANDIDATES[*]} -eq 0 ]; then
COMPREPLY=()
else
COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}"))
fi
fi
# did not hit any specifcs so show all options from help
if [[ -z ${COMPREPLY} ]]; then
COMPREPLY=( $(compgen -W "-h $(govc ${subcmd} -h | awk '{print $1}' | grep "^-" | sed -e 's/=.*//g' )" -- ${cur}) )
fi
return 0
}
complete -F _govc_complete govc