bbnew /bin/bbnew

Language Ruby Lines 113
MD5 Hash d36e4fbcf579bf070b0863098337901e Estimated Cost $2,055 (why?)
Repository https://bitbucket.org/rushi/bbnew View Raw File View Project SPDX
  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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env ruby
require 'optparse'
require 'net/https'
require 'uri'

require 'rubygems'              # needed for following library requires
require 'highline/import'       # needed for interactive authorization input

help_text = {
  :conf     =>  'Specify a custom config file.',
  :desc     =>  'Specify a project description.',
  :lang     =>  'Specify a programming language.',
  :pass     =>  'Specify a Bitbucket password.',
  :user     =>  'Specify a Bitbucket user name.',
  :site     =>  'Specify a project website URI.',
  :private  =>  'Specify if a project is private',
  :help     =>  'Display this help text and exit.'
}

@usage = <<EOF

USAGE: #{File.basename $0} [options] <NAME>

The <NAME> token is the name of the remote repository you wish to create.

EOF

@config = {:conf => '~/.bitbucket'}
@repo   = Hash.new

OptionParser.new do |opts|
  opts.banner = @usage

  opts.on('-c', '--conf=FILE', help_text[:conf]) do |conf|
    @config[:conf] = conf
  end
  
  opts.on('-d', '--desc=DESCRIPTION', help_text[:desc]) do |desc|
    @repo[:description] = desc
  end
  
  opts.on('-l', '--lang=LANGUAGE', help_text[:lang]) do |lang|
    @repo[:language] = lang
  end
  
  opts.on('-p', '--pass=PASSWORD', help_text[:pass]) do |pass|
    @config[:pass] = pass
  end
  
  opts.on('-u', '--user=USERNAME', help_text[:user]) do |user|
    @config[:user] = user
  end
  
  opts.on('--private', '--private', help_text[:private]) do |is_private|
    @repo[:is_private] = is_private
  end
  
  opts.on('-s', '--site=URI', help_text[:site]) do |site|
    @repo[:website] = site
  end

  opts.on('--help', '-h', help_text[:help]) do
    puts opts
    puts
    exit!(0)
  end
  
end.parse!

  
if 0 < ARGV.size
  p ARGV[0]
  @repo[:name] = ARGV[0]
else
  puts @usage
  puts %Q{    Try "#{File.basename $0} --help" for usage information.}
  puts
  exit 1
end
  
@config[:conf] ||= "~/.bitbucket"
if File.exist?(File.expand_path @config[:conf])
  File.open File.expand_path(@config[:conf]) do |bb|
    user, pass = bb.gets.chomp.split ':',2
    @config[:user] ||= user
    @config[:pass] ||= pass
  end
end
  
uri = URI.parse('https://api.bitbucket.org/1.0/repositories/')
  
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new(uri.path)
  
# uses BitBucket password, not SSH password
req.basic_auth(
  @config[:user] ||= ask('username: '),
  @config[:pass] || ask('password: ') {|q| q.echo = '' }
)
  
req.set_form_data(@repo)
res = http.start {|http| http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
  puts 'Successfully created the repository. Now cloning via ssh.'
  system `hg clone ssh://hg@bitbucket.org/#{
    @config[:user]
  }/#{@repo[:name]}`
else
  res.error!
end
Back to Top