Skip to content

Commit

Permalink
plugins/proxy: use _command_exists
Browse files Browse the repository at this point in the history
Addresses Bash-it#1632

Alsö, use `_log_notice`, quote variables, handle unbound parameters, &c.
  • Loading branch information
gaelicWizard committed Sep 10, 2021
1 parent 5105d4a commit 8717b69
Showing 1 changed file with 91 additions and 75 deletions.
166 changes: 91 additions & 75 deletions plugins/available/proxy.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'Proxy Tools'

disable-proxy ()
function disable-proxy()
{
about 'Disables proxy settings for Bash, npm and SSH'
group 'proxy'
Expand All @@ -13,52 +14,52 @@ disable-proxy ()
unset ALL_PROXY
unset no_proxy
unset NO_PROXY
echo "Disabled proxy environment variables"
_log_notice "Disabled proxy environment variables"

npm-disable-proxy
ssh-disable-proxy
svn-disable-proxy
}

enable-proxy ()
function enable-proxy()
{
about 'Enables proxy settings for Bash, npm and SSH'
group 'proxy'

export http_proxy=$BASH_IT_HTTP_PROXY
export https_proxy=$BASH_IT_HTTPS_PROXY
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export ALL_PROXY=$http_proxy
export no_proxy=$BASH_IT_NO_PROXY
export NO_PROXY=$no_proxy
echo "Enabled proxy environment variables"
export http_proxy="${BASH_IT_HTTP_PROXY:-}"
export https_proxy="${BASH_IT_HTTPS_PROXY:-}"
export HTTP_PROXY="${http_proxy:-}"
export HTTPS_PROXY="${https_proxy:-}"
export ALL_PROXY="${http_proxy:-}"
export no_proxy="${BASH_IT_NO_PROXY:-}"
export NO_PROXY="${no_proxy:-}"
_log_notice "Enabled proxy environment variables"

npm-enable-proxy
ssh-enable-proxy
svn-enable-proxy
}

enable-proxy-alt ()
function enable-proxy-alt()
{
about 'Enables alternate proxy settings for Bash, npm and SSH'
group 'proxy'

export http_proxy=$BASH_IT_HTTP_PROXY_ALT
export https_proxy=$BASH_IT_HTTPS_PROXY_ALT
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export ALL_PROXY=$http_proxy
export no_proxy=$BASH_IT_NO_PROXY
export NO_PROXY=$no_proxy
echo "Enabled alternate proxy environment variables"
export http_proxy="${BASH_IT_HTTP_PROXY_ALT:-}"
export https_proxy="${BASH_IT_HTTPS_PROXY_ALT:-}"
export HTTP_PROXY="${http_proxy:-}"
export HTTPS_PROXY="${https_proxy:-}"
export ALL_PROXY="${http_proxy:-}"
export no_proxy="${BASH_IT_NO_PROXY:-}"
export NO_PROXY="${no_proxy:-}"
_log_notice "Enabled alternate proxy environment variables"

npm-enable-proxy $http_proxy $https_proxy
npm-enable-proxy "${http_proxy:-}" "${https_proxy:-}"
ssh-enable-proxy
svn-enable-proxy $http_proxy
svn-enable-proxy "${http_proxy:-}"
}

show-proxy ()
function show-proxy()
{
about 'Shows the proxy settings for Bash, Git, npm and SSH'
group 'proxy'
Expand All @@ -75,7 +76,7 @@ show-proxy ()
ssh-show-proxy
}

proxy-help ()
function proxy-help()
{
about 'Provides an overview of the bash-it proxy configuration'
group 'proxy'
Expand All @@ -97,7 +98,7 @@ EOF
bash-it-show-proxy
}

bash-it-show-proxy ()
function bash-it-show-proxy()
{
about 'Shows the bash-it proxy settings'
group 'proxy'
Expand All @@ -110,12 +111,13 @@ bash-it-show-proxy ()
env | grep -e "BASH_IT.*PROXY"
}

npm-show-proxy ()
function npm-show-proxy()
{
about 'Shows the npm proxy settings'
group 'proxy'

if $(command -v npm &> /dev/null) ; then
if _command_exists npm
then
echo ""
echo "npm"
echo "==="
Expand All @@ -125,42 +127,45 @@ npm-show-proxy ()
fi
}

npm-disable-proxy ()
function npm-disable-proxy()
{
about 'Disables npm proxy settings'
group 'proxy'

if $(command -v npm &> /dev/null) ; then
if _command_exists npm
then
npm config delete proxy
npm config delete https-proxy
npm config delete noproxy
echo "Disabled npm proxy settings"
_log_notice "Disabled npm proxy settings"
fi
}

npm-enable-proxy ()
function npm-enable-proxy()
{
about 'Enables npm proxy settings'
group 'proxy'

local my_http_proxy=${1:-$BASH_IT_HTTP_PROXY}
local my_https_proxy=${2:-$BASH_IT_HTTPS_PROXY}
local my_no_proxy=${3:-$BASH_IT_NO_PROXY}
local my_http_proxy="${1:-${BASH_IT_HTTP_PROXY:-}}"
local my_https_proxy="${2:-${BASH_IT_HTTPS_PROXY:-}}"
local my_no_proxy="${3:-${BASH_IT_NO_PROXY:-}}"

if $(command -v npm &> /dev/null) ; then
npm config set proxy $my_http_proxy
npm config set https-proxy $my_https_proxy
npm config set noproxy $my_no_proxy
echo "Enabled npm proxy settings"
if _command_exists npm
then
npm config set proxy "${my_http_proxy:?}" || return
npm config set https-proxy "${my_https_proxy:?}" || return
npm config set noproxy "${my_no_proxy:-}" || return
_log_notice "Enabled npm proxy settings"
fi
}

