Files
hatchet/hack/dev/manage-hosts.sh
Luca Steeb 6fef81eb52 chore(contributing): improve onboarding (#3)
- shell scripts logging and fail on error
- use npx --yes instead of manual global install
- run go libs directly instead of installing and running binaries
- add missing brew dependencies
2023-12-18 13:36:48 -05:00

50 lines
1.3 KiB
Bash

#!/bin/sh
set -eux
# run:
# ./manage-etc-hosts.sh add 10.20.1.2 test.com
# ./manage-etc-hosts.sh remove 10.20.1.2 test.com
# PATH TO YOUR HOSTS FILE
ETC_HOSTS=/etc/hosts
function remove() {
# IP to add/remove.
IP=$1
# Hostname to add/remove.
HOSTNAME=$2
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
if [ -n "$(grep $HOSTS_LINE $ETC_HOSTS)" ]
then
echo "$HOSTS_LINE Found in your $ETC_HOSTS, Removing now...";
sudo sed -i".bak" "/$HOSTS_LINE/d" $ETC_HOSTS
else
echo "$HOSTS_LINE was not found in your $ETC_HOSTS";
fi
}
function add() {
IP=$1
HOSTNAME=$2
HOSTS_LINE="$IP[[:space:]]$HOSTNAME"
line_content=$( printf "%s %s\n" "$IP" "$HOSTNAME" )
# grep "$HOSTS_LINE" "$ETC_HOSTS"
if [ -n "$(grep $HOSTS_LINE $ETC_HOSTS)" ]
then
echo "$line_content already exists : $(grep $HOSTNAME $ETC_HOSTS)"
else
echo "Adding $line_content to your $ETC_HOSTS";
sudo -- sh -c -e "echo '$line_content' >> /etc/hosts";
if [ -n "$(grep $HOSTNAME $ETC_HOSTS)" ]
then
echo "$line_content was added successfully";
else
echo "Failed to Add $line_content, Try again!";
fi
fi
}
$@