-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51e1f9f
commit 1165875
Showing
3 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# PVE-BULK Bash Completion Script | ||
|
||
## Intro | ||
|
||
This script contains the definitions that enable bash-completion for pve-bulk command. It can complete pve-bulk | ||
actions and options and it can also suggest some of the parameters after the --vm-list and --ct-list options. | ||
|
||
## Environment Variables for VMs' and CTs' lists | ||
|
||
You can define some specific environment variables with a list of IDs (of VMs or CTs) which can be used by the bash | ||
completion script to be suggested after --vm-list and --ct-list options: | ||
|
||
- PVB_CT_* variables are used for completion after --ct-list option | ||
- PVB_VM_* variables are used for completion after --vm-list option | ||
|
||
For example, you can place within the ~/.bashrc of the user running pve-bulk the following variables and then restart | ||
the shell or execute *source ~/.bashrc*: | ||
|
||
```bash | ||
export PVB_CT_TEST="101,102" | ||
export PVB_CT_PROD="103,104" | ||
export PVB_VM_DEVEL="1001,1011,1012,1013,1021,1022" | ||
export PVB_VM_TEST="1211,1212,1213" | ||
``` | ||
|
||
These variables allow you to call pve-bulk with a command like this and will be used by bash-completion: | ||
|
||
```bash | ||
pve-bulk listsnapshot --vm-list=$PVB_VM_DEVEL | ||
``` | ||
|
||
## Testing pve-bulk completion script | ||
|
||
You can test pve-bulk bash completion by loading it with source: | ||
|
||
```bash | ||
source pve-bulk-completion.bash | ||
``` | ||
|
||
## Installing pve-bulk permanently for all users | ||
|
||
You can install pve-bulk along with the other bash-completion scripts with the following command: | ||
|
||
```bash | ||
sudo install -m 0644 -o root -g root pve-bulk-completion.bash /etc/bash_completion.d/ | ||
``` | ||
|
||
When you'll start a new shell pve-bulk completion script will be active. | ||
|
||
## Uninstalling pve-bulk | ||
|
||
It is sufficient to remove it from the global bash-completion folder and then restart the shell: | ||
|
||
```bash | ||
rm -f /etc/bash_completion.d/pve-bulk-completion.bash -i | ||
rm: remove regular file '/etc/bash_completion.d/pve-bulk-completion.bash'? y | ||
removed '/etc/bash_completion.d/pve-bulk-completion.bash' | ||
``` | ||
|
||
## Completion examples | ||
|
||
Follow some examples of how pressing [TAB] (double [TAB] if there are multiple options) triggers pve-bulk bash completion: | ||
|
||
```bash | ||
$ pve-bulk [TAB][TAB] | ||
delsnapshot help rollback snapshot status | ||
--help listsnapshot shutdown start stop | ||
|
||
$ pve-bulk st[TAB][TAB] | ||
start status stop | ||
|
||
# The following expands the command on the same line | ||
$ pve-bulk sn[TAB][TAB] | ||
# producing the following | ||
$ pve-bulk snapshot | ||
|
||
# The following triggers a suggestion to specify a snapshot name, you can delete it and enter the desired name | ||
$ pve-bulk snapshot [TAB] | ||
$ pve-bulk snapshot SNAP_NAME | ||
$ pve-bulk snapshot my-snapshot --[TAB][TAB] | ||
--all --ct-list= --running --stopped --vm-list= | ||
|
||
# If we specify --vm-list then the completion scripts looks for environment variables starting with PVB_VM_ | ||
$ pve-bulk snapshot my-snapshot --vm-list=[TAB][TAB] | ||
$PVB_VM_CEPH $PVB_VM_DEVEL running stopped | ||
$ pve-bulk snapshot my-snapshot --vm-list=$P[TAB] | ||
$ pve-bulk snapshot my-snapshot --vm-list=$PVB_VM_[TAB][TAB] | ||
$PVB_VM_CEPH $PVB_VM_DEVEL | ||
$ pve-bulk snapshot my-snapshot --vm-list=$PVB_VM_D[TAB] | ||
$ pve-bulk snapshot my-snapshot --vm-list=$PVB_VM_DEVEL | ||
|
||
# We can press [TAB] after --vm-list=xxx the completion immediately completes with --ct-list which is the only other | ||
# valid option after --vm-list. It works vice versa too. | ||
$ pve-bulk snapshot my-snapshot --vm-list=$PVB_VM_DEVEL [TAB] | ||
$ pve-bulk snapshot my-snapshot --vm-list=$PVB_VM_DEVEL --ct-list=[TAB][TAB] | ||
$PVB_CT_PROD $PVB_CT_TEST running stopped | ||
$ pve-bulk snapshot my-snapshot --vm-list=$PVB_VM_DEVEL --ct-list=r[TAB] | ||
$ pve-bulk snapshot my-snapshot --vm-list=$PVB_VM_DEVEL --ct-list=running | ||
|
||
$ pve-bulk listsnapshot --[TAB][TAB] | ||
--all --ct-list= --running --stopped --vm-list= | ||
$ pve-bulk listsnapshot --a[TAB] | ||
$ pve-bulk listsnapshot --all | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#/usr/bin/env bash -x | ||
|
||
####################################### | ||
# Bash Completion script for pve-bulk # | ||
# # | ||
# Version: 1.0 # | ||
####################################### | ||
|
||
PVE_BULK_ACTIONS="help start shutdown stop status listsnapshot snapshot rollback delsnapshot --help" | ||
PVE_BULK_OPTIONS="--ct-list= --vm-list= --all --stopped --running" | ||
|
||
_pve_bulk_completion() | ||
{ | ||
# Retrieve environment variables with lists of VMs and CTs | ||
PVE_BULK_VM_VARS=$(env | grep PVB_VM_ | cut -d= -f 1) | ||
PVE_BULK_CT_VARS=$(env | grep PVB_CT_ | cut -d= -f 1) | ||
|
||
local suggestions="" | ||
|
||
if [ "${#COMP_WORDS[@]}" == "2" ]; then | ||
# Complete actions at the beginning of the command | ||
suggestions=($(compgen -W "${PVE_BULK_ACTIONS}" -- "${COMP_WORDS[1]}" )) | ||
elif [ "${#COMP_WORDS[@]}" -ge "2" ]; then | ||
# Complete options for specific actions | ||
ACTION_TYPE="STOP_COMPLETION" | ||
case ${COMP_WORDS[1]} in | ||
"snapshot" | "rollback" | "delsnapshot") | ||
ACTION_TYPE="NEED_SNAPSHOT" | ||
;; | ||
"start" | "shutdown" | "stop" | "status" | "listsnapshot") | ||
ACTION_TYPE="NO_SNAPSHOT" | ||
;; | ||
esac | ||
|
||
if [ ${ACTION_TYPE} == "STOP_COMPLETION" ] ; then | ||
return | ||
else | ||
if [ ${ACTION_TYPE} == "NEED_SNAPSHOT" ] && [ "${#COMP_WORDS[@]}" == "3" ] && [ -z ${COMP_WORDS[2]} ] ; then | ||
# For snapshot, rollback and delsnapshot actions give users an hint about the need to specify the name of the snapshot | ||
suggestions="SNAP-NAME" | ||
else | ||
if [ ${ACTION_TYPE} == "NEED_SNAPSHOT" ] ; then | ||
# If action is snapshot, rollback or delsnapshot, pop the first element of the COMP_WORDS array in order to | ||
# have the same processing as listsnapshot and similar options without the need to enter a snapshot name | ||
COMP_WORDS=("${COMP_WORDS[@]:1}") | ||
fi | ||
|
||
if [ "${#COMP_WORDS[@]}" == "3" ] ; then | ||
# Suggest all the options if this is the first option after the action | ||
suggestions=($(compgen -W "${PVE_BULK_OPTIONS}" -- "${COMP_WORDS[2]}")) | ||
elif [ "${#COMP_WORDS[@]}" == "6" ] ; then | ||
# If this is the second option specified, suggest --vm-list if first option is --ct-list and vice versa | ||
if [ ${COMP_WORDS[2]} == "--vm-list" ] ; then | ||
suggestions=($(compgen -W "--ct-list=" -- "${COMP_WORDS[5]}")) | ||
elif [ ${COMP_WORDS[2]} == "--ct-list" ] ; then | ||
suggestions=($(compgen -W "--vm-list=" -- "${COMP_WORDS[5]}")) | ||
else | ||
return | ||
fi | ||
else | ||
################################# | ||
# Completions for active option # | ||
################################# | ||
|
||
for option in "--vm-list" "--ct-list" | ||
do | ||
#echo "option=<$option>" | ||
|
||
VARS="$PVE_BULK_VM_VARS" | ||
if [ ${option} == "--ct-list" ] ; then | ||
VARS="$PVE_BULK_CT_VARS" | ||
fi | ||
|
||
# If we have --XX-list= without any character after "=", return the available $PVE_BULK_XX_* vars, if available, as suggestions, along with running and stopped keywords | ||
if [ "${#COMP_WORDS[@]}" == "4" ] && [ ${COMP_WORDS[2]} == "${option}" ] && [ ${COMP_WORDS[3]} == "=" ] ; then | ||
suggestions=($(compgen -W "${VARS}" -P '$' ; compgen -W "running stopped")) | ||
elif [ "${#COMP_WORDS[@]}" == "7" ] && [ ${COMP_WORDS[5]} == "${option}" ] && [ ${COMP_WORDS[6]} == "=" ] ; then | ||
suggestions=($(compgen -W "${VARS}" -P '$' ; compgen -W "running stopped")) | ||
# If we have --vm-list=XXX, return the available $PVE_BULK_VM_* vars, if available, or the keyword running or stopped that have XXX as prefix | ||
elif [ "${#COMP_WORDS[@]}" == "5" ] && [ ${COMP_WORDS[2]} == "${option}" ] ; then | ||
suggestions=($(compgen -W "${VARS}" -P '$' -- "${COMP_WORDS[4]##$}" ; compgen -W "running stopped" -- "${COMP_WORDS[4]##$}")) | ||
elif [ "${#COMP_WORDS[@]}" == "8" ] && [ ${COMP_WORDS[5]} == "${option}" ] ; then | ||
suggestions=($(compgen -W "${VARS}" -P '$' -- "${COMP_WORDS[7]##$}" ; compgen -W "running stopped" -- "${COMP_WORDS[7]##$}")) | ||
fi | ||
done | ||
fi | ||
fi | ||
|
||
if [ -z $suggestions ] ; then | ||
return | ||
fi | ||
fi | ||
fi | ||
|
||
COMPREPLY=("${suggestions[@]}") | ||
} | ||
|
||
complete -o nospace -F _pve_bulk_completion pve-bulk |