Class: VrowserCLI

Inherits:
Object
  • Object
show all
Defined in:
bin/vrowser

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVrowserCLI

Returns a new instance of VrowserCLI.



22
23
# File 'bin/vrowser', line 22

def initialize
end

Class Method Details

.run(argv) ⇒ Object



11
12
13
# File 'bin/vrowser', line 11

def self.run(argv)
  self.new.parse(argv)
end

.sub_commandsObject

get subcommand names



16
17
18
19
20
# File 'bin/vrowser', line 16

def self.sub_commands
  self.instance_methods.map(&:to_s).grep(/command_/).map{ |command_symbol| 
    command_symbol.to_s.gsub(/^command_/, "")
  }
end

Instance Method Details

#command_daemon(options) ⇒ Object



122
123
124
# File 'bin/vrowser', line 122

def command_daemon(options)
  execute_as_server(options.merge({:daemonize => true}))
end

#command_fetch(options) ⇒ Object



96
97
98
99
100
101
# File 'bin/vrowser', line 96

def command_fetch(options)
  Vrowser.load_file(options[:config_file]) do |vrowser|
    vrowser.fetch
    vrowser.clear
  end
end

#command_httpd_daemon(options) ⇒ Object



126
127
128
# File 'bin/vrowser', line 126

def command_httpd_daemon(options)
  execute_as_httpd_server(options.merge({:daemonize => true}))
end

#command_json(options) ⇒ Object



110
111
112
113
114
115
116
# File 'bin/vrowser', line 110

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

#command_list(options) ⇒ Object

define sub commands



79
80
81
82
83
# File 'bin/vrowser', line 79

def command_list(options)
  Vrowser.load_file(options[:config_file]) do |vrowser|
    puts vrowser.active_servers.map(&:name).join($/)
  end
end

#command_sample(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'bin/vrowser', line 85

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

#command_server(options) ⇒ Object



118
119
120
# File 'bin/vrowser', line 118

def command_server(options)
  execute_as_server(options.merge({:damonize => false}))
end

#command_update(options) ⇒ Object



103
104
105
106
107
108
# File 'bin/vrowser', line 103

def command_update(options)
  Vrowser.load_file(options[:config_file]) do |vrowser|
    vrowser.update
    vrowser.clear
  end
end

#execute_sub_commands(cmd, cmd_opts) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'bin/vrowser', line 68

def execute_sub_commands(cmd, cmd_opts)
  if self.class.sub_commands.include? cmd
    self.send("command_" +  cmd, cmd_opts)
    return 0
  else
    cmd_opts.help.display
    return 1
  end
end

#parse(argv) ⇒ Object



25
26
27
28
29
30
31
32
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
# File 'bin/vrowser', line 25

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 :log_path, "log file path", :short => "-l", :type => String
    when "httpd_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
  return execute_sub_commands(cmd, cmd_opts)
end