Sha256: fb77bcd8b3dafa2aca8587ff2d9ce13234f95133616cc755eb51a9b4d86417e7

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module FlareUp

  class Boot

    # TODO: This control flow is untested and too procedural
    def self.boot(command)
      conn = create_connection
      cmd = create_command(command)

      begin
        trap('SIGINT') do
          Emitter.warn("\nCTRL-C received; cancelling COPY command...")
          error_message = conn.cancel_current_command
          if error_message
            Emitter.error("Error cancelling COPY: #{error_message}")
          else
            Emitter.success('COPY command cancelled.')
          end
          CLI.bailout(1)
        end

        Emitter.info("Executing command: #{cmd.get_command}")
        handle_load_errors(cmd.execute(conn))
      rescue ConnectionError => e
        Emitter.error(e.message)
        CLI.bailout(1)
      rescue CommandError => e
        Emitter.error(e.message)
        CLI.bailout(1)
      end

    end

    def self.create_connection
      FlareUp::Connection.new(
        OptionStore.get(:redshift_endpoint),
        OptionStore.get(:database),
        OptionStore.get(:redshift_username),
        OptionStore.get(:redshift_password)
      )
    end
    private_class_method :create_connection

    def self.create_command(klass)
      cmd = klass.new(
        OptionStore.get(:table),
        OptionStore.get(:data_source),
        OptionStore.get(:aws_access_key),
        OptionStore.get(:aws_secret_key)
      )
      cmd.columns = OptionStore.get(:column_list) if OptionStore.get(:column_list)
      cmd.options = OptionStore.get(:copy_options) if OptionStore.get(:copy_options)
      cmd
    end
    private_class_method :create_command

    # TODO: Backfill tests
    def self.handle_load_errors(stl_load_errors)
      return if stl_load_errors.empty?
      Emitter.error("There was an error processing the COPY command.  Displaying the last (#{stl_load_errors.length}) errors.")
      stl_load_errors.each do |e|
        Emitter.error(e.pretty_print)
      end
      CLI.bailout(1)
    end
    private_class_method :handle_load_errors

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flare-up-0.9 lib/flare_up/boot.rb