Class: Miniserver::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/miniserver/command.rb

Constant Summary collapse

COMMANDS =
%w(start stop restart status)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/miniserver/command.rb', line 9

def initialize(argv)
  @options = {
    daemonize: false,
    host: '0.0.0.0',
    port: 8080,
    rackup: ::File.expand_path('config.ru'),
    log: ::File.expand_path('log/miniserver.log'),
    pid: ::File.expand_path('tmp/pids/miniserver.pid'),
    environment: 'development'
  }

  parser.parse!(argv)

  @command = argv.pop || 'start'
end

Class Method Details

.restart(script, options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/miniserver/command.rb', line 78

def self.restart(script, options)
  File.delete(options[:pid]) if File.exist?(options[:pid])

  shellified_options = options.inject([]) do |args, (name, value)|
    option_name = name.to_s.tr("_", "-")
    case value
    when NilClass, TrueClass
      args << "--#{option_name}"
    when FalseClass
    when Array
      value.each { |v| args << "--#{option_name}=#{v}" }
    else
      args << "--#{option_name}=#{value}"
    end

    args
  end

  cmd = "#{script} #{shellified_options.compact.join(' ')} start"

  Open3.popen3(cmd) do |stdin, stdout, stderr|
    puts stdout.gets until stdout.eof?
    puts stderr.gets until stderr.eof?
  end

  exit
end

Instance Method Details

#parserObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/miniserver/command.rb', line 33

def parser
  @parser ||= OptionParser.new do |o|
    o.banner = "Miniserver #{Miniserver::VERSION}\nUsage: miniserver <options> #{COMMANDS.join('|')}"
    o.separator ""

    o.on "-p", "--port PORT", "Define the TCP port to bind (default: #{@options[:port]})" do |arg|
      @options[:port] = arg
    end

    o.on "-o", "--host HOST", "Bind to HOST address (default: #{@options[:host]})" do |arg|
      @options[:host] = arg
    end

    o.on "-l", "--log FILE", "File to redirect output (default: log/miniserver.log)" do |arg|
      @options[:log] = arg
    end

    o.on "-P", "--pid FILE", "File to store PID (default: tmp/pids/miniserver.pid)" do |arg|
      @options[:pid] = arg
    end

    o.on "-d", "--daemonize", "Run daemonized in the background(default: false)" do |arg|
      @options[:daemonize] = arg
    end

    o.on "-e", "--environment ENVIRONMENT", "The environment to run the Rack app on (default development)" do |arg|
      @options[:environment] = arg
    end

    o.on "-R", "--rackup FILE", "Load a Rack config file (default: #{@options[:rackup]})" do |arg|
      @options[:rackup] = arg
    end

    o.on_tail "-h", "--help", "Show help" do
      puts @parser
      exit
    end

    o.on_tail("-v", "--version", "Show version") do
      puts "Miniserver #{Miniserver::VERSION}"
      exit
    end
  end
end

#run!Object



25
26
27
28
29
30
31
# File 'lib/miniserver/command.rb', line 25

def run!
  if COMMANDS.include?(@command)
    Runner.new(@options).send(@command)
  else
    puts @parser
  end
end