akaer 1.0.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,49 +11,44 @@ module Akaer
11
11
  # The {Configuration Configuration} of this application.
12
12
  attr_reader :config
13
13
 
14
- # The arguments passed via command-line.
15
- attr_reader :args
14
+ # The Mamertes command.
15
+ attr_reader :command
16
16
 
17
17
  # The logger for this application.
18
18
  attr_accessor :logger
19
19
 
20
20
  # Creates a new application.
21
21
  #
22
- # @param globals [Hash] Global options.
23
- # @param locals [Hash] Local command options.
24
- # @param args [Array] Extra arguments.
25
- def initialize(globals = {}, locals = {}, args = [])
26
- @args = {
27
- :global => globals,
28
- :local => locals,
29
- :args => args
30
- }
22
+ # @param command [Mamertes::Command] The current Mamertes command.
23
+ def initialize(command)
24
+ @command = command
25
+ application = @command.application
31
26
 
32
27
  # Setup logger
33
28
  Bovem::Logger.start_time = Time.now
34
- @logger = Bovem::Logger.create(Bovem::Logger.get_real_file(@args[:global][:log_file]) || Bovem::Logger.default_file, Logger::INFO)
29
+ @logger = Bovem::Logger.create(Bovem::Logger.get_real_file(application.options["log-file"].value) || Bovem::Logger.default_file, Logger::INFO)
35
30
 
36
31
  # Open configuration
37
32
  begin
38
33
  overrides = {
39
- :interface => @args[:global][:interface],
40
- :addresses => @args[:global][:addresses],
41
- :start_address => @args[:global][:"start-address"],
42
- :aliases => @args[:global][:aliases],
43
- :add_command => @args[:global][:"add-command"],
44
- :remove_command => @args[:global][:"remove-command"],
45
- :log_file => @args[:global][:"log-file"],
46
- :log_level => @args[:global][:"log-level"],
47
- :dry_run => @args[:global][:"dry-run"],
48
- :quiet => @args[:global][:quiet]
34
+ :interface => application.options["interface"].value,
35
+ :addresses => application.options["addresses"].value,
36
+ :start_address => application.options["start-address"].value,
37
+ :aliases => application.options["aliases"].value,
38
+ :add_command => application.options["add-command"].value,
39
+ :remove_command => application.options["remove-command"].value,
40
+ :log_file => application.options["log-file"].value,
41
+ :log_level => application.options["log-level"].value,
42
+ :dry_run => application.options["dry-run"].value,
43
+ :quiet => application.options["quiet"].value
49
44
  }.reject {|k,v| v.nil? }
50
45
 
51
- @config = Akaer::Configuration.new(@args[:global][:config], overrides, @logger)
46
+ @config = Akaer::Configuration.new(application.options["configuration"].value, overrides, @logger)
52
47
 
53
48
  @logger = nil
54
49
  @logger = self.get_logger
55
50
  rescue Bovem::Errors::InvalidConfiguration => e
56
- @logger ? @logger.fatal(e.message) : Bovem::Logger.create("STDERR").fatal("Cannot log to #{config.log_file}. Exiting...")
51
+ @logger ? @logger.fatal(e.message) : Bovem::Logger.create("STDERR").fatal("Cannot log to {mark=bright}#{config.log_file}{/mark}. Exiting...")
57
52
  raise ::SystemExit
58
53
  end
59
54
 
@@ -182,16 +177,15 @@ module Akaer
182
177
  @addresses ||= self.compute_addresses
183
178
  length = self.pad_number(@addresses.length)
184
179
  index = (@addresses.index(address) || 0) + 1
185
- prefix = ["[".color(:cyan), self.pad_number(index, length.length).bright, "/".color(:cyan), length.bright, "]".color(:cyan)].join("")
180
+ prefix = "{mark=blue}[{mark=bright white}#{self.pad_number(index, length.length)}{mark=reset blue}/{/mark}#{length}{/mark}]{/mark}"
186
181
 
