class RGovData::Shell

Constants

OPTIONS

command line options definition

Attributes

discovery_path[RW]
options[RW]
prompt[RW]

Public Class Methods

new(options) click to toggle source

new

# File lib/rgovdata/shell/shell.rb, line 40
def initialize(options)
  require_config_file
  @options = (options||{}).each{|k,v| {k => v} }
  @discovery_path = [config.default_realm].compact
end
usage() click to toggle source

Usage message

# File lib/rgovdata/shell/shell.rb, line 9
  def self.usage
    puts <<-EOS

rgovdata client v#{RGovData::Version::STRING}
===================================

Usage:
  rgd [options]

Command Options
  -c | --command    immediately executes the command provided

The following commands are supported in the rgovdata shell.
They can also be passed on the command line:

  rgd -c command
  
  == Core Commands ==
  help
  exit
  show status

  == Discovery Commands ==
  ls
  cd
  info

    EOS
  end

Public Instance Methods

run() click to toggle source

run the basic REPL

# File lib/rgovdata/shell/shell.rb, line 47
def run
  if options[:command].present?
    evaluate options[:command].join(' ')
    return
  end
  welcome
  repl = lambda do |prompt|
    print prompt
    evaluate( STDIN.gets.chomp! )
  end
  loop { break unless repl[prompt] }
end

Protected Instance Methods

cd(args=[]) click to toggle source

handle cd command

# File lib/rgovdata/shell/shell.rb, line 145
def cd(args=[])
  cmd = args.shift || "ls"
  case cmd
  when '..'
    discovery_path.pop
  when /^ls$/i
    ls
  else
    discovery_path.push(*cmd.split('/'))
  end
end
current_object() click to toggle source

Returns the RGovData object corresponding to the current path

# File lib/rgovdata/shell/shell.rb, line 71
def current_object
  RGovData::Catalog.get(current_path)
rescue
  STDERR.puts "ERROR: there is no object matching #{current_path}"
  nil
end
current_path() click to toggle source

Returns the current path string

# File lib/rgovdata/shell/shell.rb, line 67
def current_path
  "//#{discovery_path.join('/')}"
end
evaluate(cmd) click to toggle source

Evaluates a specific command

# File lib/rgovdata/shell/shell.rb, line 79
def evaluate(cmd)
  if cmd =~ /exit/i
    puts "bfn!\n\n"
    return false
  end
  begin
    tokens = cmd.strip.split
    return true unless tokens.size>0
    self.send tokens.shift.downcase, tokens
  rescue => e
    puts "sorry, I have a problem doing \"#{cmd}\"\nerror => #{e}"
    puts e.backtrace if options[:trace]
  end
  return true
end
help(*args) click to toggle source

Print usage details

# File lib/rgovdata/shell/shell.rb, line 96
def help(*args)
  self.class.usage
end
ls(args=[]) click to toggle source

handle ls command

# File lib/rgovdata/shell/shell.rb, line 133
def ls(args=[])
  rgd_object = current_object
  if rgd_object
    puts "Current node: #{current_object}"
    puts "Records:"
    current_object.records.each do |record|
      puts record
    end
  end
end
show(args=[]) click to toggle source

handle show command

# File lib/rgovdata/shell/shell.rb, line 113
def show(args=[])
  cmd = args.shift || "help"
  
  case cmd
  when /^set/i
    set(args)
  when /^status?$/i
    status(args)
  else
    help
  end
end
show_status() click to toggle source

Prints current status

# File lib/rgovdata/shell/shell.rb, line 108
def show_status
  puts "shell options: #{options.inspect}"
end
status(args=[]) click to toggle source

handle status command

# File lib/rgovdata/shell/shell.rb, line 127
def status(args=[])
  show_status
  config.show_status
end
welcome(*args) click to toggle source

Print welcome message

# File lib/rgovdata/shell/shell.rb, line 101
  def welcome(*args)
    puts <<-EOS
rgovdata client v#{RGovData::Version::STRING}. Type 'help' for info...
    EOS
  end