Last active
August 31, 2017 14:56
-
-
Save jBouyoud/c58df83db08afbe1cd5e75d000e0a75d to your computer and use it in GitHub Desktop.
Utilities functions to setup proxies able to switch easily
This file contains hidden or 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
############################ | |
# Npm proxy settings | |
############################ | |
alias init-npm-proxy='echo "" > ~/.npmrc' | |
alias rm-npm-proxy='sed -i '/proxy/d' ~/.npmrc' | |
set-npm-proxy() { | |
echo "proxy=$1" >> ~/.npmrc | |
echo "https-proxy=$1" >> ~/.npmrc | |
echo "https_proxy=$1" >> ~/.npmrc | |
} | |
############################ | |
# Bower proxy settings | |
############################ | |
init-bower-proxy() { | |
echo "{" > ~/.bowerrc | |
echo '}' >> ~/.bowerrc | |
} | |
alias rm-bower-proxy='sed -i '/proxy/d' ~/.bowerrc' | |
set-bower-proxy() { | |
sed -i '$ d' ~/.bowerrc | |
sed -i 's/[^,\{]$/,/' ~/.bowerrc | |
echo " \"proxy\":\"$1\"," >> ~/.bowerrc | |
echo " \"https-proxy\":\"$1\"" >> ~/.bowerrc | |
echo '}' >> ~/.bowerrc | |
} | |
############################ | |
# Gradle proxy settings | |
############################ | |
export GRADLE_USER_HOME="${GRADLE_USER_HOME:-$HOME/.gradle}" | |
init-gradle-proxy() { | |
mkdir $GRADLE_USER_HOME 2>/dev/null | |
cat <<EOF > $GRADLE_USER_HOME/gradle.properties | |
systemProp.http.keepAlive="true" | |
systemProp.http.nonProxyHosts=localhost|127.0.0.1 | |
systemProp.https.keepAlive="true" | |
systemProp.https.nonProxyHosts=localhost|127.0.0.1 | |
EOF | |
} | |
alias rm-gradle-proxy='sed -i '/proxy/d' $GRADLE_USER_HOME/gradle.properties' | |
set-gradle-proxy() { | |
cat <<EOF >> $GRADLE_USER_HOME/gradle.properties | |
systemProp.proxySet="true" | |
systemProp.http.proxyHost=$1 | |
systemProp.http.proxyPort=$2 | |
systemProp.https.proxyHost=$1 | |
systemProp.https.proxyPort=$2 | |
EOF | |
} | |
############################ | |
# Maven proxy settings | |
############################ | |
export MVN_USER_HOME="${MVN_USER_HOME:-$HOME/.m2}" | |
init-mvn-proxy() { | |
mkdir $MVN_USER_HOME 2>/dev/null | |
cat <<EOF > $MVN_USER_HOME/settings.xml | |
<settings> | |
</settings> | |
EOF | |
} | |
rm-mvn-proxy() { | |
cat $MVN_USER_HOME/settings.xml | awk '/<proxies>/{hide=1} /<\/proxies>/ {hide=0} {if (hide==0) print;}' | sed '/proxies/d' > $MVN_USER_HOME/settings.xml | |
} | |
set-mvn-proxy() { | |
sed -i '$ d' $MVN_USER_HOME/settings.xml | |
cat <<EOF >> $MVN_USER_HOME/settings.xml | |
<proxies> | |
<proxy> | |
<id>http-proxy</id> | |
<active>true</active> | |
<protocol>http</protocol> | |
<host>$1</host> | |
<port>$2</port> | |
<nonProxyHosts>localhost|127.0.0.1</nonProxyHosts> | |
</proxy> | |
<proxy> | |
<id>https-proxy</id> | |
<active>true</active> | |
<protocol>https</protocol> | |
<host>$1</host> | |
<port>$2</port> | |
<nonProxyHosts>localhost|127.0.0.1</nonProxyHosts> | |
</proxy> | |
</proxies> | |
</settings> | |
EOF | |
} | |
############################ | |
# git proxy settings | |
############################ | |
alias rm-git-proxy='sed -i '/proxy/d' ~/.gitconfig' | |
set-git-proxy() { | |
git config --global http.proxy $1 | |
git config --global https.proxy $1 | |
} | |
############################ | |
# yarn proxy settings | |
############################ | |
#export PATH="$PATH:$HOME/.config/yarn/global/node_modules/.bin" | |
rm-yarn-proxy() { | |
#yarn config delete proxy $1 | |
#yarn config delete https-proxy $1 | |
echo 'Not installed' > /dev/null | |
} | |
set-yarn-proxy() { | |
#yarn config set proxy $1 | |
#yarn config set https-proxy $1 | |
echo 'Not installed' > /dev/null | |
} | |
############################ | |
# Global utilities | |
############################ | |
proxy-rm(){ | |
unset HTTP_PROXY | |
unset HTTPS_PROXY | |
unset http_proxy | |
unset https_proxy | |
rm-npm-proxy | |
rm-bower-proxy | |
rm-gradle-proxy | |
rm-git-proxy | |
rm-mvn-proxy | |
rm-yarn-proxy | |
} | |
proxy-init(){ | |
init-npm-proxy | |
init-bower-proxy | |
init-gradle-proxy | |
init-mvn-proxy | |
} | |
proxy-set(){ | |
proxy-rm | |
PROXY=http://$1:$2 | |
export HTTP_PROXY=$PROXY | |
export HTTPS_PROXY=$PROXY | |
export http_proxy=$PROXY | |
export https_proxy=$PROXY | |
export NO_PROXY=$NO_PROXY,127.0.0.1,localhost | |
export no_proxy=$no_proxy,127.0.0.1,localhost | |
set-npm-proxy $PROXY | |
set-bower-proxy $PROXY | |
set-gradle-proxy $1 $2 | |
set-mvn-proxy $1 $2 | |
set-git-proxy $PROXY | |
set-yarn-proxy $PROXY | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment