Sha256: 480216954ecd287f36892745ad3a389d5ef9e930d3ac07d0afbd1dea64ac2640

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

require 'open3'

module Thin
  # Run a command through the +thin+ command-line script.
  class Command
    include Logging
    
    class << self
      # Path to the +thin+ script used to control the servers.
      # Leave this to default to use the one in the path.
      attr_accessor :script
    end
    
    def initialize(name, options={})
      @name    = name
      @options = options
    end
    
    def self.run(*args)
      new(*args).run
    end
    
    # Send the command to the +thin+ script
    def run
      shell_cmd = shellify
      trace shell_cmd
      trap('INT') {} # Ignore INT signal to pass CTRL+C to subprocess
      Open3.popen3(shell_cmd) do |stdin, stdout, stderr|
        log stdout.gets until stdout.eof?
        log stderr.gets until stderr.eof?
      end
    end
    
    # Turn into a runnable shell command
    def shellify
      shellified_options = @options.inject([]) do |args, (name, value)|
        case value
        when NilClass,
             TrueClass then args << "--#{name}"
        when FalseClass
        when Array     then value.each { |v| args << "--#{name}=#{v.inspect}" }
        else                args << "--#{name.to_s.tr('_', '-')}=#{value.inspect}"
        end
        args
      end
      
      raise ArgumentError, "Path to thin script can't be found, set Command.script" unless self.class.script
      
      "#{self.class.script} #{@name} #{shellified_options.compact.join(' ')}"
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
macournoyer-thin-1.0.1 lib/thin/command.rb
thin-1.0.0 lib/thin/command.rb