PageRenderTime 32ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/bbnew

https://bitbucket.org/designermonkey/bbnew
Ruby | 88 lines | 68 code | 18 blank | 2 comment | 5 complexity | d43b3e5800fd5f3a35f5ac65d5e94f6b 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. if $0 == __FILE__
  8. repo = Hash.new # new repository settings
  9. config = Hash.new # bbnew config options
  10. optparse = OptionParser.new do |opts|
  11. opts.banner = 'usage: bbnew [OPTIONS] <NAME>'
  12. opts.on('-n', '--name name', 'Repository name (deprecated)') do |name|
  13. repo["name"] = name
  14. end
  15. opts.on('-c', '--conf file', 'Configuration file') do |conf|
  16. config["conf"] = conf
  17. end
  18. opts.on('-u', '--user username', 'User name') do |user|
  19. config["user"] = user
  20. end
  21. opts.on('-p', '--pass password', 'BitBucket password') do |pass|
  22. config["pass"] = pass
  23. end
  24. opts.on('-d', '--desc description', 'Project description') do |desc|
  25. repo["description"] = desc
  26. end
  27. opts.on('-l', '--lang language', 'Programming language') do |lang|
  28. repo["language"] = lang
  29. end
  30. opts.on('-w', '--site website', 'Web site for project') do |site|
  31. repo["website"] = site
  32. end
  33. end
  34. optparse.parse!
  35. begin
  36. repo['name'] ||= ARGV[0]
  37. raise "Repository name required" if !repo["name"]
  38. rescue Exception => e
  39. puts e
  40. puts optparse
  41. exit 1
  42. end
  43. config['conf'] ||= "~/.bitbucket"
  44. if File.exist?(File.expand_path config['conf'])
  45. File.open File.expand_path(config['conf']) do |bb|
  46. user, pass = bb.gets.chomp.split ':',2
  47. config['user'] ||= user
  48. config['pass'] ||= pass
  49. end
  50. end
  51. uri = URI.parse('https://api.bitbucket.org/1.0/repositories/')
  52. http = Net::HTTP.new(uri.host, uri.port)
  53. http.use_ssl = true
  54. req = Net::HTTP::Post.new(uri.path)
  55. # uses BitBucket password, not SSH password
  56. req.basic_auth(
  57. config['user'] ||= ask('username: '),
  58. config['pass'] || ask('password: ') {|q| q.echo = '' }
  59. )
  60. req.set_form_data(repo)
  61. res = http.start {|http| http.request(req) }
  62. case res
  63. when Net::HTTPSuccess, Net::HTTPRedirection
  64. system `hg clone ssh://hg@bitbucket.org/#{
  65. config['user']
  66. }/#{repo["name"]}`
  67. else
  68. res.error!
  69. end
  70. end