Skip to content

Commit 35725d5

Browse files
committed
GET SHIT DONE again.
1 parent 4f8c780 commit 35725d5

1 file changed

Lines changed: 157 additions & 0 deletions

File tree

bin/get-shit-done

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/bin/bash
2+
#
3+
# gsd
4+
# A simple script to help you get work done.
5+
#
6+
#
7+
# on
8+
# Turns on the blocking.
9+
#
10+
# off
11+
# Turns off the blocking.
12+
#
13+
# version, -v
14+
# Prints the version number.
15+
#
16+
# help, -h
17+
# Prints some instructions.
18+
19+
# Constants
20+
#
21+
22+
VERSION=0.1.4
23+
HOSTS="/etc/hosts"
24+
HOSTS_BACKUP="$HOSTS.backup"
25+
BLOCKED_HOSTS="$HOSTS.blocked"
26+
27+
28+
# Function
29+
#
30+
function on {
31+
echo
32+
echo "It's time to Get Shit Done(tm)."
33+
echo
34+
35+
# Make a backup of hosts file, if DNE.
36+
if [ ! -e $HOSTS_BACKUP ]; then
37+
echo " Making a backup of $HOSTS..."
38+
cp $HOSTS $HOSTS_BACKUP
39+
echo " Backup file is $HOSTS_BACKUP."
40+
fi
41+
42+
# Read from the array passed in, and add to /etc/hosts
43+
echo " Blocking:"
44+
for site in $*; do
45+
echo " "$site
46+
if ! grep " "$site $HOSTS > /dev/null; then
47+
echo "127.0.0.1 $site" >> $HOSTS
48+
fi
49+
done
50+
51+
# Flush the cache.
52+
flush
53+
54+
# Exit.
55+
echo
56+
exit
57+
}
58+
59+
function off {
60+
echo
61+
echo "Ok, time to relax."
62+
echo " Unblocking sites..."
63+
64+
# Read from the array, and remove files from /etc/hosts
65+
sed_i='-i ""'
66+
[[ $(uname) == Linux ]] && sed_i='-i'
67+
for site in $*; do
68+
sed $sed_i "/127.0.0.1 $site/d" $HOSTS
69+
done
70+
71+
# Flush the cache.
72+
flush
73+
74+
# And, done.
75+
echo " The internet is back in town."
76+
echo
77+
exit
78+
}
79+
80+
function flush {
81+
if [[ $(uname) == Darwin ]]; then
82+
echo
83+
echo " Flushing cache..."
84+
$(which dscacheutil) -flushcache
85+
fi
86+
}
87+
88+
function version {
89+
echo "gsd, v$VERSION"
90+
exit
91+
}
92+
93+
function hlp {
94+
echo
95+
echo "gsd, the get shit done script"
96+
echo
97+
echo "on"
98+
echo " Blocks a list of sites by modifying the $HOSTS file."
99+
echo "off"
100+
echo " Unblocks sites from your list by modifying the $HOSTS file."
101+
echo "-v"
102+
echo " Prints the version."
103+
echo "-h"
104+
echo " Prints these instructions."
105+
echo
106+
107+
exit
108+
}
109+
110+
function checkSudo {
111+
if [[ $UID != 0 ]]; then
112+
echo "usage: 'sudo gsd $1'" >&2
113+
exit 2
114+
fi
115+
}
116+
117+
# If called without arguments, exit.
118+
if [[ $# == 0 ]]; then
119+
echo
120+
echo "usage: gsd on ... turns on blocking."
121+
echo " gsd off ... turns off blocking."
122+
echo
123+
exit
124+
125+
else
126+
# Find all blocked hosts
127+
if [ ! -f $BLOCKED_HOSTS ]; then
128+
echo "ERROR: The list of websites to block does not exist."
129+
echo " Make sure there's a hosts.blocked file in /etc/." >&1
130+
exit 1
131+
fi
132+
133+
# Read all sites from our $BLOCKED_HOSTS file and remove prefix www.
134+
blocked_sites=( $(sed -n '/^[a-zA-Z0-9].*/p ; s/^www\.//p' $BLOCKED_HOSTS) )
135+
136+
case $1 in
137+
on)
138+
# Make sure sudo is used.
139+
checkSudo $1
140+
on ${blocked_sites[@]}
141+
;;
142+
143+
off)
144+
# Make sure sudo is used.
145+
checkSudo $1
146+
off ${blocked_sites[@]}
147+
;;
148+
149+
version|-v)
150+
version
151+
;;
152+
153+
help|-h)
154+
hlp
155+
;;
156+
esac
157+
fi

0 commit comments

Comments
 (0)