PageRenderTime 74ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/bbnew

https://bitbucket.org/sterlingcamden/bbnew
Ruby | 112 lines | 86 code | 24 blank | 2 comment | 3 complexity | d36e4fbcf579bf070b0863098337901e MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'optparse'
  3. require 'net/https'
  4. require 'uri'
  5. require 'rubygems' # needed for following library requires
  6. require 'highline/import' # needed for interactive authorization input
  7. help_text = {
  8. :conf => 'Specify a custom config file.',
  9. :desc => 'Specify a project description.',
  10. :lang => 'Specify a programming language.',
  11. :pass => 'Specify a Bitbucket password.',
  12. :user => 'Specify a Bitbucket user name.',
  13. :site => 'Specify a project website URI.',
  14. :private => 'Specify if a project is private',
  15. :help => 'Display this help text and exit.'
  16. }
  17. @usage = <<EOF
  18. USAGE: #{File.basename $0} [options] <NAME>
  19. The <NAME> token is the name of the remote repository you wish to create.
  20. EOF
  21. @config = {:conf => '~/.bitbucket'}
  22. @repo = Hash.new
  23. OptionParser.new do |opts|
  24. opts.banner = @usage
  25. opts.on('-c', '--conf=FILE', help_text[:conf]) do |conf|
  26. @config[:conf] = conf
  27. end
  28. opts.on('-d', '--desc=DESCRIPTION', help_text[:desc]) do |desc|
  29. @repo[:description] = desc
  30. end
  31. opts.on('-l', '--lang=LANGUAGE', help_text[:lang]) do |lang|
  32. @repo[:language] = lang
  33. end
  34. opts.on('-p', '--pass=PASSWORD', help_text[:pass]) do |pass|
  35. @config[:pass] = pass
  36. end
  37. opts.on('-u', '--user=USERNAME', help_text[:user]) do |user|
  38. @config[:user] = user
  39. end
  40. opts.on('--private', '--private', help_text[:private]) do |is_private|
  41. @repo[:is_private] = is_private
  42. end
  43. opts.on('-s', '--site=URI', help_text[:site]) do |site|
  44. @repo[:website] = site
  45. end
  46. opts.on('--help', '-h', help_text[:help]) do
  47. puts opts
  48. puts
  49. exit!(0)
  50. end
  51. end.parse!
  52. if 0 < ARGV.size
  53. p ARGV[0]
  54. @repo[:name] = ARGV[0]
  55. else
  56. puts @usage
  57. puts %Q{ Try "#{File.basename $0} --help" for usage information.}
  58. puts
  59. exit 1
  60. end
  61. @config[:conf] ||= "~/.bitbucket"
  62. if File.exist?(File.expand_path @config[:conf])
  63. File.open File.expand_path(@config[:conf]) do |bb|
  64. user, pass = bb.gets.chomp.split ':',2
  65. @config[:user] ||= user
  66. @config[:pass] ||= pass
  67. end
  68. end
  69. uri = URI.parse('https://api.bitbucket.org/1.0/repositories/')
  70. http = Net::HTTP.new(uri.host, uri.port)
  71. http.use_ssl = true
  72. req = Net::HTTP::Post.new(uri.path)
  73. # uses BitBucket password, not SSH password
  74. req.basic_auth(
  75. @config[:user] ||= ask('username: '),
  76. @config[:pass] || ask('password: ') {|q| q.echo = '' }
  77. )
  78. req.set_form_data(@repo)
  79. res = http.start {|http| http.request(req) }
  80. case res
  81. when Net::HTTPSuccess, Net::HTTPRedirection
  82. puts 'Successfully created the repository. Now cloning via ssh.'
  83. system `hg clone ssh://hg@bitbucket.org/#{
  84. @config[:user]
  85. }/#{@repo[:name]}`
  86. else
  87. res.error!
  88. end