boxen-git-credential
1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/ruby
# Provide git credentials using Boxen's config.
unless command = ARGV[0]
this = File.basename $0
abort "Usage: #{this} <get|store|erase>"
end
require "pathname"
# It's a UTF-8, UTF-8, UTF-8 world.
Encoding.default_external = Encoding::UTF_8 if RUBY_VERSION > "1.9"
# Put us where we belong, in the root dir of our boxen repo.
Dir.chdir Pathname.new(__FILE__).realpath + "../.."
# 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 }
# Set up our local configuration, deps, and load path.
load "config/basic.rb"
require "boxen/config"
config = Boxen::Config.load
input = $stdin.read
attrs = Hash[input.split($/).map { |l| l.split("=") }]
# find GitHub or GitHub Enterprise host
ghhost = URI(config.ghurl).host
host_exp = Regexp.new "(^|\.)" + Regexp.escape(ghhost)
if command == "get" && host_exp.match(attrs["host"])
puts "username=#{config.token}"
puts "password=x-oauth-basic"
else
require "open4"
fallback = ENV["BOXEN_GIT_CREDENTIAL_FALLBACK"]
fallback ||= "/usr/local/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