forked from mlafeldt/get-shit-done.rb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-shit-done.rb
executable file
·75 lines (62 loc) · 1.46 KB
/
get-shit-done.rb
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
#!/usr/bin/env ruby
HOSTS_FILE = "/etc/hosts"
USER_SITES_FILE = "~/.get-shit-done.ini"
GLOBAL_SITES_FILE = "./sites.ini"
START_TOKEN = "## start-gsd"
END_TOKEN = "## end-gsd"
def get_sites(filename)
sites = []
path = File.expand_path(filename)
if File.exists?(path)
File.read(path).scan(/^sites\s*=\s*(.*)/).flatten.each do |m|
sites += m.split(",").map(&:strip)
end
end
sites
end
def flush_dns
cmd = case RUBY_PLATFORM
when /linux/
["/etc/init.d/networking", "restart"]
when /darwin/
["dscacheutil", "-flushcache"]
else
["echo", "Please contribute DNS cache flush command on GitHub."]
end
system(*cmd)
end
def work?
File.read(HOSTS_FILE).include?(START_TOKEN)
end
def work
abort "error: work mode already set" if work?
sites = get_sites(GLOBAL_SITES_FILE) + get_sites(USER_SITES_FILE)
abort "error: no sites configured" if sites.empty?
File.open(HOSTS_FILE, "a") do |f|
f.puts START_TOKEN
sites.uniq.sort.each do |site|
f.puts "127.0.0.1\t#{site}"
f.puts "127.0.0.1\twww.#{site}"
end
f.puts END_TOKEN
end
flush_dns
end
def play?
!work?
end
def play
abort "error: play mode already set" if play?
File.open(HOSTS_FILE, "r+") do |f|
i = f.read.index(/#{START_TOKEN}.*#{END_TOKEN}/m)
f.truncate(i) unless i.nil?
end
flush_dns
end
mode = ARGV.first
case mode
when "work", "play"
Object.send(mode)
else
abort "usage: #{File.basename($PROGRAM_NAME)} <work | play>"
end