class Zabby::Runner

This is the main interpreter. Additional shell commands are defined in the ShellHelpers module.

Attributes

config[R]
connection[R]

Public Class Methods

new() click to toggle source
# File lib/rbZabbix/runner.rb, line 22
def initialize
  @config = Zabby::Config.new
  @connection = Zabby::Connection.new
  @pure_binding = instance_eval "binding"

  # Configure Readline for the shell, if available
  if Object.const_defined?(:Readline)
    @readline = true
    Readline.basic_word_break_characters = ""
                      Readline.completion_append_character = nil
                      #Readline.completion_proc = completion_proc

                      $last_res = nil
  else
    # Without Readline the Zabby shell is not available.
    @readline = false
  end
end

Public Instance Methods

run(command_file = nil, &block) click to toggle source

Execute script file and/or instruction block @param [String] command_file Filename containing commands to execute. @param [Proc] block A block containing commands to execute.

# File lib/rbZabbix/runner.rb, line 44
def run(command_file = nil, &block)
  unless command_file.nil?
    commands = File.read(command_file)
    instance_eval(commands, command_file, 1)
  end
  instance_eval(&block) if block_given?
end
shell() click to toggle source

Execute an irb like shell in which we can type Zabby commands.

# File lib/rbZabbix/runner.rb, line 53
    def shell
      raise RuntimeError.new("Shell cannot run because 'readline' is missing.") if !@readline

      puts <<HEADER
Zabby Shell #{Zabby::VERSION}

** This is a simple irb like Zabbix Shell. Multiline commands do not work for e.g. **
Type "help" for online documentation.
HEADER
      loop do
        cmd = Readline.readline('zabby> ')

        exit(0) if cmd.nil? or cmd == 'exit'
        next if cmd == ""
        Readline::HISTORY.push(cmd)

        execute(cmd)
      end
    end

Private Instance Methods

execute(cmd) click to toggle source

Run a single command. @param [String] cmd A command to execute in the object’s context.

# File lib/rbZabbix/runner.rb, line 77
def execute(cmd)
  res = eval(cmd, @pure_binding)
  $last_res = res
  eval("_ = $last_res", @pure_binding)
  print_result res
rescue ::Exception => e
  puts "Exception #{e.class} -> #{e.message}"
  e.backtrace.each do |t|
    puts "   #{::File.expand_path(t)}"
  end
end
print_result(res) click to toggle source

Print a single command’s output on the screen using “inspect” if necessary. @param [Object] res Result object to display