Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
def call_with_password(user, url, &block) abort "misformed URL #{url}" unless url =~ %r{^(\w+)://([^/]+)(.*?/?)[^/]*$} proto, host, path = $1, $2, $3 action = :retry res = %x{security find-internet-password -g -a "#{user}" -s "#{host}" -p "#{path}" -r #{proto} 2>&1 >/dev/null} action = block.call($1) if res =~ /^password: "(.*)"$/ while action == :retry cd_path = ENV['TM_SUPPORT_PATH'] + '/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog' res = %x{"#{cd_path}" secure-standard-inputbox \ --title "Enter Password" \ --informative-text "Enter password for “#{user}” at host “#{host}”" } break if res[0] == ?2 action = block.call(res[2..-2]) if action == :save_password then %x{security add-internet-password -a "#{user}" -s "#{host}" -r "#{proto}" -p "#{path}" -w "#{res[2..-2]}"} end end end call_with_password('duff', 'http://example.com/blog/xmlrpc.php') do |pw| p pw pw == "foo" ? :retry : :save_password end
From the Design Piracy series on my blog: