hostman 0.1.0 → 0.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7373ab5c110b1075361295890bd0d671eeffe8f8
4
- data.tar.gz: 2fa6eed0cb331d572a6ab80efe19f5580e36b71f
3
+ metadata.gz: 7cd4f304ea4abc9a4b1b7f4b8e9a4fe4c0a554c6
4
+ data.tar.gz: 23504ce17bd69897849e9c0009ed3aefdb93eacd
5
5
  SHA512:
6
- metadata.gz: 9085e7b0d9bcafe14bb7e55e6693262391952b438acd22f8e8e072b530f4b52f787ce10d6f5ddc0821d92d9a98c26af697e85b707971aacbf734acfb7fc91f9f
7
- data.tar.gz: c8aef060414e5d4ceb96570d2475899f83dab78e048bc7a1b6ba8189dde0fb7159ced1525228f49dbaf3523e22439bbfe886c43ba3e4cdf32520c8b8a423f2cf
6
+ metadata.gz: 7bcd752925acea24ea8e0e3a9492bb67d28bad310e5320b31e58776c79a2efbfea2eae66b1ee3b52841f3205e7e5593fb1c271b77ceb029d2431bd040ba168dc
7
+ data.tar.gz: aab3ca0c1740abbb81ad86a5dbfee3fd978aaa7713304d32722d5152824f578078f7804a35e1f4e130265f0fa6743775ec9869c11f81eac9cdf7e0b2dbbd5c7e
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /test/fixtures/
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 TODO: Write your name
3
+ Copyright (c) 2016 Jean M. Lescure
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,28 +1,22 @@
1
1
  # Hostman
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hostman`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Hostman (host manager) is a stand-alone ruby gem that makes blocking and unblocking domains a breeze.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ![Demo gif](http://i.giphy.com/l0HlDHG0f1yBrfAsg.gif)
6
6
 
7
7
  ## Installation
8
8
 
9
- Add this line to your application's Gemfile:
9
+ Since this gem will edit the file `/etc/hosts`, in most day to day cases you'll need ti install it as root so it can be called using sudo.
10
10
 
11
- ```ruby
12
- gem 'hostman'
13
- ```
11
+ Install it using sudo:
14
12
 
15
- And then execute:
13
+ $ sudo gem install hostman
16
14
 
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install hostman
15
+ **NOTE:** You can use several workarounds to avoid having to sudo everytime you run this, if you do, it is under your own risk.
22
16
 
23
17
  ## Usage
24
18
 
25
- TODO: Write usage instructions here
19
+
26
20
 
27
21
  ## Development
28
22
 
@@ -32,7 +26,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
26
 
33
27
  ## Contributing
34
28
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hostman. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jeanlescure/hostman. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
30
 
37
31
 
38
32
  ## License
File without changes
@@ -1,64 +1,36 @@
1
1
  require "hostman/version"
2
+ require "hostman/executor"
2
3
  require "slop"
3
4
 
4
5
  module Hostman
5
6
  BLOCK_UNBLOCK_ERROR = "Both --block and --unblock set, doing nothing..."
6
7
 
7
- class Executor
8
- def exec
8
+ def self.block hosts_file, domain
9
+ not_blocked = true
9
10
 
10
- opts = Slop.parse do |o|
11
- o.string '-b', '--block', 'a hostname'
12
- o.string '-u', '--unblock', 'a hostname'
13
- o.string '-f', '--hosts-file', 'override hosts file path', default: '/etc/hosts'
14
- o.bool '-o', '--output-hosts', 'output hosts file contents after execution', default: false
15
- o.on '--version', 'print the version' do
16
- puts Hostman::VERSION
17
- exit
11
+ File.open(hosts_file, "r") do |f|
12
+ f.each_line do |line|
13
+ not_blocked = false if line == "127.0.0.1\t#{domain}\n"
18
14
  end
19
15
  end
20
16
 
21
- opts.inspect
22
- begin
23
- raise ArgumentError, ::Hostman::BLOCK_UNBLOCK_ERROR if !opts[:block].nil? and !opts[:unblock].nil?
24
- rescue ArgumentError => err
25
- puts err
26
- exit
27
- end
28
-
29
- if !opts[:block].nil?
30
- not_blocked = true
31
-
32
- File.open(opts["hosts-file"], "r") do |f|
33
- f.each_line do |line|
34
- not_blocked = false if line == "127.0.0.1\t#{opts[:block]}\n"
35
- end
36
- end
37
-
38
- if not_blocked
39
- File.open(opts["hosts-file"], "a") do |f|
40
- f.puts "127.0.0.1\t#{opts[:block]}"
41
- end
42
- end
43
- end
44
-
45
- if !opts[:unblock].nil?
46
- contents = ""
47
- File.open(opts["hosts-file"], "r") do |f|
48
- f.each_line do |line|
49
- contents += line unless line == "127.0.0.1\t#{opts[:unblock]}\n"
50
- end
51
- end
52
-
53
- File.open(opts["hosts-file"], "w") do |f|
54
- f.puts contents
17
+ if not_blocked
18
+ File.open(hosts_file, "a") do |f|
19
+ f.puts "127.0.0.1\t#{domain}"
55
20
  end
56
21
  end
22
+ end
57
23
 
58
- if opts["output-hosts"]
59
- puts File.read(opts["hosts-file"])
24
+ def self.unblock hosts_file, domain
25
+ contents = ""
26
+ File.open(hosts_file, "r") do |f|
27
+ f.each_line do |line|
28
+ contents += line unless line == "127.0.0.1\t#{domain}\n"
60
29
  end
30
+ end
61
31
 
32
+ File.open(hosts_file, "w") do |f|
33
+ f.puts contents
62
34
  end
63
35
  end
64
36
  end
@@ -0,0 +1,45 @@
1
+ module Hostman
2
+ class Executor
3
+ def exec
4
+
5
+ opts = Slop.parse do |o|
6
+ o.array '-b', '--block', 'a list of hostnames', delimiter: ','
7
+ o.array '-u', '--unblock', 'a list of hostname', delimiter: ','
8
+ o.string '-f', '--hosts-file', 'override hosts file path', default: '/etc/hosts'
9
+ o.bool '-o', '--output-hosts', 'output hosts file contents after execution', default: false
10
+ o.on '-h', '--help', 'print this help message' do
11
+ puts o
12
+ exit
13
+ end
14
+ o.on '--version', 'print the version' do
15
+ puts Hostman::VERSION
16
+ exit
17
+ end
18
+ end
19
+
20
+ begin
21
+ raise ArgumentError, ::Hostman::BLOCK_UNBLOCK_ERROR if !opts[:block].empty? and !opts[:unblock].empty?
22
+ rescue ArgumentError => err
23
+ puts err
24
+ exit
25
+ end
26
+
27
+ if !opts[:block].empty?
28
+ opts[:block].each do |domain|
29
+ ::Hostman.block opts["hosts-file"], domain
30
+ end
31
+ end
32
+
33
+ if !opts[:unblock].empty?
34
+ opts[:unblock].each do |domain|
35
+ ::Hostman.unblock opts["hosts-file"], domain
36
+ end
37
+ end
38
+
39
+ if opts.output_hosts?
40
+ puts File.read(opts["hosts-file"])
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Hostman
2
- VERSION = "0.1.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -24,14 +24,14 @@ describe Hostman do
24
24
  result.must_equal ::Fixtures::HOSTS_CONTENT
25
25
  end
26
26
 
27
- it "can block and unblock a domain" do
28
- `hostman --block test.com --hosts-file ./test/fixtures/hosts2`
27
+ it "can block and unblock domains" do
28
+ `hostman --block test.com,test2.com --block test3.com --hosts-file ./test/fixtures/hosts2`
29
29
 
30
- result = `hostman --block test2.com --hosts-file ./test/fixtures/hosts2 --output-hosts`.chop
31
- result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\ttest.com\n127.0.0.1\ttest2.com"
30
+ result = `hostman --block test4.com --hosts-file ./test/fixtures/hosts2 --output-hosts`.chop
31
+ result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\ttest.com\n127.0.0.1\ttest2.com\n127.0.0.1\ttest3.com\n127.0.0.1\ttest4.com"
32
32
 
33
- result = `hostman --unblock test.com --hosts-file ./test/fixtures/hosts2 --output-hosts`.chop
34
- result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\ttest2.com"
33
+ result = `hostman --unblock test.com,test2.com --unblock test3.com --hosts-file ./test/fixtures/hosts2 --output-hosts`.chop
34
+ result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\ttest4.com"
35
35
  end
36
36
 
37
37
  it "will not block more than once" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hostman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean M. Lescure
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-31 00:00:00.000000000 Z
11
+ date: 2016-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,9 +85,8 @@ files:
85
85
  - bin/setup
86
86
  - hostman.gemspec
87
87
  - lib/hostman.rb
88
+ - lib/hostman/executor.rb
88
89
  - lib/hostman/version.rb
89
- - test/fixtures/hosts1
90
- - test/fixtures/hosts2
91
90
  - test/hostman_test.rb
92
91
  - test/test_helper.rb
93
92
  homepage: http://jeanlescure.io
@@ -114,7 +113,5 @@ signing_key:
114
113
  specification_version: 4
115
114
  summary: Manage the blocked hosts in /etc/hosts
116
115
  test_files:
117
- - test/fixtures/hosts1
118
- - test/fixtures/hosts2
119
116
  - test/hostman_test.rb
120
117
  - test/test_helper.rb
@@ -1 +0,0 @@
1
- 127.0.0.1 localhost
@@ -1,2 +0,0 @@
1
- 127.0.0.1 localhost
2
- 127.0.0.1 test2.com