/bbnew
Ruby | 88 lines | 68 code | 18 blank | 2 comment | 5 complexity | d43b3e5800fd5f3a35f5ac65d5e94f6b MD5 | raw file
- #!/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
- if $0 == __FILE__
- repo = Hash.new # new repository settings
- config = Hash.new # bbnew config options
- optparse = OptionParser.new do |opts|
- opts.banner = 'usage: bbnew [OPTIONS] <NAME>'
-
- opts.on('-n', '--name name', 'Repository name (deprecated)') do |name|
- repo["name"] = name
- end
-
- opts.on('-c', '--conf file', 'Configuration file') do |conf|
- config["conf"] = conf
- end
-
- opts.on('-u', '--user username', 'User name') do |user|
- config["user"] = user
- end
-
- opts.on('-p', '--pass password', 'BitBucket password') do |pass|
- config["pass"] = pass
- end
-
- opts.on('-d', '--desc description', 'Project description') do |desc|
- repo["description"] = desc
- end
-
- opts.on('-l', '--lang language', 'Programming language') do |lang|
- repo["language"] = lang
- end
-
- opts.on('-w', '--site website', 'Web site for project') do |site|
- repo["website"] = site
- end
-
- end
-
- optparse.parse!
-
- begin
- repo['name'] ||= ARGV[0]
- raise "Repository name required" if !repo["name"]
- rescue Exception => e
- puts e
- puts optparse
- 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
- system `hg clone ssh://hg@bitbucket.org/#{
- config['user']
- }/#{repo["name"]}`
- else
- res.error!
- end
- end