#!/usr/bin/env ruby -wKU require 'optparse' require 'yaml' UPDATEAGENT_VERSION = File.read(File.dirname(__FILE__) + '/../VERSION').strip options = {:confirm => true, :force => false} config = nil opt_parser = OptionParser.new do |opts| opts.banner = "Usage: update_onebody -c path/to/config.yml [options] path/to/people.csv" opts.on("-y", "--no-confirm", "Assume 'yes' to any questions") do |v| options[:confirm] = false end opts.on("-l", "--log LOGFILE", "Output to log rather than stdout") do |log| $stdout = $stderr = File.open(log, 'a') end opts.on("-f", "--force", "Force update all records") do |f| options[:force] = true end opts.on("-t", "--test COUNT", "Test input and conversion without sending data to OneBody - show first COUNT records") do |test| options[:test] = test.to_i end opts.on("-c", "--config-file PATH", "Path to configuration file") do |c| options[:config_file] = c config = YAML::load(File.read(c)) ONEBODY_SITE = config['site'] ONEBODY_USER_EMAIL = config['user_email'] ONEBODY_USER_KEY = config['user_key'] if c = config['converter'] ONEBODY_USE_CONVERTER = c['name'] end end # doesn't work yet #opts.on("-d", "--delete", "Delete people and families not found in source file") do |d| # options[:delete] = true #end opts.on("--debug", "Enable extra debugging messages.") do |d| config['debug'] = true if config end opts.on("--version", "Show version and exit.") do puts "OneBody UpdateAgent version #{UPDATEAGENT_VERSION}" puts exit end end opt_parser.parse! unless options[:config_file] puts opt_parser.help puts puts 'You must specify a config file containing site and login info.' puts "See #{File.expand_path(File.dirname(File.dirname(__FILE__)))}/example.yml" puts exit end if ARGV[0] # path/to/people.csv $: << 'lib' require 'onebody-updateagent' puts "Update Agent running at #{Time.now.strftime('%m/%d/%Y %I:%M %p')}" agent = PeopleUpdater.new(ARGV[0], config) if options[:test] require 'pp' pp agent.data[0...options[:test]] else puts "comparing records..." agent.compare(options[:force]) if agent.has_work? if options[:confirm] case ask("#{agent.create_count} record(s) to create and #{agent.update_count} record(s) to update. Continue? (Yes, No, Review) ") { |q| q.in = %w(yes no review y n r) } when 'review', 'r' agent.present unless agent.confirm puts "Canceled by user\n" exit end when 'no', 'n' puts "Canceled by user\n" exit end end agent.start_sync agent.push agent.send_notification agent.finish_sync puts "Completed at #{Time.now.strftime('%m/%d/%Y %I:%M %p')}\n\n" else puts "Nothing to push\n\n" end end else puts opt_parser.help end