-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_gate.sh
More file actions
executable file
·167 lines (149 loc) · 3.63 KB
/
Copy pathcommit_gate.sh
File metadata and controls
executable file
·167 lines (149 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# Blocks chosen hostnames in /etc/hosts until $GITHUB_USER has made a GitHub commit today (local TZ).
set -u
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ENV_FILE="$SCRIPT_DIR/.env"
if [ -f "$ENV_FILE" ]; then
set -a
# shellcheck disable=SC1090
. "$ENV_FILE"
set +a
fi
: "${GITHUB_USER:?GITHUB_USER is not set. Copy .env.example to .env and fill it in.}"
: "${BLOCKED_HOSTS:=mail.google.com,www.mail.google.com,gmail.com,www.gmail.com}"
HOSTS_FILE="/etc/hosts"
MARKER_BEGIN="# >>> commit_motivation >>>"
MARKER_END="# <<< commit_motivation <<<"
IFS=',' read -r -a BLOCKED_HOSTS_ARR <<< "$BLOCKED_HOSTS"
STATE_DIR="$HOME/.commit_motivation"
LOG_FILE="$STATE_DIR/gate.log"
STAGED_HOSTS="$STATE_DIR/hosts.staged"
mkdir -p "$STATE_DIR"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE"
}
today_local() {
date '+%Y-%m-%d'
}
has_commit_today() {
local today
today=$(today_local)
local response
response=$(curl -fsS --max-time 10 \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/users/${GITHUB_USER}/events/public?per_page=100" 2>/dev/null) || {
log "GitHub API unreachable — failing open (not blocking)"
return 0
}
echo "$response" | /usr/bin/python3 -c "
import json, sys, datetime
today = '$today'
try:
events = json.load(sys.stdin)
except Exception:
sys.exit(2)
for e in events:
if e.get('type') != 'PushEvent':
continue
created = e.get('created_at', '')
try:
utc = datetime.datetime.strptime(created, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=datetime.timezone.utc)
except Exception:
continue
local_date = utc.astimezone().strftime('%Y-%m-%d')
if local_date == today:
sys.exit(0)
sys.exit(1)
"
return $?
}
is_blocked() {
grep -q "$MARKER_BEGIN" "$HOSTS_FILE"
}
flush_dns() {
sudo -n /usr/bin/dscacheutil -flushcache 2>/dev/null
sudo -n /usr/bin/killall -HUP mDNSResponder 2>/dev/null
}
desired_block() {
echo "$MARKER_BEGIN"
for h in "${BLOCKED_HOSTS_ARR[@]}"; do
h=$(echo "$h" | xargs)
[ -z "$h" ] && continue
echo "127.0.0.1 $h"
echo "::1 $h"
done
echo "$MARKER_END"
}
current_block() {
awk -v b="$MARKER_BEGIN" -v e="$MARKER_END" '
$0==b {inside=1}
inside {print}
$0==e {inside=0}
' "$HOSTS_FILE"
}
block_matches_desired() {
[ "$(current_block)" = "$(desired_block)" ]
}
apply_block() {
if is_blocked && block_matches_desired; then
return 0
fi
if is_blocked; then
awk -v b="$MARKER_BEGIN" -v e="$MARKER_END" '
$0==b {skip=1; next}
$0==e {skip=0; next}
!skip {print}
' "$HOSTS_FILE" > "$STAGED_HOSTS"
else
cat "$HOSTS_FILE" > "$STAGED_HOSTS"
fi
{
echo ""
desired_block
} >> "$STAGED_HOSTS"
sudo -n /bin/cp "$STAGED_HOSTS" "$HOSTS_FILE"
local rc=$?
flush_dns
log "block applied (rc=$rc, hosts=$BLOCKED_HOSTS)"
return $rc
}
remove_block() {
if ! is_blocked; then
return 0
fi
awk -v b="$MARKER_BEGIN" -v e="$MARKER_END" '
$0==b {skip=1; next}
$0==e {skip=0; next}
!skip {print}
' "$HOSTS_FILE" > "$STAGED_HOSTS"
sudo -n /bin/cp "$STAGED_HOSTS" "$HOSTS_FILE"
local rc=$?
flush_dns
log "block removed (rc=$rc)"
return $rc
}
case "${1:-check}" in
check)
if has_commit_today; then
log "commit found today — unblocking"
remove_block
else
log "no commit today — blocking"
apply_block
fi
;;
reset)
log "daily reset — re-engaging block"
apply_block
;;
unblock)
remove_block
;;
status)
if is_blocked; then echo "BLOCKED"; else echo "UNBLOCKED"; fi
;;
*)
echo "usage: $0 {check|reset|unblock|status}" >&2
exit 2
;;
esac