git-global-show-proxy ()
function git-global-show-proxy()
{
about 'Shows global Git proxy settings'
group 'proxy'

if $(command -v git &> /dev/null) ; then
if _command_exists git
then
echo ""
echo "Git (Global Settings)"
echo "====================="
Expand All @@ -169,78 +174,84 @@ git-global-show-proxy ()
fi
}

git-global-disable-proxy ()
function git-global-disable-proxy()
{
about 'Disables global Git proxy settings'
group 'proxy'

if $(command -v git &> /dev/null) ; then
if _command_exists git
then
git config --global --unset-all http.proxy
git config --global --unset-all https.proxy
echo "Disabled global Git proxy settings"
_log_notice "Disabled global Git proxy settings"
fi
}

git-global-enable-proxy ()
function git-global-enable-proxy()
{
about 'Enables global Git proxy settings'
group 'proxy'

if $(command -v git &> /dev/null) ; then
if _command_exists git
then
git-global-disable-proxy

git config --global --add http.proxy $BASH_IT_HTTP_PROXY
git config --global --add https.proxy $BASH_IT_HTTPS_PROXY
echo "Enabled global Git proxy settings"
git config --global --add http.proxy "${BASH_IT_HTTP_PROXY:?}"
git config --global --add https.proxy "${BASH_IT_HTTPS_PROXY:?}"
_log_notice "Enabled global Git proxy settings"
fi
}

git-show-proxy ()
function git-show-proxy()
{
about 'Shows current Git project proxy settings'
group 'proxy'

if $(command -v git &> /dev/null) ; then
if _command_exists git
then
echo "Git Project Proxy Settings"
echo "====================="
echo "Git HTTP proxy: " `git config --get http.proxy`
echo "Git HTTPS proxy: " `git config --get https.proxy`
fi
}

git-disable-proxy ()
function git-disable-proxy()
{
about 'Disables current Git project proxy settings'
group 'proxy'

if $(command -v git &> /dev/null) ; then
if _command_exists git
then
git config --unset-all http.proxy
git config --unset-all https.proxy
echo "Disabled Git project proxy settings"
_log_notice "Disabled Git project proxy settings"
fi
}

git-enable-proxy ()
function git-enable-proxy()
{
about 'Enables current Git project proxy settings'
group 'proxy'

if $(command -v git &> /dev/null) ; then
if _command_exists git
then
git-disable-proxy

git config --add http.proxy $BASH_IT_HTTP_PROXY
git config --add https.proxy $BASH_IT_HTTPS_PROXY
echo "Enabled Git project proxy settings"
git config --add http.proxy "${BASH_IT_HTTP_PROXY:?}"
git config --add https.proxy "${BASH_IT_HTTPS_PROXY:?}"
_log_notice "Enabled Git project proxy settings"
fi
}


svn-show-proxy ()
function svn-show-proxy()
{
about 'Shows SVN proxy settings'
group 'proxy'

if $(command -v svn &> /dev/null) && $(command -v python2 &> /dev/null) ; then
if _command_exists svn && _command_exists python2
then
echo ""
echo "SVN Proxy Settings"
echo "=================="
Expand All @@ -265,12 +276,13 @@ END
fi
}

svn-disable-proxy ()
function svn-disable-proxy()
{
about 'Disables SVN proxy settings'
group 'proxy'

if $(command -v svn &> /dev/null) && $(command -v python2 &> /dev/null) ; then
if _command_exists svn_command_exists python2
then
python2 - <<END
import ConfigParser, os
config = ConfigParser.ConfigParser()
Expand All @@ -294,15 +306,16 @@ END
fi
}

svn-enable-proxy ()
function svn-enable-proxy()
{
about 'Enables SVN proxy settings'
group 'proxy'

if $(command -v svn &> /dev/null) && $(command -v python2 &> /dev/null) ; then
local my_http_proxy=${1:-$BASH_IT_HTTP_PROXY}
if _command_exists svn _command_exists python2
then
local my_http_proxy="${1:-${BASH_IT_HTTP_PROXY:-}}"

python2 - "$my_http_proxy" "$BASH_IT_NO_PROXY" <<END
python2 - "${my_http_proxy:?}" "${BASH_IT_NO_PROXY:-}" <<END
import ConfigParser, os, sys, urlparse
pieces = urlparse.urlparse(sys.argv[1])
host = pieces.hostname
Expand Down Expand Up @@ -331,12 +344,13 @@ END
fi
}

ssh-show-proxy ()
function ssh-show-proxy()
{
about 'Shows SSH config proxy settings (from ~/.ssh/config)'
group 'proxy'

if [ -f ~/.ssh/config ] ; then
if [ -f ~/.ssh/config ]
then
echo ""
echo "SSH Config Enabled in ~/.ssh/config"
echo "==================================="
Expand Down Expand Up @@ -368,25 +382,27 @@ ssh-show-proxy ()
fi
}

ssh-disable-proxy ()
function ssh-disable-proxy()
{
about 'Disables SSH config proxy settings'
group 'proxy'

if [ -f ~/.ssh/config ] ; then
if [ -f ~/.ssh/config ]
then
sed -e's/^.*ProxyCommand/# ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
echo "Disabled SSH config proxy settings"
_log_notice "Disabled SSH config proxy settings"
fi
}


ssh-enable-proxy ()
function ssh-enable-proxy()
{
about 'Enables SSH config proxy settings'
group 'proxy'

if [ -f ~/.ssh/config ] ; then
if [ -f ~/.ssh/config ]
then
sed -e's/# ProxyCommand/ ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
echo "Enabled SSH config proxy settings"
_log_notice "Enabled SSH config proxy settings"
fi
}

0 comments on commit 8717b69

Please sign in to comment.