Commits

Seth Jackson  committed 5ad2303

Document new method params. Format code to match existing formatting. Use the /user/repositories endpoint as it returns all the info we need regardless of whether we are backing up all repos.

  • Participants
  • Parent commits cc149d8

Comments (0)

Files changed (3)

File bin/bitbucket-backup

 require "optparse"
 
 options = {}
+
 options[:all_repos] = false
 
-parser = OptionParser.new do | opts |
+parser = OptionParser.new do |opts|
+    opts.banner = "Usage: #{$0} [options] /path/to/backupdir\nUsername and password are required."
+    
+    opts.on("-a", "--all-repos", "Backup all visible repos to user") do
+        options[:all_repos] = true
+    end
+    
+    opts.on("-u", "--username USERNAME", "Required: Username for account to be backed up") do |username|
+        options[:username] = username
+    end
+    
+    opts.on("-p", "--password PASSWORD", "Required: Password for account to be backed up") do |password|
+        options[:password] = password
+    end
+end
 
-  opts.banner = "Usage: #{$0} [options] /path/to/backupdir\nUsername and password are required."
-
-  # Backup all repos visible to user
-  opts.on("-a", "--all-repos", "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? || !options[:password] || !options[:username]
     exit
 end
 
-
 Bitbucket::Backup.run(options[:username], options[:password], ARGV.first, options[:all_repos])

File 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, all_visible_repos = false)
+        # @param [Boolean] all_repos
+        #     whether to backup every repo the user has access to or just the
+        #     ones they own.
+        def self.run(username, password, backup_root, all_repos = false)
             backup_root = File.expand_path(backup_root)
             
             puts
             puts "Backing up repos to #{backup_root}"
             puts
             
-            repos = get_repo_list(username, password, all_visible_repos)
+            repos = get_repo_list(username, password)
             
             repos.each do |repo|
-                if all_visible_repos
-                  Bitbucket::Backup::Repository.new(repo, username, password, backup_root).backup
+                if all_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
+                    # 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, 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 )
+        def self.get_repo_list(username, password)
+            uri = URI.parse("https://api.bitbucket.org/1.0/user/repositories")
             
             http = Net::HTTP.new(uri.host, uri.port)
             
                 exit
             end
             
-            if all_visible_repos
-              JSON.parse(response.body)
-            else
-              JSON.parse(response.body)["repositories"]
-            end
+            JSON.parse(response.body)
         end
     end
 end

File 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
             #     the hash of repo data from the Bitbucket API.
             #
+            # @param [String] username
+            #     the username for the repo.
+            #
             # @param [String] password
             #     the plain-text passsword for the repo.
             #