Created
August 1, 2023 16:25
-
-
Save jimvandervoort/62fff1c042bcf2de8e29a74b44898f20 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 -Eeuo pipefail | |
hosts_file="/etc/hosts" | |
hosts=( | |
"www.reddit.com" | |
"twitter.com" | |
"www.nrc.nl" | |
"www.theguardian.com" | |
) | |
block() { | |
echo "127.0.0.1 $1" | sudo tee -a "$hosts_file" >/dev/null | |
} | |
unblock() { | |
sudo sed -i '' "/$1/d" "$hosts_file" | |
} | |
if grep -qF "twitter.com" "$hosts_file"; then | |
for host in "${hosts[@]}"; do | |
unblock "$host" | |
done | |
echo "Focus if off ⚡️" | |
else | |
for host in "${hosts[@]}"; do | |
block "$host" | |
done | |
echo "Focus is on 😌" | |
fi | |
# Flush DNS cache | |
sudo dscacheutil -flushcache | |
sudo killall -HUP mDNSResponder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment