#!/bin/env ruby # encoding: utf-8 # Author: kimoto require 'trollop' require 'pathname' require 'vrowser' require 'vrowser/http_daemon' require 'fileutils' class VrowserCLI def self.run(argv) self.new.parse(argv) end # get subcommand names def self.sub_commands self.instance_methods.map(&:to_s).grep(/command_/).map{ |command_symbol| command_symbol.to_s.gsub(/^command_/, "") } end def initialize end def parse(argv) @argv = argv sub_commands = self.class.sub_commands global_opts = Trollop::options do banner <<-EOS Usage: #{File.basename($0)} [#{sub_commands.join(',')}] hoge EOS version File.read(Pathname.new(__FILE__).dirname.realpath + "../VERSION") stop_on sub_commands end cmd = @argv.shift cmd_opts = Trollop::options do case cmd when "sample" opt :output_path, "output path", :short => "-o", :type => String, :default => "./config.yml" when "list" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "fetch" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "update" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "json" opt :config_file, "config file path", :short => "-f", :type => String, :required => true when "server", "daemon" opt :config_file, "config file path", :short => "-f", :type => String, :required => true opt :port, "port number", :short => "-p", :type => String, :default => '3000' opt :host, "host or ip address", :short => "-h", :type => String, :default => 'localhost' opt :log_path, "log file path", :short => "-l", :default => STDERR opt :document_root, "document root path", :short => "-d", :type => String, :default => (Pathname.new(__FILE__).dirname.realpath + '../public_html').to_s else Trollop::die "unknown subcommand: #{cmd.inspect}" end end if sub_commands.include? cmd self.send("command_" + cmd, cmd_opts) return 0 else cmd_opts.help.display end end ### define sub commands def command_list(options) Vrowser.load_file(options[:config_file]) do |vrowser| puts vrowser.active_servers.map(&:name).join($/) end end def command_sample(options) sample_config_path = (Pathname.new(__FILE__).dirname + "../examples/config.yml").realpath output_path = options[:output_path] if File.exist? output_path STDERR.puts "Already file exists!: #{output_path}" else FileUtils.cp(sample_config_path, options[:output_path]) STDOUT.puts "Generated sample config file: #{output_path}" end end def command_update(options) Vrowser.load_file(options[:config_file]) do |vrowser| vrowser.update vrowser.clear end end def command_json(options) Vrowser.load_file(options[:config_file]) do |vrowser| greped = vrowser.active_servers.select(:name, :host, :ping, :num_players, :type, :map, :players) ordered = greped.order(:host) ordered.map(&:values).to_json.display end end def command_server(options) execute_as_server(options.merge({:damonize => false})) end def command_daemon(options) execute_as_server(options.merge({:daemonize => true})) end private def execute_as_server(options) Vrowser::HTTPDaemon.new( :config_path => Pathname.new(options[:config_file]).realpath, :BindAddress => options[:host], :Port => options[:port], :DocumentRoot => Pathname.new(options[:document_root]).realpath, # :Logger => logger ) do |vrowser| if options[:daemonize] vrowser.daemonize! vrowser.start else vrowser.start end end end end Vrowser.logger = Logger.new(STDERR) exit VrowserCLI.run(ARGV)