bin/flapjack-populator in flapjack-0.8.7 vs bin/flapjack-populator in flapjack-0.8.8

- old
+ new

@@ -43,24 +43,29 @@ end end optparse.parse!(ARGV) +bail_with_usage = proc do |message| + puts message + puts "\n#{optparse}" + exit(false) +end + if options.help puts optparse exit elsif options.version puts Flapjack::VERSION exit elsif !["import-contacts", "import-entities", "purge-events"].include?(ARGV[0]) - if ARGV.nil? || ARGV.empty? - puts "No command provided" - else - puts "Unknown command provided: '#{ARGV[0]}'" - end - puts "\n#{optparse}" - exit 1 + message = if ARGV.nil? || ARGV.empty? + "No command provided" + else + "Unknown command provided: '#{ARGV[0]}'" + end + bail_with_usage.call message end FLAPJACK_ENV = ENV['FLAPJACK_ENV'] || 'production' config = Flapjack::Configuration.new @@ -71,18 +76,16 @@ if config_env.nil? || config_env.empty? puts "No config data for environment '#{FLAPJACK_ENV}' found in '#{options.config}'" exit(false) end -if options.from - filename = options.from - file = File.new(filename) -end - case ARGV[0] when "import-contacts" - contacts = Oj.load(file) + unless options.from + bail_with_usage.call "No import file provided with --from, eg. --from contacts.json" + end + contacts = Oj.load(File.new(options.from)) if contacts && contacts.is_a?(Enumerable) && contacts.any? {|e| !e['id'].nil?} @persistence = Redis.new(redis_options) contacts.each do |contact| unless contact['id'] @@ -93,11 +96,14 @@ end @persistence.quit end when "import-entities" - entities = Oj.load(file) + unless options.from + bail_with_usage.call "No import file provided with --from, eg. --from contacts.json" + end + entities = Oj.load(File.new(options.from)) if entities && entities.is_a?(Enumerable) && entities.any? {|e| !e['id'].nil?} @persistence = Redis.new(redis_options) entities.each do |entity| unless entity['id'] @@ -119,9 +125,8 @@ puts "setting expiry of '#{etq}' to 8 hours" @persistence.expire(etq, (60 * 60 * 8)) @persistence.quit else - puts "you need to give me something to do, eg a command like 'import-entities' or 'import-clients' etc" - raise ArgumentError + bail_with_usage.call "You need to give me something to do, eg. a command like 'import-entities', 'import-clients', etc." end