187
182
  # Now execute
188
183
  if !self.config.dry_run then
189
- @logger.info("#{prefix} #{(type == :remove ? "Removing" : "Adding").bright} address #{address.bright} #{type != :remove ? "to" : "from"} interface #{self.config.interface.bright}...") if !self.config.quiet
184
+ @logger.info(@command.application.console.replace_markers("#{prefix} {mark=bright}#{(type == :remove ? "Removing" : "Adding")}{/mark} address {mark=bright}#{address}{/mark} #{type != :remove ? "to" : "from"} interface {mark=bright}#{self.config.interface}{/mark}...")) if !self.config.quiet
190
185
  rv = self.execute_command(command)
191
-
192
- # TODO: The end badge.
186
+ @logger.error(@command.application.console.replace_markers("Cannot {mark=bright}#{(type == :remove ? "remove" : "add")}{/mark} address {mark=bright}#{address}{/mark} #{type != :remove ? "to" : "from"} interface {mark=bright}#{self.config.interface}{/mark}.")) if !rv
193
187
  else
194
- @logger.info("#{prefix} I will #{(type == :remove ? "remove" : "add").bright} address #{address.bright} #{type != :remove ? "to" : "from"} interface #{self.config.interface.bright}.") if !self.config.quiet
188
+ @logger.info(@command.application.console.replace_markers("#{prefix} I will {mark=bright}#{(type == :remove ? "remove" : "add")}{/mark} address {mark=bright}#{address}{/mark} #{type != :remove ? "to" : "from"} interface {mark=bright}#{self.config.interface}{/mark}.")) if !self.config.quiet
195
189
  end
196
190
 
197
191
  rv
@@ -243,7 +237,7 @@ module Akaer
243
237
  launch_agent = self.launch_agent_path
244
238
 
245
239
  begin
246
- logger.info("Creating the launch agent in #{launch_agent} ...") if !self.config.quiet
240
+ logger.info("Creating the launch agent in {mark=bright}#{launch_agent}{/mark} ...") if !self.config.quiet
247
241
 
248
242
  args = $ARGV ? $ARGV[0, $ARGV.length - 1] : []
249
243
 
@@ -302,14 +296,13 @@ module Akaer
302
296
  end
303
297
 
304
298
  # Returns a unique (singleton) instance of the application.
305
- # @param globals [Hash] Global options.
306
- # @param locals [Hash] Local command options.
307
- # @param args [Array] Extra arguments.
299
+ #
300
+ # @param command [Mamertes::Command] The current Mamertes command.
308
301
  # @param force [Boolean] If to force recreation of the instance.
309
302
  # @return [Application] The unique (singleton) instance of the application.
310
- def self.instance(globals = {}, locals = {}, args = [], force = false)
303
+ def self.instance(command, force = false)
311
304
  @instance = nil if force
312
- @instance ||= Akaer::Application.new(globals, locals, args)
305
+ @instance ||= Akaer::Application.new(command)
313
306
  end
314
307
  end
315
308
  end
data/lib/akaer/version.rb CHANGED
@@ -13,7 +13,7 @@ module Akaer
13
13
  MAJOR = 1
14
14
 
15
15
  # The minor version.
16
- MINOR = 0
16
+ MINOR = 5
17
17
 
18
18
  # The patch version.
19
19
  PATCH = 0
data/lib/akaer.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
- require "bovem"
7
+ require "mamertes"
8
8
  require "ipaddr"
9
9
 
10
10
  require "akaer/version" if !defined?(Akaer::Version)
@@ -7,12 +7,29 @@
7
7
  require "spec_helper"
8
8
 
9
9
  describe Akaer::Application do
