lib/mellon/utils.rb in mellon-1.1.1 vs lib/mellon/utils.rb in mellon-1.2.0
- old
+ new
@@ -1,6 +1,10 @@
+require "open3"
+require "shellwords"
+
module Mellon
+ # @api private
module Utils
module_function
# Build an entry info hash.
#
@@ -84,9 +88,50 @@
parsed = Plist.parse_xml(password.force_encoding("".encoding))
if parsed and parsed["NOTE"]
parsed["NOTE"]
else
password
+ end
+ end
+
+ # @see #sh
+ def security(*command, &block)
+ sh("security", *command, &block)
+ end
+
+ # @overload sh(*command, &block)
+ # Yields stdout and stderr to the given block.
+ #
+ # @overload sh(*command)
+ # Returns stdout.
+ #
+ # @raise [CommandError] if command exited with a non-zero exit status.
+ def sh(*command)
+ $stderr.puts "$ " + command.join(" ") if $VERBOSE
+ stdout, stderr, status = Open3.capture3(*command)
+
+ stdout.chomp!
+ stderr.chomp!
+
+ if $DEBUG
+ $stderr.puts stdout.gsub(/(?<=\n|\A)/, "--> ") unless stdout.empty?
+ $stderr.puts stderr.gsub(/(?<=\n|\A)/, "--! ") unless stderr.empty?
+ end
+
+ unless status.success?
+ error_string = Shellwords.join(command)
+ error_string << "\n"
+
+ stderr = "<no output>" if stderr.empty?
+ error_string << " " << stderr.chomp
+
+ raise CommandError, "[ERROR] #{error_string}"
+ end
+
+ if block_given?
+ yield [stdout, stderr]
+ else
+ stdout
end
end
end
end