Sha256: 6abb728cc554850bde5a4d7682856a0e84842fa00d331c4c9def8baa33250df2

Contents?: true

Size: 1.01 KB

Versions: 15

Compression:

Stored size: 1.01 KB

Contents

require 'open3'

module Lamby
  class Runner
    class Error < StandardError ; end
    class UnknownCommandPattern < Error ; end

    PATTERNS = [
      %r{\A\./bin/(rails|rake) db:migrate.*}
    ]

    class << self

      def handle?(event)
        event.dig 'lamby', 'runner'
      end

      def call(event)
        new(event).call
      end

    end

    def initialize(event)
      @event = event
      @body = ''
    end

    def call
      validate!
      status = Open3.popen3(command, chdir: chdir) do |_stdin, stdout, stderr, thread|
        @body << stdout.read
        @body << stderr.read
        puts @body
        thread.value.exitstatus
      end
      [status, {}, StringIO.new(@body)]
    end

    def command
      @event.dig 'lamby', 'runner'
    end

    private

    def chdir
      defined?(::Rails) ? ::Rails.root : Dir.pwd
    end

    def validate!
      return if pattern?
      raise UnknownCommandPattern.new(command)
    end

    def pattern?
      PATTERNS.any? { |p| p === command }
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
lamby-4.0.1 lib/lamby/runner.rb
lamby-4.0.0 lib/lamby/runner.rb
lamby-4.0.0.pre1 lib/lamby/runner.rb
lamby-4.0.0.beta6 lib/lamby/runner.rb
lamby-4.0.0.beta5 lib/lamby/runner.rb
lamby-4.0.0.beta4 lib/lamby/runner.rb
lamby-4.0.0.beta3 lib/lamby/runner.rb
lamby-4.0.0.beta2 lib/lamby/runner.rb
lamby-4.0.0.beta1 lib/lamby/runner.rb
lamby-3.1.3 lib/lamby/runner.rb
lamby-3.1.2 lib/lamby/runner.rb
lamby-3.1.1 lib/lamby/runner.rb
lamby-3.1.0 lib/lamby/runner.rb
lamby-3.0.3 lib/lamby/runner.rb
lamby-3.0.2 lib/lamby/runner.rb