Sha256: 9b593047f15270f7c6bc15f0919ce505608959ef45eb6700933961a15adf0273

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require 'optparse'

module RubyWolf
  class CLI
    attr_reader :app, :server, :configs

    def initialize(args)
      @args = args
      @configs = RubyWolf::Configuration.new
      @app_root = `pwd`.to_s.strip
    end

    def run
      parse_options
      set_environment

      raise 'Rack file not found' unless File.exist?(rack_file)

      @server = RubyWolf::Server.new(rack_file, configs)
      @server.start
    end

    def parse_options
      opt_parser = OptionParser.new do |opts|
        opts.banner = 'Usage: ruby_wolf [options]'

        opts.on('-d', '--daemon', 'Demonize this web server to run background') do
          @configs[:daemon] = true
        end

        opts.on('-h HOST', '--port=HOST', 'Binding host') do |arg|
          @configs[:host] = arg
        end

        opts.on('-p PORT', '--port=PORT', 'Port of the program') do |arg|
          @configs[:port] = arg.to_i
        end

        opts.on('-w WORKER', '--worker=WORKER', 'Number of worker processes') do |arg|
          @configs[:worker] = arg.to_i
        end

        opts.on('-e ENVIRONMENT', '--environment=ENVIRONMENT', 'Current environment') do |arg|
          @configs[:environment] = arg
        end

        opts.on('-h', '--help', 'Show the usages') do
          puts opts
          exit
        end
      end

      opt_parser.parse!(@args)
    end

    private

    def rack_file
      "#{@app_root}/config.ru"
    end

    def set_environment
      ENV['RAILS_ENV'] = configs[:environment]
      ENV['RACK_ENV'] = configs[:environment]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby_wolf-0.4.0 lib/ruby_wolf/cli.rb
ruby_wolf-0.3.3 lib/ruby_wolf/cli.rb
ruby_wolf-0.3.2 lib/ruby_wolf/cli.rb
ruby_wolf-0.3.1 lib/ruby_wolf/cli.rb