Class: RVC::Shell
- Inherits:
-
Object
- Object
- RVC::Shell
- Defined in:
- lib/rvc/shell.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#fs ⇒ Object
readonly
Returns the value of attribute fs.
Instance Method Summary collapse
- #eval_command(input) ⇒ Object
- #eval_input(input) ⇒ Object
- #eval_ruby(input) ⇒ Object
-
#initialize ⇒ Shell
constructor
A new instance of Shell.
- #introspect_class(klass) ⇒ Object
- #introspect_object(obj) ⇒ Object
- #prompt ⇒ Object
Constructor Details
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
24 25 26 |
# File 'lib/rvc/shell.rb', line 24 def connections @connections end |
#debug ⇒ Object
Returns the value of attribute debug.
25 26 27 |
# File 'lib/rvc/shell.rb', line 25 def debug @debug end |
#fs ⇒ Object (readonly)
Returns the value of attribute fs.
24 25 26 |
# File 'lib/rvc/shell.rb', line 24 def fs @fs end |
Instance Method Details
#eval_command(input) ⇒ Object
78 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 |
# File 'lib/rvc/shell.rb', line 78 def eval_command input cmd, *args = Shellwords.shellwords(input) return unless cmd err "invalid command" unless cmd.is_a? String case cmd when RVC::FS::MARK_PATTERN CMD.basic.cd lookup_single(cmd) else if cmd.include? '.' module_name, cmd, = cmd.split '.' elsif ALIASES.member? cmd module_name, cmd, = ALIASES[cmd].split '.' else err "unknown alias #{cmd}" end m = MODULES[module_name] or 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. m.send cmd.to_sym, *(args + [opts]) else m.send cmd.to_sym, *args end end nil end |
#eval_input(input) ⇒ Object
35 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 |
# File 'lib/rvc/shell.rb', line 35 def eval_input input if input == '//' @persist_ruby = !@persist_ruby return end if input[0..0] == '!' 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 UserError, RuntimeError, RbVmomi::Fault if ruby or debug puts "#{$!.class}: #{$!.}" puts $!.backtrace * "\n" else case $! when RbVmomi::Fault, UserError puts $!. else puts "#{$!.class}: #{$!.}" end end rescue Exception puts "#{$!.class}: #{$!.}" puts $!.backtrace * "\n" end end |
#eval_ruby(input) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/rvc/shell.rb', line 115 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
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rvc/shell.rb', line 154 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rvc/shell.rb', line 129 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 |
#prompt ⇒ Object
125 126 127 |
# File 'lib/rvc/shell.rb', line 125 def prompt "#{@fs.display_path}#{$terminal.color(@persist_ruby ? '~' : '>', :yellow)} " end |