Trevor Basinger avatar Trevor Basinger committed 517bbd5

Implemented optparse to make more script friendly
Added configuration option to download all visible repos for user.

Comments (0)

Files changed (3)

bin/bitbucket-backup

 
 require "highline/import"
 require "bitbucket-backup"
+require "optparse"
 
-if ARGV.empty?
-    puts "Must specify a path to backup repos to."
-    
+options = {}
+options[:all_repos] = false
+
+parser = OptionParser.new do | opts |
+
+  opts.banner = "Usage: #{$0} [options] /path/to/backupdir\nUsername and password are required."
+
+  # Backup all repos visible to user
+  opts.on("-a", "--all-visible", "Backup all visible repos to user") do
+    options[:all_repos] = true
+  end
+
+  # Required: Username
+  opts.on("-u", "--username USERNAME", "Required: Username for account to be backed up") do | username |
+    options[:username] = username
+  end
+
+  # Required: Password
+  opts.on("-p", "--password PASSWORD", "Required: Password for account to be backed up") do | password |
+    options[:password] = password
+  end
+
+end
+parser.parse!
+
+if ARGV.empty? or !options[:password] or !options[:username]
+    puts parser.banner
     exit
 end
 
-username = ask("Username: ")
 
-password = ask("Password: ") do |q|
-    q.echo = false
-end
-
-Bitbucket::Backup.run(username, password, ARGV.first)
+Bitbucket::Backup.run(options[:username], options[:password], ARGV.first, options[:all_repos])

lib/bitbucket-backup/backup.rb

         # @param [String] backup_root
         #     the absolute or relative path of the directory to backup the repos to.
         #
-        def self.run(username, password, backup_root)
+        def self.run(username, password, backup_root, all_visible_repos = false)
             backup_root = File.expand_path(backup_root)
             
             puts
             puts "Backing up repos to #{backup_root}"
             puts
             
-            repos = get_repo_list(username, password)
+            repos = get_repo_list(username, password, all_visible_repos)
             
             repos.each do |repo|
-                # Only backup repos that the user owns.
-                if repo["owner"] == username
-                    Bitbucket::Backup::Repository.new(repo, password, backup_root).backup
+                puts "\n\nBLAH::::: #{repo.to_s}\n\n"
+                if all_visible_repos
+                  Bitbucket::Backup::Repository.new(repo, username, password, backup_root).backup
+                else
+                  # Only backup repos that the user owns.
+                  if repo["owner"] == username
+                    Bitbucket::Backup::Repository.new(repo, username, password, backup_root).backup
+                  end
                 end
             end
         end
         # @return [Array<String>]
         #     the repos.
         #
-        def self.get_repo_list(username, password)
-            uri = URI.parse("https://api.bitbucket.org/1.0/user/")
+        def self.get_repo_list(username, password, all_visible_repos = false)
+            owner_repos = "https://api.bitbucket.org/1.0/user/"
+            all_repos = "https://api.bitbucket.org/1.0/user/repositories"
+            uri = URI.parse( all_visible_repos ? all_repos : owner_repos )
             
             http = Net::HTTP.new(uri.host, uri.port)
             
                 exit
             end
             
-            JSON.parse(response.body)["repositories"]
+            if all_visible_repos
+              JSON.parse(response.body)
+            else
+              JSON.parse(response.body)["repositories"]
+            end
         end
     end
 end

lib/bitbucket-backup/repository.rb

             #     the hash of repo data from the Bitbucket API.
             #
             attr_reader :repo
-            
+
+            # @return [String]
+            #     the username for the repo.
+            #
+            attr_reader :username
+
             # @return [String]
             #     the plain-text password for the repo.
             #
             attr_reader :password
-            
             # @return [String]
             #     the absolute path of the directory to backup the repo to.
             #
             attr_reader :backup_root
-            
+
             # Creates a new repo.
             #
             # @param [Hash] repo
             # @param [String] backup_root
             #     the absolute path of the directory to backup the repo to.
             #
-            def initialize(repo, password, backup_root)
+            def initialize(repo, username, password, backup_root)
                 @repo        = repo
+                @username    = username
                 @password    = password
                 @backup_root = backup_root
             end
                 end
                 
                 if repo["is_private"]
-                    base_uri = "https://#{repo["owner"]}:#{CGI.escape(password)}@bitbucket.org/#{repo["owner"]}/#{repo["slug"]}#{ext}"
+                    base_uri = "https://#{username}:#{CGI.escape(password)}@bitbucket.org/#{repo["owner"]}/#{repo["slug"]}#{ext}"
                 else
                     base_uri = "https://bitbucket.org/#{repo["owner"]}/#{repo["slug"]}#{ext}"
                 end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.