#!/usr/bin/env ruby require 'optparse' require 'yaml' 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 opts.on("-g", "--groups", "Sync groups.csv rather than people.csv") do |g| options[:sync_groups] = true end opts.on("-d", "--delete", "Delete people and families not found in source file") do |d| options[:delete] = true 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 or path/to/groups.csv $: << 'lib' require 'onebody-updateagent' puts "Update Agent running at #{Time.now.strftime('%m/%d/%Y %I:%M %p')}" if options[:sync_groups] agent = ExternalGroupUpdater.new(ARGV[0]) puts "comparing records..." agent.compare if agent.has_work? if options[:confirm] case ask("#{agent.create.length + agent.update.length} record(s) to push. Continue? (Yes, No) ") { |q| q.in = %w(yes no y n) } when 'no', 'n' puts "Canceled by user\n" exit end end agent.push puts "Completed at #{Time.now.strftime('%m/%d/%Y %I:%M %p')}\n\n" else puts "Nothing to push\n\n" end else # sync people 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.length} record(s) to create and #{agent.update.length} 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 end else puts opt_parser.help end