Class: VirtualBox::Command
- Inherits:
-
Object
- Object
- VirtualBox::Command
- Defined in:
- lib/virtualbox/command.rb
Overview
Used by the rest of the virtualbox library to call shell commands. It also can be used to change the path for your VBoxManage program.
Changing VBoxManage Path
The rest of the library won't work without a proper path to VBoxManage,
so it is crucial to set this properly right away. By default its set
to VBoxManage
which assumes that it is in your PATH
.
VirtualBox::Command.vboxmanage = "/opt/local/bin/VBoxManage"
Constant Summary
- @@vboxmanage =
"VBoxManage"
Class Method Summary
- + (Object) execute(command) Runs a command and returns the STDOUT result.
- + (Nokogiri::XML::Document) parse_xml(filename) Reads the XML file and returns a Nokogiri document.
- + (Object) shell_escape(str) Shell escapes a string.
- + (Boolean) success? Returns true if the last run command was a success.
- + (Object) test(command) Runs a command and returns a boolean result showing if the command ran successfully or not based on the exit code.
- + (String) vboxmanage(*args) Runs a VBoxManage command and returns the output.
- + (Object) vboxmanage(path) Sets the path to VBoxManage, which is required for this gem to work.
Class Method Details
+ (Object) execute(command)
Runs a command and returns the STDOUT result. The reason this is a method at the moment is because in the future we may want to change the way commands are run (replace the backticks), plus it makes testing easier.
74 75 76 |
# File 'lib/virtualbox/command.rb', line 74 def execute(command) `#{command}` end |
+ (Nokogiri::XML::Document) parse_xml(filename)
Reads the XML file and returns a Nokogiri document. Reads the XML data from the specified file and returns a Nokogiri document.
22 23 24 25 26 27 28 |
# File 'lib/virtualbox/command.rb', line 22 def parse_xml(filename) f = File.open(filename, "r") result = Nokogiri::XML(f) f.close result end |
+ (Object) shell_escape(str)
Shell escapes a string. This is almost a direct copy/paste from the ruby mailing list. I'm not sure how well it works but so far it hasn't failed!
81 82 83 84 85 |
# File 'lib/virtualbox/command.rb', line 81 def shell_escape(str) str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\'). gsub(/\n/, "'\n'"). sub(/^$/, "''") end |
+ (Boolean) success?
Returns true if the last run command was a success. Obviously this will introduce all sorts of thread-safe problems. Those will have to be addressed another time.
33 34 35 |
# File 'lib/virtualbox/command.rb', line 33 def success? $?.to_i == 0 end |
+ (Object) test(command)
Runs a command and returns a boolean result showing if the command ran successfully or not based on the exit code.
65 66 67 68 |
# File 'lib/virtualbox/command.rb', line 65 def test(command) execute(command) success? end |
+ (String) vboxmanage(*args)
Runs a VBoxManage command and returns the output. This method will automatically shell escape all args passed to it. There is no way to avoid this at the moment (since it hasn't been necessary to). This will raise an Exceptions::CommandFailedException if the exit status of the command is nonzero. It is up to the caller to figure out how to handle this; there is no way to suppress it via a parameter to this call.
Upon success, vboxmanage returns the stdout output from the command.
55 56 57 58 59 60 |
# File 'lib/virtualbox/command.rb', line 55 def vboxmanage(*args) args.collect! { |arg| shell_escape(arg.to_s) } result = execute("#{@@vboxmanage} -q #{args.join(" ")}") raise Exceptions::CommandFailedException.new(result) if !Command.success? result end |
+ (Object) vboxmanage=(path)
Sets the path to VBoxManage, which is required for this gem to work.
41 42 43 |
# File 'lib/virtualbox/command.rb', line 41 def vboxmanage=(path) @@vboxmanage = path end |