Class: RVC::Shell
- Inherits:
-
Object
- Object
- RVC::Shell
- Defined in:
- lib/rvc/shell.rb
Constant Summary collapse
- BULTIN_MODULE_PATH =
[File.(File.join(File.dirname(__FILE__), 'modules')), File.join(ENV['HOME'], ".rvc")]
- ENV_MODULE_PATH =
(ENV['RVC_MODULE_PATH'] || '').split ':'
Instance Attribute Summary collapse
-
#cmds ⇒ Object
Returns the value of attribute cmds.
-
#completion ⇒ Object
readonly
Returns the value of attribute completion.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#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.
Class Method Summary collapse
Instance Method Summary collapse
- #eval_command(input) ⇒ Object
- #eval_input(input) ⇒ Object
- #eval_ruby(input, file = "<input>") ⇒ Object
-
#initialize ⇒ Shell
constructor
A new instance of Shell.
- #inspect ⇒ Object
- #introspect_class(klass) ⇒ Object
- #introspect_object(obj) ⇒ Object
- #prompt ⇒ Object
- #reload_modules(verbose = true) ⇒ Object
- #switch_connection(name) ⇒ Object
Constructor Details
#initialize ⇒ Shell
Returns a new instance of Shell.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rvc/shell.rb', line 33 def initialize @persist_ruby = false @fs = RVC::FS.new RVC::RootNode.new(self) @ruby_evaluator = RVC::RubyEvaluator.new self @completion = RVC::Completion.new self @connection = NullConnection.new @connections = { '' => @connection } @debug = false @cmds = nil end |
Instance Attribute Details
#cmds ⇒ Object
Returns the value of attribute cmds.
31 32 33 |
# File 'lib/rvc/shell.rb', line 31 def cmds @cmds end |
#completion ⇒ Object (readonly)
Returns the value of attribute completion.
29 30 31 |
# File 'lib/rvc/shell.rb', line 29 def completion @completion end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
30 31 32 |
# File 'lib/rvc/shell.rb', line 30 def connection @connection end |
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
30 31 32 |
# File 'lib/rvc/shell.rb', line 30 def connections @connections end |
#debug ⇒ Object
Returns the value of attribute debug.
31 32 33 |
# File 'lib/rvc/shell.rb', line 31 def debug @debug end |
#fs ⇒ Object (readonly)
Returns the value of attribute fs.
29 30 31 |
# File 'lib/rvc/shell.rb', line 29 def fs @fs end |
Class Method Details
.parse_input(input) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rvc/shell.rb', line 97 def self.parse_input input begin cmdpath, *args = Shellwords.shellwords(input) rescue ArgumentError # unmatched double quote cmdpath, *args = Shellwords.shellwords(input + '"') end return nil unless cmdpath cmdpath = cmdpath.split('.').map(&:to_sym) [cmdpath, args] end |
Instance Method Details
#eval_command(input) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/rvc/shell.rb', line 108 def eval_command input cmdpath, args = Shell.parse_input input RVC::Util.err "invalid input" unless cmdpath cmd = cmds.lookup cmdpath RVC::Util.err "invalid command" unless cmd begin args, opts = cmd.parser.parse args rescue Trollop::HelpNeeded cmd.parser.educate return end if cmd.parser. cmd.invoke *(args + [opts]) else cmd.invoke *args end end |
#eval_input(input) ⇒ Object
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rvc/shell.rb', line 52 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}: #{$!.}" puts $!.backtrace * "\n" else case $! when RbVmomi::Fault, RVC::Util::UserError puts $!. else puts "#{$!.class}: #{$!.}" end end rescue Exception puts "#{$!.class}: #{$!.}" puts $!.backtrace * "\n" ensure $stdout.flush end end |
#eval_ruby(input, file = "<input>") ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rvc/shell.rb', line 130 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 |
#inspect ⇒ Object
48 49 50 |
# File 'lib/rvc/shell.rb', line 48 def inspect "#<RVC::Shell:#{object_id}>" end |
#introspect_class(klass) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/rvc/shell.rb', line 178 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} < #{klass.superclass}" puts methods_by_class = klass.instance_methods.map { |x| klass.instance_method(x) }.group_by { |m| m.owner } klass.ancestors.each do |ancestor| break if ancestor == Object if ancestor == klass puts "Methods:" else puts "Methods from #{ancestor}:" end methods_by_class[ancestor].sort_by { |m| m.name }.each do |m| if m.respond_to? :parameters puts " #{m.name}(#{m.parameters.map { |mode,name| "#{name}#{mode==:opt ? '?' : ''}" } * ', '})" else puts " #{m.name}" end end end end end |
#introspect_object(obj) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rvc/shell.rb', line 153 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 introspect_class obj.class end end |
#prompt ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/rvc/shell.rb', line 145 def prompt if false "#{@fs.display_path}#{$terminal.color(@persist_ruby ? '~' : '>', :yellow)} " else "#{@fs.display_path}#{@persist_ruby ? '~' : '>'} " end end |
#reload_modules(verbose = true) ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/rvc/shell.rb', line 224 def reload_modules verbose=true @cmds = RVC::Namespace.new 'root', self, nil module_path = (BULTIN_MODULE_PATH+ENV_MODULE_PATH).select { |d| File.directory?(d) } module_path.each do |dir| @cmds.load_module_dir dir, verbose end end |
#switch_connection(name) ⇒ Object
44 45 46 |
# File 'lib/rvc/shell.rb', line 44 def switch_connection name @connection = @connections[name] || fail("no such connection") end |