Class: RVC::Shell

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

Instance Attribute Summary collapse

Class Method 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

Class Method Details

.parse_input(input) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/rvc/shell.rb', line 81

def self.parse_input input
  cmd, *args = Shellwords.shellwords(input)
  return nil unless cmd
  if cmd.include? '.'
    module_name, cmd, = cmd.split '.'
  elsif ALIASES.member? cmd
    module_name, cmd, = ALIASES[cmd].split '.'
  end
  [MODULES[module_name], cmd.to_sym, args]
end

Instance Method Details

#delete_numeric_marksObject



186
187
188
# File 'lib/rvc/shell.rb', line 186

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

#eval_command(input) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rvc/shell.rb', line 92

def eval_command input
  m, cmd, args = Shell.parse_input input
  RVC::Util.err "invalid command" unless m != nil and
                                         cmd.is_a? Symbol and
                                         m.has_command? cmd
  parser = m.opts_for(cmd)

  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

#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
78
79
# 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, Trollop::CommandlineError
    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"
  ensure
    $stdout.flush
  end
end

#eval_ruby(input, file = "<input>") ⇒ Object



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

def eval_ruby input, file="<input>"
  result = @ruby_evaluator.do_eval input, file
  if $interactive
    if input =~ /\#$/
      if result.is_a? Class
        introspect_class result
      else
        introspect_object result
      end
    else
      pp result
    end
  end
end

#introspect_class(klass) ⇒ Object



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

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



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

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



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

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