lib/virtualbox/hard_drive.rb in virtualbox-0.4.2 vs lib/virtualbox/hard_drive.rb in virtualbox-0.4.3

- old
+ new

@@ -82,34 +82,42 @@ class <<self # Returns an array of all available hard drives as HardDrive # objects. # + # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException} + # will be raised if the command failed. # @return [Array<HardDrive>] - def all - raw = Command.vboxmanage("list hdds") - parse_blocks(raw).collect { |v| find(v[:uuid]) } + def all(raise_errors=false) + raw = Command.vboxmanage("list", "hdds") + parse_blocks(raw).collect { |v| find(v[:uuid], raise_errors) } + rescue Exceptions::CommandFailedException + raise if raise_errors + false end # Finds one specific hard drive by UUID or file name. If the # hard drive can not be found, will return `nil`. # + # @param [String] id The UUID or name of the hard drive + # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException} + # will be raised if the command failed. # @return [HardDrive] - def find(id) - raw = Command.vboxmanage("showhdinfo #{id}") + def find(id, raise_errors=false) + raw = Command.vboxmanage("showhdinfo", id) - # Return nil if the hard drive doesn't exist - return nil if raw =~ /VERR_FILE_NOT_FOUND/ - data = raw.split(/\n\n/).collect { |v| parse_block(v) }.find { |v| !v.nil? } # Set equivalent fields data[:format] = data[:"storage format"] data[:size] = data[:"logical size"].split(/\s+/)[0] if data.has_key?(:"logical size") # Return new object new(data) + rescue Exceptions::CommandFailedException + raise if raise_errors + nil end end # Clone hard drive, possibly also converting formats. All formats # supported by your local VirtualBox installation are supported @@ -122,11 +130,11 @@ # @param [String] format The format to convert to. # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException} # will be raised if the command failed. # @return [HardDrive] The new, cloned hard drive, or nil on failure. def clone(outputfile, format="VDI", raise_errors=false) - raw = Command.vboxmanage("clonehd #{uuid} #{Command.shell_escape(outputfile)} --format #{format} --remember") + raw = Command.vboxmanage("clonehd", uuid, outputfile, "--format", format, "--remember") return nil unless raw =~ /UUID: (.+?)$/ self.class.find($1.to_s) rescue Exceptions::CommandFailedException raise if raise_errors @@ -157,11 +165,11 @@ if !valid? raise Exceptions::ValidationFailedException.new(errors) if raise_errors return false end - raw = Command.vboxmanage("createhd --filename #{location} --size #{size} --format #{read_attribute(:format)} --remember") + raw = Command.vboxmanage("createhd", "--filename", location, "--size", size, "--format", read_attribute(:format), "--remember") return nil unless raw =~ /UUID: (.+?)$/ # Just replace our attributes with the newly created ones. This also # will set new_record to false. populate_attributes(self.class.find($1.to_s).attributes) @@ -199,10 +207,10 @@ # # @param [Boolean] raise_errors If true, {Exceptions::CommandFailedException} # will be raised if the command failed. # @return [Boolean] True if command was successful, false otherwise. def destroy(raise_errors=false) - Command.vboxmanage("closemedium disk #{uuid} --delete") + Command.vboxmanage("closemedium", "disk", uuid, "--delete") true rescue Exceptions::CommandFailedException raise if raise_errors false end \ No newline at end of file