github.com/vmware/govmomi@v0.43.0/scripts/govc_bash_completion (about) 1 #!/bin/bash 2 3 # govc Bash completion script 4 # place in etc/bash_completion.d/ or source on command line with "." 5 6 _govc_complete() 7 { 8 local IFS=$'\n' 9 local cur prev subcmd 10 prev=${COMP_WORDS[COMP_CWORD-1]} 11 cur=${COMP_WORDS[COMP_CWORD]} 12 subcmd=${COMP_WORDS[1]} 13 COMPREPLY=() 14 15 if [[ ${prev} == "govc" ]] ; then # show subcommands, no options 16 COMPREPLY=( $(compgen -W "$(govc -h | grep -v Usage | awk '{print $1}' )" -- ${cur}) ) 17 return 0 18 elif [[ ${cur} == "-"* ]] ; then 19 : # drop out and show options 20 elif [[ ${subcmd} == "ls" ]] ; then # not completing an option, try for appropriate values 21 if [[ ${prev} == "-t" ]] ; then 22 COMPREPLY=( $(compgen -W "$(govc ls -l "/**" | awk '{print $2}' | sort -u | tr -d '()' )" -- ${cur}) ) 23 else 24 COMPREPLY=( $(compgen -W "$(govc ls /\*\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) ) 25 fi 26 elif [[ ${prev} == "-net" ]] ; then 27 CANDIDATES=( $(compgen -W "$(govc ls /\*/network/\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) ) 28 if [ ${#CANDIDATES[*]} -eq 0 ]; then 29 COMPREPLY=() 30 else 31 COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}")) 32 fi 33 elif [[ ${prev} == "-host" ]] ; then 34 COMPREPLY=( $(compgen -W "$(govc ls /\*/host/\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) ) 35 elif [[ ${prev} == "-ds" ]] ; then 36 CANDIDATES=( $(compgen -W "$(govc ls /\*/datastore/\* | rev | cut -d'/' -f1 | rev | sort | uniq)" -- ${cur}) ) 37 if [ ${#CANDIDATES[*]} -eq 0 ]; then 38 COMPREPLY=() 39 else 40 COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}")) 41 fi 42 elif [[ ${prev} == "-pool" ]] ; then 43 CANDIDATES=( $(compgen -W "$(govc find -type p)" -- ${cur}) ) 44 if [ ${#CANDIDATES[*]} -eq 0 ]; then 45 COMPREPLY=() 46 else 47 COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}")) 48 fi 49 elif [[ ${subcmd} == "vm.network.change" ]] ; then 50 # Example of sub command 51 #govc vm.network.change -vm linuxserver1 -net Pvt_1289_Server ethernet-0 52 # Get device 53 local i=1 54 local vm="" 55 while [[ "$i" -lt "$COMP_CWORD" ]]; do 56 if [[ ${COMP_WORDS[i]} == "-vm" ]]; then 57 local vm=${COMP_WORDS[((i+1))]} 58 fi 59 (( i++ )) 60 done 61 COMPREPLY=( $(compgen -W "$(govc device.ls -vm $vm eth* | awk '{print $1}')" -- ${cur})) 62 elif [[ ${prev} == "-disk.label" ]] ; then 63 local i=1 64 local vm="" 65 while [[ "$i" -lt "$COMP_CWORD" ]]; do 66 if [[ ${COMP_WORDS[i]} == "-vm" ]]; then 67 local vm=${COMP_WORDS[((i+1))]} 68 fi 69 (( i++ )) 70 done 71 72 CANDIDATES=( $(compgen -W "$(govc device.info -json -vm $vm disk-* | jq '.devices[].deviceInfo.label' | sed 's/"//g')" -- ${cur})) 73 if [ ${#CANDIDATES[*]} -eq 0 ]; then 74 COMPREPLY=() 75 else 76 COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}")) 77 fi 78 elif [[ ${prev} == "-vm" || ${subcmd} =~ vm.(console|destroy|guest.tools|info|ip|markastemplate|markasvm|migrate|power|vnc) ]] ; then 79 #CANDIDATES=( $(compgen -W "$(govc find -type VirtualMachine -name=${cur}\* | rev | cut -d'/' -f1 | rev )" -- ${cur}) ) 80 # -- find is too slow ( # of objects to retreive = 450 ) 81 # govc find -type VirtualMachine : 8.5s 82 # govc ls /SDDC-Datacenter/vm/* : 0.45 83 # govc ls is also weird that it doesn't show the complete list of vm just by doing ls 84 85 # Check cache freshness 86 CACHE_DIR="~/govc/cache" 87 VM_CACHE="$CACHE_DIR/$GOVC_URL-vmcache" 88 if [[ $(find $VM_CACHE -mmin -1 -type f -print 2>/dev/null | wc -l) -lt 1 ]] 89 then 90 mkdir -p $CACHE_DIR 91 govc ls /\*/vm/\* | rev | cut -d'/' -f1 | rev | sort > $VM_CACHE 92 fi 93 CANDIDATES=( $(compgen -W "$(cat $VM_CACHE)" -- ${cur}) ) 94 if [ ${#CANDIDATES[*]} -eq 0 ]; then 95 COMPREPLY=() 96 else 97 COMPREPLY=($(printf '%q\n' "${CANDIDATES[@]}")) 98 fi 99 fi 100 # did not hit any specifcs so show all options from help 101 if [[ -z ${COMPREPLY} ]]; then 102 COMPREPLY=( $(compgen -W "-h $(govc ${subcmd} -h | awk '{print $1}' | grep "^-" | sed -e 's/=.*//g' )" -- ${cur}) ) 103 fi 104 105 return 0 106 } 107 complete -F _govc_complete govc