10
+ def create_application(overrides)
11
+ mamertes_app = Mamertes::App(:run => false) do
12
+ option "configuration", [], {:default => overrides["configuration"] || "~/.akaer_config"}
13
+ option "interface", [], {:default => overrides["interface"] || "lo0"}
14
+ option "addresses", [], {:type => Array, :default => overrides["addresses"] || []}
15
+ option "start-address", [], {:default => overrides["start-address"] || "10.0.0.1"}
16
+ option "aliases", [:S], {:type => Integer, :default => overrides["aliases"] || 5}
17
+ option "add-command", [:A], {:default => overrides["add-command"] || "sudo ifconfig @INTERFACE@ alias @ALIAS@"}
18
+ option "remove-command", [:R], {:default => overrides["remove-command"] || "sudo ifconfig @INTERFACE@ -alias @ALIAS@"}
19
+ option "log-file", [], {:default => overrides["log-file"] || "STDOUT"}
20
+ option "log-level", [:L], {:type => Integer, :default => overrides["log-level"] || 1}
21
+ option "dry-run", [:n], {:default => overrides["dry-run"] || false}
22
+ option "quiet", [], {:default => overrides["quiet"] || false}
23
+ end
24
+
25
+ Akaer::Application.new(mamertes_app)
26
+ end
27
+
10
28
  before(:each) do
11
29
  Bovem::Logger.stub(:default_file).and_return("/dev/null")
12
30
  end
13
-
14
31
  let(:log_file) { "/dev/null" }
15
- let(:application){ Akaer::Application.instance({:"log-file" => log_file}, true) }
32
+ let(:application){ create_application({"log-file" => log_file}) }
16
33
  let(:launch_agent_path) { "/tmp/akaer-test-agent-#{Time.now.strftime("%Y%m%d-%H%M%S")}" }
17
34
 
18
35
  describe "#initialize" do
@@ -30,7 +47,7 @@ describe Akaer::Application do
30
47
  file.write("config.aliases = ")
31
48
  file.close
32
49
 
33
- expect { Akaer::Application.new({:config => file.path, :"log-file" => log_file}) }.to raise_error(::SystemExit)
50
+ expect { create_application({"configuration" => file.path, "log-file" => log_file}) }.to raise_error(::SystemExit)
34
51
  ::File.unlink(path)
35
52
  end
36
53
  end
@@ -94,7 +111,7 @@ describe Akaer::Application do
94
111
 
95
112
  describe "#compute_addresses" do
96
113
  describe "should use only the explicit list if given" do
97
- let(:other_application){ Akaer::Application.new({:"log-file" => log_file, :addresses => ["10.0.0.1", "::1", "INVALID 1", "10.0.0.2", "INVALID 2", "2001:0db8:0::0:1428:57ab"]}) }
114
+ let(:other_application){ create_application({"log-file" => log_file, "addresses" => ["10.0.0.1", "::1", "INVALID 1", "10.0.0.2", "INVALID 2", "2001:0db8:0::0:1428:57ab"]}) }
98
115
 
99
116
  it "considering all address" do
100
117
  expect(other_application.compute_addresses).to eq(["10.0.0.1", "::1", "10.0.0.2", "2001:0db8:0::0:1428:57ab"])
@@ -102,28 +119,28 @@ describe Akaer::Application do
102
119
 
103
120
  it "considering only IPv4" do
104
121
  expect(other_application.compute_addresses(:ipv4)).to eq(["10.0.0.1", "10.0.0.2"])
105
- expect(Akaer::Application.new({:"log-file" => log_file, :addresses => ["::1", "INVALID 1"]}).compute_addresses(:ipv4)).to eq([])
122
+ expect(create_application({"log-file" => log_file, "addresses" => ["::1", "INVALID 1"]}).compute_addresses(:ipv4)).to eq([])
106
123
  end
107
124
 
108
125
  it "considering only IPv6" do
109
126
  expect(other_application.compute_addresses(:ipv6)).to eq(["::1", "2001:0db8:0::0:1428:57ab"])
