Class: Miniserver::Command
- Inherits:
-
Object
- Object
- Miniserver::Command
- Defined in:
- lib/miniserver/command.rb
Constant Summary collapse
- COMMANDS =
%w(start stop restart status)
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(argv) ⇒ Command
constructor
A new instance of Command.
- #parser ⇒ Object
- #run! ⇒ Object
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.('config.ru'), log: ::File.('log/miniserver.log'), pid: ::File.('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, ) File.delete([:pid]) if File.exist?([:pid]) = .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} #{.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
#parser ⇒ Object
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. = "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 |