Class: Nagi::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/nagi/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlugin

Returns a new instance of Plugin.



5
6
7
# File 'lib/nagi/plugin.rb', line 5

def initialize
  @optionparser = Nagi::OptionParser.new
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



3
4
5
# File 'lib/nagi/plugin.rb', line 3

def fallback
  @fallback
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/nagi/plugin.rb', line 3

def name
  @name
end

#optionparserObject

Returns the value of attribute optionparser.



3
4
5
# File 'lib/nagi/plugin.rb', line 3

def optionparser
  @optionparser
end

#prefixObject

Returns the value of attribute prefix.



3
4
5
# File 'lib/nagi/plugin.rb', line 3

def prefix
  @prefix
end

#versionObject

Returns the value of attribute version.



3
4
5
# File 'lib/nagi/plugin.rb', line 3

def version
  @version
end

Instance Method Details

#check(options) ⇒ Object

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/nagi/plugin.rb', line 9

def check(options)
  raise NotImplementedError.new('No check defined')
end

#run(args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nagi/plugin.rb', line 18

def run(args)
  options = @optionparser.parse(args)
  begin
    status = self.check(options)
  rescue StandardError => e
    status = Nagi::Status::Unknown.new(e.message)
  end
  unless status.is_a?Nagi::Status::Status
  status = @fallback || Nagi::Status::Unknown.new('Check did not provide a status')
  end
  return status
end

#run!Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nagi/plugin.rb', line 31

def run!
  begin
    status = run(ARGV)
    puts "#{@prefix.upcase if @prefix} #{status}".strip
    exit status.code
  rescue ArgumentError => e
    STDERR.puts("Error: #{e.message}")
    puts ""
    puts @optionparser
    exit 4
  end
end