110
- expect(Akaer::Application.new({:"log-file" => log_file, :addresses => ["10.0.0.1", "INVALID 1"]}).compute_addresses(:ipv6)).to eq([])
127
+ expect(create_application({"log-file" => log_file, "addresses" => ["10.0.0.1", "INVALID 1"]}).compute_addresses(:ipv6)).to eq([])
111
128
  end
112
129
  end
113
130
 
114
131
  describe "should compute a sequential list of address" do
115
132
  it "considering all address" do
116
- expect(Akaer::Application.new({:"log-file" => log_file, :"start-address" => "10.0.1.1"}).compute_addresses).to eq(["10.0.1.1", "10.0.1.2", "10.0.1.3", "10.0.1.4", "10.0.1.5"])
117
- expect(Akaer::Application.new({:"log-file" => log_file, :aliases => 3}).compute_addresses).to eq(["10.0.0.1", "10.0.0.2", "10.0.0.3"])
118
- expect(Akaer::Application.new({:"log-file" => log_file, :"start-address" => "10.0.1.1", :aliases => -1}).compute_addresses).to eq(["10.0.1.1", "10.0.1.2", "10.0.1.3", "10.0.1.4", "10.0.1.5"])
133
+ expect(create_application({"log-file" => log_file, "start-address" => "10.0.1.1"}).compute_addresses).to eq(["10.0.1.1", "10.0.1.2", "10.0.1.3", "10.0.1.4", "10.0.1.5"])
134
+ expect(create_application({"log-file" => log_file, "aliases" => 3}).compute_addresses).to eq(["10.0.0.1", "10.0.0.2", "10.0.0.3"])
135
+ expect(create_application({"log-file" => log_file, "start-address" => "10.0.1.1", :aliases => -1}).compute_addresses).to eq(["10.0.1.1", "10.0.1.2", "10.0.1.3", "10.0.1.4", "10.0.1.5"])
119
136
  end
120
137
 
121
138
  it "considering only IPv4" do
122
- expect(Akaer::Application.new({:"log-file" => log_file, :"start-address" => "::1"}).compute_addresses(:ipv4)).to eq([])
139
+ expect(create_application({"log-file" => log_file, "start-address" => "::1"}).compute_addresses(:ipv4)).to eq([])
123
140
  end
124
141
 
125
142
  it "considering only IPv6" do
126
- expect(Akaer::Application.new({:"log-file" => log_file, :"start-address" => "10.0.0.1"}).compute_addresses(:ipv6)).to eq([])
143
+ expect(create_application({"log-file" => log_file, "start-address" => "10.0.0.1"}).compute_addresses(:ipv6)).to eq([])
127
144
  end
128
145
  end
129
146
  end
@@ -153,7 +170,7 @@ describe Akaer::Application do
153
170
  end
154
171
 
155
172
  it "should return true if the command succeded" do
156
- other_application = Akaer::Application.new({:"add-command" => "echo @INTERFACE@", :quiet => true})
173
+ other_application = create_application({"add-command" => "echo @INTERFACE@", "quiet" => true})
157
174
  expect(other_application.manage(:add, "10.0.0.3")).to be_true
158
175
  end
159
176
 
@@ -162,7 +179,7 @@ describe Akaer::Application do
162
179
  end
163
180
 
164
181
  it "should respect dry-run mode" do
165
- other_application = Akaer::Application.new({:"log-file" => log_file, :"dry-run" => true})
182
+ other_application = create_application({"log-file" => log_file, "dry-run" => true})
166
183
 
167
184
  other_application.logger.should_receive(:info).with(/.+.*03.*\/.*05.*.+ I will .*add.* address .*10.0.0.3.* to interface .*lo0.*/)
168
185
  other_application.should_not_receive(:execute_command)
@@ -174,7 +191,6 @@ describe Akaer::Application do
174
191
  end
175
192
  end
176
193
 
177
-
178
194
  describe "#action_add" do
179
195
  it("should compute addresses to manage") do
