Skip to content

Commit 6e480d7

Browse files
committed
plugin/proxy: shellcheck && shfmt
plugins/proxy: use `_command_exists()` Alsö, quote variables, handle unbound parameters, &c.
1 parent 86c1e3c commit 6e480d7

File tree

2 files changed

+77
-99
lines changed

2 files changed

+77
-99
lines changed

clean_files.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ plugins/available/osx-timemachine.plugin.bash
114114
plugins/available/osx.plugin.bash
115115
plugins/available/percol.plugin.bash
116116
plugins/available/plenv.plugin.bash
117+
plugins/available/proxy.plugin.bash
117118
plugins/available/pyenv.plugin.bash
118119
plugins/available/python.plugin.bash
119120
plugins/available/rbenv.plugin.bash

plugins/available/proxy.plugin.bash

Lines changed: 76 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
cite about-plugin
1+
# shellcheck shell=bash
22
about-plugin 'Proxy Tools'
33

4-
disable-proxy ()
5-
{
4+
function disable-proxy() {
65
about 'Disables proxy settings for Bash, npm and SSH'
76
group 'proxy'
87

@@ -20,46 +19,43 @@ disable-proxy ()
2019
svn-disable-proxy
2120
}
2221

23-
enable-proxy ()
24-
{
22+
function enable-proxy() {
2523
about 'Enables proxy settings for Bash, npm and SSH'
2624
group 'proxy'
2725

28-
export http_proxy=$BASH_IT_HTTP_PROXY
29-
export https_proxy=$BASH_IT_HTTPS_PROXY
30-
export HTTP_PROXY=$http_proxy
31-
export HTTPS_PROXY=$https_proxy
32-
export ALL_PROXY=$http_proxy
33-
export no_proxy=$BASH_IT_NO_PROXY
34-
export NO_PROXY=$no_proxy
26+
export http_proxy="${BASH_IT_HTTP_PROXY:-}"
27+
export https_proxy="${BASH_IT_HTTPS_PROXY:-}"
28+
export HTTP_PROXY="${http_proxy:-}"
29+
export HTTPS_PROXY="${https_proxy:-}"
30+
export ALL_PROXY="${http_proxy:-}"
31+
export no_proxy="${BASH_IT_NO_PROXY:-}"
32+
export NO_PROXY="${no_proxy:-}"
3533
echo "Enabled proxy environment variables"
3634

3735
npm-enable-proxy
3836
ssh-enable-proxy
3937
svn-enable-proxy
4038
}
4139

42-
enable-proxy-alt ()
43-
{
40+
function enable-proxy-alt() {
4441
about 'Enables alternate proxy settings for Bash, npm and SSH'
4542
group 'proxy'
4643

47-
export http_proxy=$BASH_IT_HTTP_PROXY_ALT
48-
export https_proxy=$BASH_IT_HTTPS_PROXY_ALT
49-
export HTTP_PROXY=$http_proxy
50-
export HTTPS_PROXY=$https_proxy
51-
export ALL_PROXY=$http_proxy
52-
export no_proxy=$BASH_IT_NO_PROXY
53-
export NO_PROXY=$no_proxy
44+
export http_proxy="${BASH_IT_HTTP_PROXY_ALT:-}"
45+
export https_proxy="${BASH_IT_HTTPS_PROXY_ALT:-}"
46+
export HTTP_PROXY="${http_proxy:-}"
47+
export HTTPS_PROXY="${https_proxy:-}"
48+
export ALL_PROXY="${http_proxy:-}"
49+
export no_proxy="${BASH_IT_NO_PROXY:-}"
50+
export NO_PROXY="${no_proxy:-}"
5451
echo "Enabled alternate proxy environment variables"
5552

56-
npm-enable-proxy $http_proxy $https_proxy
53+
npm-enable-proxy "${http_proxy:-}" "${https_proxy:-}"
5754
ssh-enable-proxy
58-
svn-enable-proxy $http_proxy
55+
svn-enable-proxy "${http_proxy:-}"
5956
}
6057

61-
show-proxy ()
62-
{
58+
function show-proxy() {
6359
about 'Shows the proxy settings for Bash, Git, npm and SSH'
6460
group 'proxy'
6561

@@ -75,8 +71,7 @@ show-proxy ()
7571
ssh-show-proxy
7672
}
7773

78-
proxy-help ()
79-
{
74+
function proxy-help() {
8075
about 'Provides an overview of the bash-it proxy configuration'
8176
group 'proxy'
8277

@@ -97,8 +92,7 @@ EOF
9792
bash-it-show-proxy
9893
}
9994

100-
bash-it-show-proxy ()
101-
{
95+
function bash-it-show-proxy() {
10296
about 'Shows the bash-it proxy settings'
10397
group 'proxy'
10498

@@ -110,141 +104,130 @@ bash-it-show-proxy ()
110104
env | grep -e "BASH_IT.*PROXY"
111105
}
112106

113-
npm-show-proxy ()
114-
{
107+
function npm-show-proxy() {
115108
about 'Shows the npm proxy settings'
116109
group 'proxy'
117110

118-
if $(command -v npm &> /dev/null) ; then
111+
if _command_exists npm; then
119112
echo ""
120113
echo "npm"
121114
echo "==="
122-
echo "npm HTTP proxy: " `npm config get proxy`
123-
echo "npm HTTPS proxy: " `npm config get https-proxy`
124-
echo "npm proxy exceptions: " `npm config get noproxy`
115+
echo "npm HTTP proxy: $(npm config get proxy)"
116+
echo "npm HTTPS proxy: $(npm config get https-proxy)"
117+
echo "npm proxy exceptions: $(npm config get noproxy)"
125118
fi
126119
}
127120

128-
npm-disable-proxy ()
129-
{
121+
function npm-disable-proxy() {
130122
about 'Disables npm proxy settings'
131123
group 'proxy'
132124

133-
if $(command -v npm &> /dev/null) ; then
125+
if _command_exists npm; then
134126
npm config delete proxy
135127
npm config delete https-proxy
136128
npm config delete noproxy
137129
echo "Disabled npm proxy settings"
138130
fi
139131
}
140132

141-
npm-enable-proxy ()
142-
{
133+
function npm-enable-proxy() {
143134
about 'Enables npm proxy settings'
144135
group 'proxy'
145136

146-
local my_http_proxy=${1:-$BASH_IT_HTTP_PROXY}
147-
local my_https_proxy=${2:-$BASH_IT_HTTPS_PROXY}
148-
local my_no_proxy=${3:-$BASH_IT_NO_PROXY}
137+
local my_http_proxy="${1:-${BASH_IT_HTTP_PROXY:-}}"
138+
local my_https_proxy="${2:-${BASH_IT_HTTPS_PROXY:-}}"
139+
local my_no_proxy="${3:-${BASH_IT_NO_PROXY:-}}"
149140

150-
if $(command -v npm &> /dev/null) ; then
151-
npm config set proxy $my_http_proxy
152-
npm config set https-proxy $my_https_proxy
153-
npm config set noproxy $my_no_proxy
141+
if _command_exists npm; then
142+
npm config set proxy "${my_http_proxy:?}" || return
143+
npm config set https-proxy "${my_https_proxy:?}" || return
144+
npm config set noproxy "${my_no_proxy:-}" || return
154145
echo "Enabled npm proxy settings"
155146
fi
156147
}
157148

158-
git-global-show-proxy ()
159-
{
149+
function git-global-show-proxy() {
160150
about 'Shows global Git proxy settings'
161151
group 'proxy'
162152

163-
if $(command -v git &> /dev/null) ; then
153+
if _command_exists git; then
164154
echo ""
165155
echo "Git (Global Settings)"
166156
echo "====================="
167-
echo "Git (Global) HTTP proxy: " `git config --global --get http.proxy`
168-
echo "Git (Global) HTTPS proxy: " `git config --global --get https.proxy`
157+
echo "Git (Global) HTTP proxy: $(git config --global --get http.proxy)"
158+
echo "Git (Global) HTTPS proxy: $(git config --global --get https.proxy)"
169159
fi
170160
}
171161

172-
git-global-disable-proxy ()
173-
{
162+
function git-global-disable-proxy() {
174163
about 'Disables global Git proxy settings'
175164
group 'proxy'
176165

177-
if $(command -v git &> /dev/null) ; then
166+
if _command_exists git; then
178167
git config --global --unset-all http.proxy
179168
git config --global --unset-all https.proxy
180169
echo "Disabled global Git proxy settings"
181170
fi
182171
}
183172

184-
git-global-enable-proxy ()
185-
{
173+
function git-global-enable-proxy() {
186174
about 'Enables global Git proxy settings'
187175
group 'proxy'
188176

189-
if $(command -v git &> /dev/null) ; then
177+
if _command_exists git; then
190178
git-global-disable-proxy
191179

192-
git config --global --add http.proxy $BASH_IT_HTTP_PROXY
193-
git config --global --add https.proxy $BASH_IT_HTTPS_PROXY
180+
git config --global --add http.proxy "${BASH_IT_HTTP_PROXY:?}"
181+
git config --global --add https.proxy "${BASH_IT_HTTPS_PROXY:?}"
194182
echo "Enabled global Git proxy settings"
195183
fi
196184
}
197185

198-
git-show-proxy ()
199-
{
186+
function git-show-proxy() {
200187
about 'Shows current Git project proxy settings'
201188
group 'proxy'
202189

203-
if $(command -v git &> /dev/null) ; then
190+
if _command_exists git; then
204191
echo "Git Project Proxy Settings"
205192
echo "====================="
206-
echo "Git HTTP proxy: " `git config --get http.proxy`
207-
echo "Git HTTPS proxy: " `git config --get https.proxy`
193+
echo "Git HTTP proxy: $(git config --get http.proxy)"
194+
echo "Git HTTPS proxy: $(git config --get https.proxy)"
208195
fi
209196
}
210197

211-
git-disable-proxy ()
212-
{
198+
function git-disable-proxy() {
213199
about 'Disables current Git project proxy settings'
214200
group 'proxy'
215201

216-
if $(command -v git &> /dev/null) ; then
202+
if _command_exists git; then
217203
git config --unset-all http.proxy
218204
git config --unset-all https.proxy
219205
echo "Disabled Git project proxy settings"
220206
fi
221207
}
222208

223-
git-enable-proxy ()
224-
{
209+
function git-enable-proxy() {
225210
about 'Enables current Git project proxy settings'
226211
group 'proxy'
227212

228-
if $(command -v git &> /dev/null) ; then
213+
if _command_exists git; then
229214
git-disable-proxy
230215

231-
git config --add http.proxy $BASH_IT_HTTP_PROXY
232-
git config --add https.proxy $BASH_IT_HTTPS_PROXY
216+
git config --add http.proxy "${BASH_IT_HTTP_PROXY:?}"
217+
git config --add https.proxy "${BASH_IT_HTTPS_PROXY:?}"
233218
echo "Enabled Git project proxy settings"
234219
fi
235220
}
236221

237-
238-
svn-show-proxy ()
239-
{
222+
function svn-show-proxy() {
240223
about 'Shows SVN proxy settings'
241224
group 'proxy'
242225

243-
if $(command -v svn &> /dev/null) && $(command -v python2 &> /dev/null) ; then
226+
if _command_exists svn && _command_exists python2; then
244227
echo ""
245228
echo "SVN Proxy Settings"
246229
echo "=================="
247-
python2 - <<END
230+
python2 - << END
248231
import ConfigParser, os
249232
config = ConfigParser.ConfigParser()
250233
config.read(os.path.expanduser('~/.subversion/servers'))
@@ -265,13 +248,12 @@ END
265248
fi
266249
}
267250

268-
svn-disable-proxy ()
269-
{
251+
function svn-disable-proxy() {
270252
about 'Disables SVN proxy settings'
271253
group 'proxy'
272254

273-
if $(command -v svn &> /dev/null) && $(command -v python2 &> /dev/null) ; then
274-
python2 - <<END
255+
if _command_exists svn_command_exists python2; then
256+
python2 - << END
275257
import ConfigParser, os
276258
config = ConfigParser.ConfigParser()
277259
config.read(os.path.expanduser('~/.subversion/servers'))
@@ -294,15 +276,14 @@ END
294276
fi
295277
}
296278

297-
svn-enable-proxy ()
298-
{
279+
function svn-enable-proxy() {
299280
about 'Enables SVN proxy settings'
300281
group 'proxy'
301282

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

305-
python2 - "$my_http_proxy" "$BASH_IT_NO_PROXY" <<END
286+
python2 - "${my_http_proxy:?}" "${BASH_IT_NO_PROXY:-}" << END
306287
import ConfigParser, os, sys, urlparse
307288
pieces = urlparse.urlparse(sys.argv[1])
308289
host = pieces.hostname
@@ -331,12 +312,11 @@ END
331312
fi
332313
}
333314

334-
ssh-show-proxy ()
335-
{
315+
function ssh-show-proxy() {
336316
about 'Shows SSH config proxy settings (from ~/.ssh/config)'
337317
group 'proxy'
338318

339-
if [ -f ~/.ssh/config ] ; then
319+
if [ -f ~/.ssh/config ]; then
340320
echo ""
341321
echo "SSH Config Enabled in ~/.ssh/config"
342322
echo "==================================="
@@ -368,25 +348,22 @@ ssh-show-proxy ()
368348
fi
369349
}
370350

371-
ssh-disable-proxy ()
372-
{
351+
function ssh-disable-proxy() {
373352
about 'Disables SSH config proxy settings'
374353
group 'proxy'
375354

376-
if [ -f ~/.ssh/config ] ; then
377-
sed -e's/^.*ProxyCommand/# ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
355+
if [ -f ~/.ssh/config ]; then
356+
sed -e's/^.*ProxyCommand/# ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
378357
echo "Disabled SSH config proxy settings"
379358
fi
380359
}
381360

382-
383-
ssh-enable-proxy ()
384-
{
361+
function ssh-enable-proxy() {
385362
about 'Enables SSH config proxy settings'
386363
group 'proxy'
387364

388-
if [ -f ~/.ssh/config ] ; then
389-
sed -e's/# ProxyCommand/ ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
365+
if [ -f ~/.ssh/config ]; then
366+
sed -e's/# ProxyCommand/ ProxyCommand/' "${BASH_IT_SED_I_PARAMETERS[@]}" ~/.ssh/config
390367
echo "Enabled SSH config proxy settings"
391368
fi
392369
}

0 commit comments

Comments
 (0)