Last active
December 6, 2019 17:14
-
-
Save paolostefan/3c9f02e5fdcab8eb7432db263f8f19c2 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
set -euo pipefail | |
BASE=/etc/hostenvs/ | |
function usage { | |
echo "Usage: $0 <environment>" | |
echo | |
echo "<environment> is the name of a file in ${BASE} to be symlinked to /etc/hosts." | |
echo | |
echo -n "Available choices: " | |
ls ${BASE} | |
echo -n "Currently /etc/hosts is " | |
if [ -L /etc/hosts ]; then | |
echo "a link to $(readlink /etc/hosts)" | |
else | |
if [ -f /etc/hosts ]; then | |
echo "a regular file (not symlinked to anything)" | |
else | |
echo "something strange ($(file /etc/hosts))" | |
fi | |
fi | |
exit 1 | |
} | |
if [ "$EUID" -ne 0 ]; then | |
echo "Please run $0 as root" | |
exit 2 | |
fi | |
if [ ! -x "${BASE}" ]; then | |
echo "Creating ${BASE} for storing environments..." | |
mkdir -p "${BASE}" | |
mv /etc/hosts "${BASE}"default | |
ln -f -s "${BASE}"default /etc/hosts | |
else | |
if [ ! -L /etc/hosts ]; then | |
# Non è un link, copiamolo per sicurezza e poi creiamo il link | |
mv /etc/hosts /etc/hosts.bak | |
fi | |
fi | |
if (( $# < 1 )) || [ "$1" == "" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then | |
usage | |
fi | |
if [ ! -f "${BASE}$1" ]; then | |
echo "$1: environment not found" | |
usage | |
fi | |
ln -f -s "${BASE}$1" /etc/hosts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment