Sha256: f9f76509f9eb15509373033aea705c1e9b501dafc1949b48b81bb62c52d9ac47

Contents?: true

Size: 1.48 KB

Versions: 86

Compression:

Stored size: 1.48 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)|
        option_name = name.to_s.tr("_", "-")
        case value
        when NilClass,
             TrueClass then args << "--#{option_name}"
        when FalseClass
        when Array     then value.each { |v| args << "--#{option_name}=#{v.inspect}" }
        else                args << "--#{option_name}=#{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

86 entries across 86 versions & 7 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.7.4 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.7.3 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
macournoyer-thin-1.1.0 lib/thin/command.rb
michaelyta-thin-1.2.2 lib/thin/command.rb
classiccms-0.7.2 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.7.1 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.7.0 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
thin-1.5.1 lib/thin/command.rb
classiccms-0.6.9 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.8 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.7 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
friendlyfashion-thin-1.4.1.1 lib/thin/command.rb
classiccms-0.6.6 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.5 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.4 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.3 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.2 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.1 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb
classiccms-0.6.0 vendor/bundle/gems/thin-1.3.1/lib/thin/command.rb