Skip to content

Instantly share code, notes, and snippets.

@jimvandervoort
Created August 1, 2023 16:25
Show Gist options
  • Save jimvandervoort/62fff1c042bcf2de8e29a74b44898f20 to your computer and use it in GitHub Desktop.
Save jimvandervoort/62fff1c042bcf2de8e29a74b44898f20 to your computer and use it in GitHub Desktop.
#!/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