Commits

Seth Jackson committed ddfcd82

Add config file support. Command line arguments override settings in the config file. Closes issue #1.

  • Participants
  • Parent commits e958681
  • Tags v0.3.0

Comments (0)

Files changed (2)

File bin/bitbucket-backup

 require "highline/import"
 require "bitbucket-backup"
 require "optparse"
+require "yaml"
 
 options = {}
 
-options[:all_repos] = false
-
 parser = OptionParser.new do |opts|
-    opts.banner = "Usage: #{$0} [options] /path/to/backupdir\nUsername and password are required."
+    cmd = File.basename($0)
     
-    opts.on("-a", "--all-repos", "Backup all visible repos to user") do
-        options[:all_repos] = true
+    opts.banner = "usage: #{cmd} [options] /path/to/backupdir"
+    
+    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
+    opts.on("-c", "--config FILE", "Path to config file.") do |config_file|
+        options["config_file"] = config_file
+    end
+    
+    opts.on("-u", "--username USERNAME", "Username for account to be backed up.") do |username|
+        options["username"] = username
+    end
+    
+    opts.on("-h", "--help", "Show this help message and exit.") do
+        puts opts
+        exit
+    end
+    
+    opts.on("-v", "--version", "Display the program's version and exit.") do
+        puts "#{cmd} (version #{Bitbucket::Backup::VERSION})"
+        exit
     end
 end
 
 parser.parse!
 
-if ARGV.empty? || !options[:username]
+config = {}
+
+if options["config_file"]
+    begin
+        path = File.expand_path(options["config_file"])
+        
+        yaml = YAML.parse_file(path)
+        
+        if !yaml
+            puts "Error: invalid config file: #{path}"
+            exit
+        end
+        
+        config ||= yaml
+    rescue YAML::SyntaxError, StandardError
+        puts "Error: invalid config file: #{path}."
+        exit
+    end
+end
+
+config.merge!(options)
+
+if ARGV.empty? || !config["username"]
     puts parser.banner
     exit
 end
 
-options[:password] = ask("Password: ") do |q|
-    q.echo = false
+if !config["password"]
+    config["password"] = ask("Password: ") do |q|
+        q.echo = false
+    end
 end
 
-Bitbucket::Backup.run(options[:username], options[:password], ARGV.first, options[:all_repos])
+Bitbucket::Backup.run(config["username"], config["password"], ARGV.first, config["all_repos"] || false)

File lib/bitbucket-backup/version.rb

 module Bitbucket
     module Backup
-        VERSION = "0.2.2"
+        VERSION = "0.3.0"
     end
 end