2012-10-05 00:09:40 -04:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# Provide git credentials using Boxen's config.
|
|
|
|
|
2013-02-14 21:11:12 -05:00
|
|
|
unless command = ARGV[0]
|
2012-10-05 00:09:40 -04:00
|
|
|
this = File.basename $0
|
|
|
|
abort "Usage: #{this} <get|store|erase>"
|
|
|
|
end
|
|
|
|
|
|
|
|
require "pathname"
|
|
|
|
|
2013-11-16 02:11:07 -05:00
|
|
|
# It's a UTF-8, UTF-8, UTF-8 world.
|
|
|
|
|
|
|
|
Encoding.default_external = Encoding::UTF_8
|
|
|
|
|
2012-10-05 00:09:40 -04:00
|
|
|
# Put us where we belong, in the root dir of our boxen repo.
|
|
|
|
|
|
|
|
Dir.chdir Pathname.new(__FILE__).realpath + "../.."
|
|
|
|
|
2012-12-06 19:02:49 -05:00
|
|
|
# Because we can be called from inside other Ruby processes, unset any
|
|
|
|
# `BUNDLE_` environment variables.
|
|
|
|
|
|
|
|
ENV.keys.select { |k| /^BUNDLE_/i }.each { |k| ENV.delete k }
|
|
|
|
|
2012-10-05 00:09:40 -04:00
|
|
|
# Set up our local configuration, deps, and load path.
|
|
|
|
|
|
|
|
load "config/basic.rb"
|
|
|
|
require "boxen/config"
|
|
|
|
|
|
|
|
config = Boxen::Config.load
|
2013-02-14 21:11:12 -05:00
|
|
|
input = $stdin.read
|
|
|
|
attrs = Hash[input.split($/).map { |l| l.split("=") }]
|
2013-10-09 16:23:57 -04:00
|
|
|
# find GitHub or GitHub Enterprise host
|
2013-07-17 11:55:49 -04:00
|
|
|
ghhost = URI(config.ghurl).host
|
2013-02-14 21:11:12 -05:00
|
|
|
|
2013-10-09 16:23:57 -04:00
|
|
|
host_exp = Regexp.new "(^|\.)" + Regexp.escape(ghhost)
|
|
|
|
if command == "get" && host_exp.match(attrs["host"])
|
2013-09-06 16:47:03 -04:00
|
|
|
puts "username=#{config.token}"
|
|
|
|
puts "password=x-oauth-basic"
|
|
|
|
else
|
2013-02-14 21:11:12 -05:00
|
|
|
require "open4"
|
|
|
|
|
|
|
|
fallback = ENV["BOXEN_GIT_CREDENTIAL_FALLBACK"]
|
|
|
|
fallback ||= "#{config.homedir}/homebrew/bin/git-credential-osxkeychain"
|
|
|
|
|
|
|
|
status = Open4.popen4 fallback, *ARGV do |pid, stdin, stdout, stderr|
|
|
|
|
stdin.write input
|
|
|
|
stdin.puts
|
|
|
|
|
|
|
|
$stdout.write stdout.read
|
|
|
|
end
|
|
|
|
|
|
|
|
exit status.exitstatus
|
|
|
|
end
|