nagios_check 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,7 +38,7 @@ require "nagios_check"
38
38
  class SimpleCheck
39
39
  include NagiosCheck
40
40
 
41
- on "-H HOST", :required, &store(:host)
41
+ on "-H HOST", :mandatory, &store(:host)
42
42
  on "-P PORT", &store(:port, default: 8080, transform: :to_i)
43
43
 
44
44
  enable_warning
data/Rakefile CHANGED
@@ -1 +1,4 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:rspec)
data/lib/nagios_check.rb CHANGED
@@ -9,6 +9,7 @@ module NagiosCheck
9
9
  attr_reader :options
10
10
  attr_accessor :message
11
11
 
12
+
12
13
  def prepare
13
14
  @values = {}
14
15
  self.message = nil
@@ -62,7 +63,8 @@ module NagiosCheck
62
63
 
63
64
  module ClassMethods
64
65
  def on(*args, &block)
65
- @ons << [args, args.delete(:mandatory), block]
66
+ mandatory = args.delete(:mandatory)
67
+ @option_specs[option_name(args.first)] = [args, mandatory, block]
66
68
  end
67
69
 
68
70
  def store(name, opts = {})
@@ -76,7 +78,7 @@ module NagiosCheck
76
78
 
77
79
  def defaults
78
80
  @defaults
79
- end
81
+ end
80
82
 
81
83
  def enable_warning(*args)
82
84
  on("-w RANGE", *args) do |value|
@@ -95,12 +97,34 @@ module NagiosCheck
95
97
  @timeout = value.to_f
96
98
  end
97
99
  end
100
+
101
+ def check_options!(options)
102
+ @option_specs.each do |name, spec|
103
+ if spec[1] == :mandatory && options.send(name).nil?
104
+ raise MissingOption.new(name)
105
+ end
106
+ end
107
+ end
108
+
109
+ private
110
+
111
+ def option_name(arg)
112
+ if arg =~ /^--\[no-\]([^-\s][^\s]*)/
113
+ $1.to_sym
114
+ elsif arg =~ /^--([^-\s][^\s]*)/
115
+ $1.to_sym
116
+ elsif arg =~ /^-([^-\s][^\s]*)/
117
+ $1.to_sym
118
+ else
119
+ raise "Unable to parse option '#{arg}'"
120
+ end
121
+ end
98
122
  end
99
123
 
100
124
  def self.included(base)
101
125
  base.extend(ClassMethods)
102
126
  base.instance_eval do
103
- @ons = []
127
+ @option_specs = {}
104
128
  @defaults = {}
105
129
  end
106
130
  end
@@ -110,7 +134,8 @@ module NagiosCheck
110
134
  def opt_parse
111
135
  unless @opt_parse
112
136
  opt_parse = OptionParser::new
113
- self.class.instance_variable_get("@ons").each do |args, mand, block|
137
+ self.class.instance_variable_get("@option_specs").each do |name, spec|
138
+ args, mand, block = spec
114
139
  opt_parse.on(*args) do |value|
115
140
  instance_exec(value, &block)
116
141
  end
@@ -123,6 +148,7 @@ module NagiosCheck
123
148
  def parse_options(argv = ARGV)
124
149
  @options = OpenStruct::new(self.class.defaults)
125
150
  opt_parse.parse!(argv)
151
+ self.class.check_options!(@options)
126
152
  end
127
153
 
128
154
 
@@ -130,6 +156,6 @@ module NagiosCheck
130
156
  Timeout.timeout(@timeout) { check }
131
157
  end
132
158
 
133
- class MissingOption < StandardError
134
- end
159
+ class MissingOption < StandardError; end
160
+ class MissingOption < StandardError; end
135
161
  end
@@ -1,3 +1,3 @@
1
1
  module NagiosCheck
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/nagios_check.gemspec CHANGED
@@ -17,4 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec", ">= 2.0.0"
20
22
  end
data/spec/options_spec.rb CHANGED
@@ -89,4 +89,21 @@ describe NagiosCheck do
89
89
  }.should raise_error(OptionParser::InvalidOption)
90
90
  end
91
91
  end
92
+
93
+ context "when a mandatory option is specified" do
94
+ subject do
95
+ Class::new do
96
+ include NagiosCheck
97
+ on "-a VALUE", :mandatory, &store(:a)
98
+ end.new
99
+ end
100
+
101
+ it "fails if -a is not given" do
102
+ lambda {
103
+ subject.send :parse_options, %w{}
104
+ }.should raise_error(NagiosCheck::MissingOption)
105
+ end
106
+
107
+ specify { subject.send :parse_options, %w{-a foo} }
108
+ end
92
109
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nagios_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-30 00:00:00.000000000Z
13
- dependencies: []
12
+ date: 2011-12-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2152787360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152787360
14
25
  description: An easy to use DSL for building custom probes for the Nagios monitoring
15
26
  system
16
27
  email: