focus.rb/bin/focus

54 lines
1.7 KiB
Ruby
Executable File

#!/usr/bin/env ruby
$LOAD_PATH << File.join(Dir.pwd, "lib")
require "colorize"
require "optparse"
require "focus/config"
require "focus/host"
script_name = File.basename($0)
config_path = ".config/focus.rb/config.yaml"
action = :list_block_hosts
args = []
options_parser = nil
dry = false
ARGV.options do |opts|
options_parser = opts
opts.banner = "Usage: ruby #{script_name} [--start | --restore]
Help to focus on your work, block leasure website while you need working, unblock for week-ends !
"
opts.on("-c=PATH", "--config=PATH", "Change the default config path") { |path| config_path = path }
opts.on("-a=BLOCK", "--add-block-host=BLOCK", "Add a new host to block") { |block| action = :add_block_host; args = [block] }
opts.on("-d=BLOCK", "--delete-block-host=BLOCK", "Delete a host to block") { |block| action = :delete_block_host; args = [block] }
opts.on("-b", "--block", "Block non-work website") { action = :block }
opts.on("-r", "--restore", "Restore non-work website") { action = :restore }
opts.on("-d", "--dry", "Don't write on real files") { dry = true }
opts.separator ""
opts.on("-h", "--help", "Show this help message.") { action = :help }
opts.parse!
end
config = Focus::Config.new(filepath: config_path)
host_file = "/etc/hosts"
host_file = "/dev/null" if dry
ACTIONS = {
list_block_hosts: lambda { puts config.block_hosts },
add_block_host: lambda { config.add_block_host(*args) },
delete_block_host: lambda { config.add_block_host(*args) },
block: lambda { Focus::Host.new(host_file).block!(config) },
restore: lambda { Focus::Host.new(host_file).restore!(config) },
help: lambda { puts options_parser }
}
ACTIONS.fetch(action, ACTIONS[:help]).call()