class Qs::CLI

Public Class Methods

new(kernel = nil) click to toggle source
# File lib/qs/cli.rb, line 15
def initialize(kernel = nil)
  @kernel = kernel || Kernel
  @cli = CLIRB.new
end
run(args) click to toggle source
# File lib/qs/cli.rb, line 11
def self.run(args)
  self.new.run(*args)
end

Public Instance Methods

run(*args) click to toggle source
# File lib/qs/cli.rb, line 20
def run(*args)
  begin
    run!(*args)
  rescue CLIRB::HelpExit
    @kernel.puts help
  rescue CLIRB::VersionExit
    @kernel.puts Qs::VERSION
  rescue CLIRB::Error, Qs::ConfigFile::InvalidError => exception
    @kernel.puts "#{exception.message}\n\n"
    @kernel.puts help
    @kernel.exit 1
  rescue StandardError => exception
    @kernel.puts "#{exception.class}: #{exception.message}"
    @kernel.puts exception.backtrace.join("\n")
    @kernel.exit 1
  end
  @kernel.exit 0
end

Private Instance Methods

help() click to toggle source
# File lib/qs/cli.rb, line 61
def help
  "Usage: qs [CONFIG_FILE] [COMMAND]\n\n" \
  "Commands: run, start, stop, restart\n" \
  "Options: #{@cli}"
end
run!(*args) click to toggle source
# File lib/qs/cli.rb, line 41
def run!(*args)
  @cli.parse!(args)
  config_file_path, command = @cli.args
  config_file_path ||= 'config.qs'
  command ||= 'run'
  daemon = Qs::ConfigFile.new(config_file_path).daemon
  case(command)
  when 'run'
    Qs::Process.new(daemon, :daemonize => false).run
  when 'start'
    Qs::Process.new(daemon, :daemonize => true).run
  when 'stop'
    Qs::ProcessSignal.new(daemon, 'TERM').send
  when 'restart'
    Qs::ProcessSignal.new(daemon, 'USR2').send
  else
    raise CLIRB::Error, "#{command.inspect} is not a valid command"
  end
end