module Flucti module Utilities module Miscellaneous extend self BOGUS_ERROR_MESSAGES = ['error'] def try_save(*resources) resources.each do |resource| begin resource.save rescue WebService::ResourceInvalid error! $!.response.data, "(While attempting to save resource #{q resource}: #{resource.inspect}.)" end end yield end def error!(messages, note=nil, io=$stderr) messages = messages['errors'] if messages.respond_to?(:[]) && messages.include?('errors') messages = messages['error'] if messages.respond_to?(:[]) && messages.include?('error') messages = [messages].flatten.compact - BOGUS_ERROR_MESSAGES messages << "An unknown error occured while processing your request." if messages.empty? if method(:exit).arity == -1 puts_title(messages.size == 1 ? "Error" : "Errors", io) io.puts(note, "\n") if note messages.each do |message| puts_long("! #{message.lstrip}", io) end exit 1 else raise messages.map { |msg| msg.split("\n") }.flatten.map { |line| line.strip }.join(' ') end end def command(task_name=nil) @program ||= ((path = Pathname($0)).absolute? ? path.basename : path).to_s [@program, task_name].compact.join(' ') end def sh(command) command = command.to_s displayable_command = command[0,20] displayable_command += "..." if command.length > 20 print "Running locally: #{displayable_command} " succeeded = system(command) if succeeded print "Done\n" else raise "command #{q displayable_command} failed (status: #{$?.exitstatus})" end end def clean_name(name) name.to_s. gsub(/["']/, ''). gsub(/[^a-z0-9]+/i, "-"). gsub(/^\-|\-$/, ''). downcase end end end end