From a584c6e71f0c4000e0d233dc5fdff5a6e82a72e5 Mon Sep 17 00:00:00 2001 From: Oleksandr Gavenko Date: Thu, 26 Oct 2017 17:27:56 +0300 Subject: [PATCH 1/2] Prevent parsing errors due to ANSI control sequences emitted by default. At least "vagrant global-status | od -c" produces: \n 033 [ 0 m - - before delimiter ----...-- ID so regex /^-+$/ is failed. There is --no-color flag but I am not sure about backward compatibility. So ended with VAGRANT_NO_COLOR env var which is the most unintrusive way to introduce fix. https://www.vagrantup.com/docs/other/environmental-variables.html --- .../etc/bash_completion.d/vagrant | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/vagrant-bash-completion/etc/bash_completion.d/vagrant b/vagrant-bash-completion/etc/bash_completion.d/vagrant index 92901a4..65fdab0 100644 --- a/vagrant-bash-completion/etc/bash_completion.d/vagrant +++ b/vagrant-bash-completion/etc/bash_completion.d/vagrant @@ -93,7 +93,7 @@ __vagrant_get_commands(){ local vagrant_commands='' vagrant_output='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant list-commands 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant list-commands 2>/dev/null) || return 1 # `NF != 0` is there solely to insure against any errata appended to list-commands output at a later date vagrant_commands=$(awk '/^$/ {while (getline && NF != 0) print $1}' <<<"$vagrant_output" | sort) @@ -108,7 +108,7 @@ __vagrant_get_environments(){ local vagrant_environments='' vagrant_output='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant global-status 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant global-status 2>/dev/null) || return 1 vagrant_environments=$(awk '/^-+$/ {while (getline && NF != 0) print $1}' <<<"$vagrant_output") @@ -139,7 +139,7 @@ __vagrant_get_not_off_global(){ local not_off='' vagrant_output='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant global-status 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant global-status 2>/dev/null) || return 1 not_off=$(awk '/^-+$/ {while (getline && NF != 0) if ($4 != "poweroff") print $1}' <<<"$vagrant_output") @@ -153,7 +153,7 @@ __vagrant_get_not_off_local(){ local not_off='' vagrant_output='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant status 2>/dev/null) || return 100 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant status 2>/dev/null) || return 100 not_off=$(awk '/^$/ {while (getline && NF != 0) if ($2 != "poweroff") print $1}' <<<"$vagrant_output") @@ -184,7 +184,7 @@ __vagrant_get_not_running_global(){ local not_running='' vagrant_output='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant global-status 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant global-status 2>/dev/null) || return 1 not_running=$(awk '/^-+$/ {while (getline && NF != 0) if ($4 != "running") print $1}' <<<"$vagrant_output") @@ -199,7 +199,7 @@ __vagrant_get_not_running_local(){ local not_running='' vagrant_output='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant status 2>/dev/null) || return 100 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant status 2>/dev/null) || return 100 not_running=$(awk '/^$/ {while (getline && NF != 0) if ($2 != "running") print $1}' <<<"$vagrant_output") @@ -224,7 +224,7 @@ __vagrant_get_plugins(){ local vagrant_plugins='' - vagrant_plugins=$(vagrant plugin list 2>/dev/null | awk '{print $1}') + vagrant_plugins=$(VAGRANT_NO_COLOR=1 vagrant plugin list 2>/dev/null | awk '{print $1}') [[ -z $vagrant_plugins ]] && return 1 @@ -236,7 +236,7 @@ __vagrant_get_snapshots(){ local vagrant_output='' vagrant_snapshots='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant snapshot list 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant snapshot list 2>/dev/null) || return 1 vagrant_snapshots=$(awk '$1 ~ /Name:/ {print $2}' <<<"$vagrant_output") @@ -250,7 +250,7 @@ __vagrant_get_subcommands(){ local vagrant_output='' vagrant_subcommands='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant help "$1" 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant help "$1" 2>/dev/null) || return 1 vagrant_subcommands=$(awk '/^Available subcommands:$/ {while (getline && NF == 1) print $1}' <<<"$vagrant_output") # vagrant_subcommands=$(awk '/^ [a-z]+$/ {print $1}' <<<"$vagrant_output") @@ -265,7 +265,7 @@ __vagrant_get_versions(){ local vagrant_output='' vagrant_versions='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant box list 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant box list 2>/dev/null) || return 1 vagrant_versions=$(awk '{sub(")$", "", $NF); print $NF}' <<<"$vagrant_output") @@ -279,7 +279,7 @@ __vagrant_get_vms(){ local vagrant_output='' vagrant_vms='' # local must be declared beforehand in order to return an accurate exit status here - vagrant_output=$(vagrant status 2>/dev/null) || return 1 + vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant status 2>/dev/null) || return 1 vagrant_vms=$(awk '/^$/ {while (getline && NF != 0) print $1}' <<<"$vagrant_output") From 682ec008b773c99c19507cd9349203c1eec47340 Mon Sep 17 00:00:00 2001 From: Oleksandr Gavenko Date: Thu, 26 Oct 2017 18:15:18 +0300 Subject: [PATCH 2/2] Prevent parsing errors due to trailing \r (CR) sign when completion works within Cygwin's Bash with native Windows build of Vagrant. # vagrant help | od -c \r \n \r \n - v , - - v e r s i o n It is possible to strip \r by piping through 'tr -d $"\n"' at cost of extra process but completion is already slow on Windows so it's better to handle CR in awk at cost of readability. Only __vagrant_get_snapshots wasn't updated as I found it difficult to test. --- .../etc/bash_completion.d/vagrant | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/vagrant-bash-completion/etc/bash_completion.d/vagrant b/vagrant-bash-completion/etc/bash_completion.d/vagrant index 65fdab0..b394842 100644 --- a/vagrant-bash-completion/etc/bash_completion.d/vagrant +++ b/vagrant-bash-completion/etc/bash_completion.d/vagrant @@ -96,7 +96,7 @@ __vagrant_get_commands(){ vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant list-commands 2>/dev/null) || return 1 # `NF != 0` is there solely to insure against any errata appended to list-commands output at a later date - vagrant_commands=$(awk '/^$/ {while (getline && NF != 0) print $1}' <<<"$vagrant_output" | sort) + vagrant_commands=$(awk '/^\r?$/ {while (getline && NF > 1) print $1}' <<<"$vagrant_output" | sort) [[ -z $vagrant_commands ]] && return 1 @@ -110,7 +110,7 @@ __vagrant_get_environments(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant global-status 2>/dev/null) || return 1 - vagrant_environments=$(awk '/^-+$/ {while (getline && NF != 0) print $1}' <<<"$vagrant_output") + vagrant_environments=$(awk '/^-+\r?$/ {while (getline && NF > 1) print $1}' <<<"$vagrant_output") [[ -z $vagrant_environments ]] && return 1 @@ -141,7 +141,7 @@ __vagrant_get_not_off_global(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant global-status 2>/dev/null) || return 1 - not_off=$(awk '/^-+$/ {while (getline && NF != 0) if ($4 != "poweroff") print $1}' <<<"$vagrant_output") + not_off=$(awk '/^-+\r?$/ {while (getline && NF > 1) if ($4 != "poweroff") print $1}' <<<"$vagrant_output") [[ -z $not_off ]] && return 1 @@ -155,7 +155,7 @@ __vagrant_get_not_off_local(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant status 2>/dev/null) || return 100 - not_off=$(awk '/^$/ {while (getline && NF != 0) if ($2 != "poweroff") print $1}' <<<"$vagrant_output") + not_off=$(awk '/^\r?$/ {while (getline && NF > 1) if ($2 != "poweroff") print $1}' <<<"$vagrant_output") [[ -z $not_off ]] && return 1 @@ -186,7 +186,7 @@ __vagrant_get_not_running_global(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant global-status 2>/dev/null) || return 1 - not_running=$(awk '/^-+$/ {while (getline && NF != 0) if ($4 != "running") print $1}' <<<"$vagrant_output") + not_running=$(awk '/^-+\r?$/ {while (getline && NF > 1) if ($4 != "running") print $1}' <<<"$vagrant_output") [[ -z $not_running ]] && return 1 @@ -201,7 +201,7 @@ __vagrant_get_not_running_local(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant status 2>/dev/null) || return 100 - not_running=$(awk '/^$/ {while (getline && NF != 0) if ($2 != "running") print $1}' <<<"$vagrant_output") + not_running=$(awk '/^\r?$/ {while (getline && NF > 1) if ($2 != "running") print $1}' <<<"$vagrant_output") [[ -z $not_running ]] && return 1 @@ -252,7 +252,7 @@ __vagrant_get_subcommands(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant help "$1" 2>/dev/null) || return 1 - vagrant_subcommands=$(awk '/^Available subcommands:$/ {while (getline && NF == 1) print $1}' <<<"$vagrant_output") + vagrant_subcommands=$(awk '/^Available subcommands:\r?$/ {while (getline && length($1) > 1) print $1}' <<<"$vagrant_output") # vagrant_subcommands=$(awk '/^ [a-z]+$/ {print $1}' <<<"$vagrant_output") [[ -z $vagrant_subcommands ]] && return 1 @@ -267,7 +267,7 @@ __vagrant_get_versions(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant box list 2>/dev/null) || return 1 - vagrant_versions=$(awk '{sub(")$", "", $NF); print $NF}' <<<"$vagrant_output") + vagrant_versions=$(awk '{sub(")\r?$", "", $NF); print $NF}' <<<"$vagrant_output") [[ -z $vagrant_versions ]] && return 1 @@ -281,7 +281,7 @@ __vagrant_get_vms(){ # local must be declared beforehand in order to return an accurate exit status here vagrant_output=$(VAGRANT_NO_COLOR=1 vagrant status 2>/dev/null) || return 1 - vagrant_vms=$(awk '/^$/ {while (getline && NF != 0) print $1}' <<<"$vagrant_output") + vagrant_vms=$(awk '/^\r?$/ {while (getline && NF > 1) print $1}' <<<"$vagrant_output") [[ -z $vagrant_vms ]] && return 1