Class: RVC::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/rvc/shell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Shell

Returns a new instance of Shell.



27
28
29
30
31
32
33
34
# File 'lib/rvc/shell.rb', line 27

def initialize session
  @session = session
  @persist_ruby = false
  @fs = RVC::FS.new RVC::RootNode.new
  @ruby_evaluator = RubyEvaluator.new @fs
  @connections = {}
  @debug = false
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



24
25
26
# File 'lib/rvc/shell.rb', line 24

def connections
  @connections
end

#debugObject

Returns the value of attribute debug.



25
26
27
# File 'lib/rvc/shell.rb', line 25

def debug
  @debug
end

#fsObject (readonly)

Returns the value of attribute fs.



24
25
26
# File 'lib/rvc/shell.rb', line 24

def fs
  @fs
end

#sessionObject (readonly)

Returns the value of attribute session.



24
25
26
# File 'lib/rvc/shell.rb', line 24

def session
  @session
end

Instance Method Details

#delete_numeric_marksObject



184
185
186
# File 'lib/rvc/shell.rb', line 184

def delete_numeric_marks
  @session.marks.grep(/^\d+$/).each { |x| @session.set_mark x, nil }
end

#eval_command(input) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rvc/shell.rb', line 79

def eval_command input
  cmd, *args = Shellwords.shellwords(input)
  return unless cmd
  RVC::Util.err "invalid command" unless cmd.is_a? String
  case cmd
  when RVC::FS::MARK_PATTERN
    CMD.basic.cd RVC::Util.lookup_single(cmd)
  else
    if cmd.include? '.'
      module_name, cmd, = cmd.split '.'
    elsif ALIASES.member? cmd
      module_name, cmd, = ALIASES[cmd].split '.'
    else
      RVC::Util.err "unknown alias #{cmd}"
    end

    m = MODULES[module_name] or RVC::Util.err("unknown module #{module_name}")

    opts_block = m.opts_for(cmd.to_sym)
    parser = RVC::OptionParser.new cmd, &opts_block

    begin
      args, opts = parser.parse args
    rescue Trollop::HelpNeeded
      parser.educate
      return
    end

    if parser.has_options?
      m.send cmd.to_sym, *(args + [opts])
    else
      m.send cmd.to_sym, *args
    end
  end
  nil
end

#eval_input(input) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rvc/shell.rb', line 36

def eval_input input
  if input == '//'
    @persist_ruby = !@persist_ruby
    return
  end

  if input[0..0] == '!'
    RVC::Util.system_fg input[1..-1]
    return
  end

  ruby = @persist_ruby
  if input =~ /^\//
    input = $'
    ruby = !ruby
  end

  begin
    if ruby
      eval_ruby input
    else
      eval_command input
    end
  rescue SystemExit, IOError
    raise
  rescue RVC::Util::UserError, RuntimeError, RbVmomi::Fault
    if ruby or debug
      puts "#{$!.class}: #{$!.message}"
      puts $!.backtrace * "\n"
    else
      case $!
      when RbVmomi::Fault, RVC::Util::UserError
        puts $!.message
      else
        puts "#{$!.class}: #{$!.message}"
      end
    end
  rescue Exception
    puts "#{$!.class}: #{$!.message}"
    puts $!.backtrace * "\n"
  end
end

#eval_ruby(input) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/rvc/shell.rb', line 116

def eval_ruby input
  result = @ruby_evaluator.do_eval input
  if input =~ /\#$/
    introspect_object result
  else
    pp result
  end
  nil
end

#introspect_class(klass) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rvc/shell.rb', line 159

def introspect_class klass
  q = lambda { |x| x =~ /^xsd:/ ? $' : x }
  if klass < RbVmomi::VIM::DataObject
    puts "Data Object #{klass}"
    klass.full_props_desc.each do |desc|
      puts " #{desc['name']}: #{q[desc['wsdl_type']]}#{desc['is-array'] ? '[]' : ''}"
    end
  elsif klass < RbVmomi::VIM::ManagedObject
    puts "Managed Object #{klass}"
    puts
    puts "Properties:"
    klass.full_props_desc.each do |desc|
      puts " #{desc['name']}: #{q[desc['wsdl_type']]}#{desc['is-array'] ? '[]' : ''}"
    end
    puts
    puts "Methods:"
    klass.full_methods_desc.sort_by(&:first).each do |name,desc|
      params = desc['params']
      puts " #{name}(#{params.map { |x| "#{x['name']} : #{q[x['wsdl_type'] || 'void']}#{x['is-array'] ? '[]' : ''}" } * ', '}) : #{q[desc['result']['wsdl_type'] || 'void']}"
    end
  else
    puts klass
  end
end

#introspect_object(obj) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rvc/shell.rb', line 134

def introspect_object obj
  case obj
  when RbVmomi::VIM::DataObject, RbVmomi::VIM::ManagedObject
    introspect_class obj.class
  when Array
    klasses = obj.map(&:class).uniq
    if klasses.size == 0
      puts "Array"
    elsif klasses.size == 1
      $stdout.write "Array of "
      introspect_class klasses[0]
    else
      counts = Hash.new 0
      obj.each { |o| counts[o.class] += 1 }
      puts "Array of:"
      counts.each { |k,c| puts "  #{k}: #{c}" }
      puts
      $stdout.write "Common ancestor: "
      introspect_class klasses.map(&:ancestors).inject(&:&)[0]
    end
  else
    puts obj.class
  end
end

#promptObject



126
127
128
129
130
131
132
# File 'lib/rvc/shell.rb', line 126

def prompt
  if false
    "#{@fs.display_path}#{$terminal.color(@persist_ruby ? '~' : '>', :yellow)} "
  else
    "#{@fs.display_path}#{@persist_ruby ? '~' : '>'} "
  end
end