module Flydata module Helpers @@development_mode = nil module_function def parse_command(cmd) klass = Flydata::Command::Base method = cmd if cmd.include?(':') class_name, method = cmd.split(':') klass = to_command_class(class_name) end [klass, method] end def usage_text text = "" text += '-' * 64 text += "\n" text += <<-EOM Usage: flydata COMMAND start # start flydata process stop # stop flydata process restart # restart flydata process status # check flydata process version # show flydata version conf # show configuration sync [tables] # initial sync (only for mysql database) sync:generate_table_ddl [tables| –all-tables] # Generate CREATE TABLE script for Redshift sync:reset [tables] # reset sync and stop flydata process sync:flush # flush your current buffer and stop flydata process EOM text += "\n" text += '-' * 64 text += "\n" text end def flydata_version IO.read(VERSION_PATH).strip end def to_command_class(class_name) eval("Flydata::Command::#{class_name.camelcase}") end def development? return @@development_mode unless @@development_mode.nil? @@development_mode = !!(File.open(flydata_conf_file).first.strip.end_with?('dev')) end def env_mode development? ? 'dev' : '' end def env_suffix development? ? '_dev' : '' end # format text def format_menu_list(menu_list) max_length_list = menu_list.inject(Array.new(menu_list.first.size, 0)) do |ml, menu| 0.upto(menu.size - 1) do |i| ml[i] = menu[i].length if ml[i] < menu[i].length end ml end menu_list = menu_list.collect do |menu| 0.upto(menu.size - 1).inject("") do |str, i| str = "#{str}#{menu[i].ljust(max_length_list[i] + 1, ' ')}" end end end def flydata_api_host_file File.join(FLYDATA_HOME, 'flydata_api_host') end def flydata_conf_file File.join(FLYDATA_HOME, 'flydata.conf') end # Retry the given block if +exception+ happens def retry_on(exception = StandardError, try_count = 3, interval = 1.0) count = 0 begin count += 1 yield rescue exception if count < try_count sleep interval interval *= 2 retry else raise end end end UNIT_PREFIX = %W(TB GB MB KB B).freeze def as_size( s ) s = s.to_f i = UNIT_PREFIX.length - 1 while s > 512 && i > 0 s /= 1024 i -= 1 end ((s > 9 || s.modulo(1) < 0.1 ? '%d' : '%.1f') % s) + ' ' + UNIT_PREFIX[i] end end end