180
196
  application.should_receive(:compute_addresses)
@@ -192,7 +208,7 @@ describe Akaer::Application do
192
208
 
193
209
  it("should show an error there's no address to manage") do
194
210
  application.stub(:compute_addresses).and_return([])
195
- other_application = Akaer::Application.new({:"log-file" => log_file, :quiet => true})
211
+ other_application = create_application({"log-file" => log_file, "quiet" => true})
196
212
  other_application.stub(:compute_addresses).and_return([])
197
213
 
198
214
  application.logger.should_receive(:error).with("No valid addresses to add to the interface found.")
@@ -219,7 +235,7 @@ describe Akaer::Application do
219
235
 
220
236
  it("should show an error there's no address to manage") do
221
237
  application.stub(:compute_addresses).and_return([])
222
- other_application = Akaer::Application.new({:"log-file" => log_file, :quiet => true})
238
+ other_application = create_application({"log-file" => log_file, "quiet" => true})
223
239
  other_application.stub(:compute_addresses).and_return([])
224
240
 
225
241
  application.logger.should_receive(:error).with("No valid addresses to remove from the interface found.")
@@ -4,10 +4,11 @@
4
4
  # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
5
  #
6
6
 
7
- require "simplecov"
8
7
  require "pathname"
9
8
 
10
- if ENV["AKAER_COVERAGE"] == "TRUE" then
9
+ if ENV["AKAER_COVERAGE"] == "TRUE" && RUBY_VERSION >= "1.9" then
10
+ require "simplecov"
11
+
11
12
  root = Pathname.new(File.dirname(__FILE__)) + ".."
12
13
 
13
14
  SimpleCov.start do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akaer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,16 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-06 00:00:00.000000000 Z
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: bovem
15
+ name: mamertes
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.5.0
21
+ version: 1.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,23 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.5.0
30
- - !ruby/object:Gem::Dependency
31
- name: gli
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: 1.6.0
38
- type: :runtime
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 1.6.0
29
+ version: 1.1.0
46
30
  - !ruby/object:Gem::Dependency
47
31
  name: rspec
48
32
  requirement: !ruby/object:Gem::Requirement
@@ -172,6 +156,28 @@ files:
172
156
  - Rakefile
173
157
  - akaer.gemspec
174
158
  - bin/akaer
159
+ - doc/Akaer.html
160
+ - doc/Akaer/Application.html
161
+ - doc/Akaer/Configuration.html
162
+ - doc/Akaer/Errors.html
163
+ - doc/Akaer/Errors/InvalidConfiguration.html
164
+ - doc/Akaer/Logger.html
165
+ - doc/Akaer/Version.html
166
+ - doc/Fixnum.html
167
+ - doc/_index.html
168
+ - doc/class_list.html
169
+ - doc/css/common.css
170
+ - doc/css/full_list.css
171
+ - doc/css/style.css
172
+ - doc/file.README.html
173
+ - doc/file_list.html
174
+ - doc/frames.html
175
+ - doc/index.html
176
+ - doc/js/app.js
177
+ - doc/js/full_list.js
178
+ - doc/js/jquery.js
179
+ - doc/method_list.html
180
+ - doc/top-level-namespace.html
175
181
  - lib/akaer.rb
176
182
  - lib/akaer/application.rb
177
183
  - lib/akaer/configuration.rb
@@ -194,7 +200,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
194
200
  version: '0'
195
201
  segments:
196
202
  - 0
197
- hash: -4081054337891207421
203
+ hash: -2611377790548200103
198
204
  required_rubygems_version: !ruby/object:Gem::Requirement
199
205
  none: false
200
206
  requirements:
@@ -203,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
209
  version: '0'
204
210
  segments:
205
211
  - 0
206
- hash: -4081054337891207421
212
+ hash: -2611377790548200103
207
213
  requirements: []
208
214
  rubyforge_project: akaer
209
215
  rubygems_version: 1.8.24