focus.rb 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/focus +47 -0
  3. data/lib/focus/config.rb +58 -0
  4. data/lib/focus/host.rb +63 -0
  5. metadata +62 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be3ca51dc59f5a69a6b15b2381b7596776c7a85b656f2402db409ef11a095e0a
4
+ data.tar.gz: dfed043bd5a771f9ecc8ad7e22eb898349bbece9cb5c9520da50cf37256e7390
5
+ SHA512:
6
+ metadata.gz: a551d74afb999598545732fe17a94f0e96581b44a6b06e3ea6cce87fab20f66ed51cda65715238abc8c1612fb468b2f72f4774e4ad81ed96310bd35c2c897311
7
+ data.tar.gz: 3230ffd2acd77850eaf32bd2eb01b3bf150bd531beaa291c3d3cee11cf5f2821ab1307cef3f0e16dc78f56cb0ce32bf29c70b1769835868afa85faa3e2a9f612
data/bin/focus ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "colorize"
4
+ require "optparse"
5
+ require "focus/config"
6
+ require "focus/host"
7
+
8
+ script_name = File.basename($0)
9
+ config_path = ".config/focus.rb/config.yaml"
10
+ action = :list_block_hosts
11
+ args = []
12
+ options_parser = nil
13
+
14
+ ARGV.options do |opts|
15
+ options_parser = opts
16
+
17
+ opts.banner = "Usage: ruby #{script_name} [--start | --restore]
18
+ Help to focus on your work, block leasure website while you need working, unblock for week-ends !
19
+ "
20
+
21
+ opts.on("-c=PATH", "--config=PATH", "Change the default config path") { |path| config_path = path }
22
+
23
+ opts.on("-a=BLOCK", "--add-block-host=BLOCK", "Add a new host to block") { |block| action = :add_block_host; args = [block] }
24
+ opts.on("-d=BLOCK", "--delete-block-host=BLOCK", "Delete a host to block") { |block| action = :delete_block_host; args = [block] }
25
+
26
+ opts.on("-b", "--block", "Block non-work website") { action = :block }
27
+ opts.on("-r", "--restore", "Restore non-work website") { action = :restore }
28
+
29
+ opts.separator ""
30
+
31
+ opts.on("-h", "--help", "Show this help message.") { action = :help }
32
+
33
+ opts.parse!
34
+ end
35
+
36
+ config = Focus::Config.new(filepath: config_path)
37
+
38
+ ACTIONS = {
39
+ list_block_hosts: lambda { puts config.block_hosts },
40
+ add_block_host: lambda { config.add_block_host(*args) },
41
+ delete_block_host: lambda { config.add_block_host(*args) },
42
+ block: lambda { Focus::Host.new.block!(config) },
43
+ restore: lambda { Focus::Host.new.restore!(config) },
44
+ help: lambda { puts options_parser }
45
+ }
46
+
47
+ ACTIONS.fetch(action, ACTIONS[:help]).call()
@@ -0,0 +1,58 @@
1
+ require "yaml"
2
+
3
+ module Focus
4
+ class Config
5
+ BASE_PATH = ENV["HOME"]
6
+ DEFAULT_BLOCK_HOSTS = %w(reddit.com youtube.com okcupid.com)
7
+
8
+ def initialize(filepath:)
9
+ @filepath = File.join(BASE_PATH, filepath)
10
+ dir = File.dirname(@filepath)
11
+ if !File.exist?(dir)
12
+ Dir.mkdir(dir)
13
+ end
14
+ if !File.exist?(@filepath)
15
+ write(
16
+ {
17
+ block_hosts: DEFAULT_BLOCK_HOSTS,
18
+ },
19
+ )
20
+ end
21
+ end
22
+
23
+ def read
24
+ YAML.load_file(@filepath)
25
+ end
26
+
27
+ def write(data)
28
+ data[:block_hosts].uniq!
29
+ File.write(@filepath, data.to_yaml)
30
+ end
31
+
32
+ def block_hosts
33
+ read[:block_hosts]
34
+ end
35
+
36
+ def add_block_host(blocks)
37
+ puts "This operation adds \"#{blocks}\" to the list of blocked hosts.".yellow
38
+ puts "This operation does not block the hosts. Execute -b to execute the blocking.".yellow
39
+ blocks.split(",").each do |block|
40
+ data = read
41
+ data[:block_hosts] << block
42
+ write(data)
43
+ puts "Added host \"#{block}\"!".green
44
+ end
45
+ end
46
+
47
+ def delete_block_host(blocks)
48
+ puts "This operation removes \"#{blocks}\" to the list of blocked hosts.".yellow
49
+ puts "This operation does not unblock the hosts. Execute -b to refresh the blocking or -r to reset it.".yellow
50
+ blocks.split(",").each do |block|
51
+ data = read
52
+ data[:block_hosts].delete_if { |host| host == block }
53
+ write(data)
54
+ puts "Removed host \"#{block}\"!".green
55
+ end
56
+ end
57
+ end
58
+ end
data/lib/focus/host.rb ADDED
@@ -0,0 +1,63 @@
1
+ module Focus
2
+ class Host
3
+ def initialize(file = "/etc/hosts")
4
+ @file = file
5
+ read!
6
+ end
7
+
8
+ def get_index
9
+ start_index = @all_lines.find_index { |line| /### FOCUS:start \(automaticaly generated\) ###/i === line }
10
+ stop_index = @all_lines.find_index { |line| /### FOCUS:stop \(automaticaly generated\) ###/i === line }
11
+ { start: start_index + 1, stop: stop_index - 1 } if start_index && stop_index
12
+ end
13
+
14
+ def read!
15
+ @all_lines = File.read(@file).split("\n")
16
+ index = get_index
17
+ @data = []
18
+ if index
19
+ selected_lines = @all_lines[index[:start]..index[:stop]]
20
+ @data = selected_lines.map { |line| line.split(/\s+/) }.map { |ip, host| { ip: ip, host: host } }
21
+ end
22
+ end
23
+
24
+ def overwrite!
25
+ index = get_index
26
+ if index
27
+ count = index[:stop] - index[:start]
28
+ @all_lines << "" if @all_lines[index[:start] - 2] != ""
29
+ @all_lines.slice!(index[:start] - 1, count + 3)
30
+ else
31
+ @all_lines << "" if @all_lines.last != ""
32
+ end
33
+
34
+ @all_lines << "### FOCUS:start (automaticaly generated) ###"
35
+ @data.each do |managed|
36
+ @all_lines << "#{managed[:ip]} #{managed[:host]}"
37
+ end
38
+ @all_lines << "### FOCUS:stop (automaticaly generated) ###"
39
+ @all_lines << ""
40
+ File.write(@file, @all_lines.join("\n"))
41
+ end
42
+
43
+ def block!(config)
44
+ puts "Blocking the hosts".green
45
+ read!
46
+ config.block_hosts.each do |block_host|
47
+ puts block_host.red
48
+ @data << { host: block_host, ip: "127.0.0.1" }
49
+ @data << { host: "www.#{block_host}", ip: "127.0.0.1" }
50
+ end
51
+ @data.uniq!
52
+ overwrite!
53
+ puts "Done!".green
54
+ end
55
+
56
+ def restore!(config)
57
+ puts "Restoring blocked hosts".green
58
+ @data = []
59
+ overwrite!
60
+ puts "Done!".green
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: focus.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Poulet
8
+ autorequire:
9
+ bindir:
10
+ - bin
11
+ cert_chain: []
12
+ date: 2021-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colorize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.8.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.8.1
28
+ description: A tool to help you focus on work* Block hosts names localy
29
+ email:
30
+ - arthur.focus@sceptique.eu
31
+ executables:
32
+ - focus
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - bin/focus
37
+ - lib/focus/config.rb
38
+ - lib/focus/host.rb
39
+ homepage: https://git.sceptique.eu/Sceptique/focus.rb
40
+ licenses:
41
+ - GPL-3.0
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.2.27
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: A tool to help you focus on work
62
+ test_files: []