Module: Jerakia::CLI::Server

Included in:
Jerakia::CLI
Defined in:
lib/jerakia/cli/server.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
67
68
# File 'lib/jerakia/cli/server.rb', line 4

def self.included(thor)
  thor.class_eval do
    desc 'server <options>', 'Start the Jerakia REST server'
    option :port,
           aliases: :p,
           type: :string,
           desc: 'Specify alternate port to bind to (default 9843)'

    option :bind,
           aliases: :b,
           type: :string,
           desc: 'Specify alternate address to bind to (default 127.0.0.1)'

    option :token_ttl,
           aliases: :t,
           type: :string,
           desc: 'Specify token TTL (default 300)'

    option :config,
           aliases: :c,
           type: :string,
           desc: 'Configuration file'
    option :log_level,
           aliases: :l,
           type: :string,
           desc: 'Log level'
    option :verbose,
           aliases: :v,
           type: :boolean,
           desc: 'Log to STDOUT in verbose mode'
    option :debug,
           aliases: :D,
           type: :boolean,
           desc: 'Log to STDOUT in debug mode'
    def server
      case true
      when options[:verbose]
        loglevel = 'verbose'
        logfile  = STDOUT
      when options[:debug]
        loglevel = 'debug'
        logfile  = STDOUT
      else
        logfile = nil
        loglevel = options[:log_level]
      end

      jerakia_opts = {
        :config => options[:config],
        :logfile  => logfile,
        :loglevel => loglevel,
        :trace    => options[:trace],
      }

      server_opts = {
        "port" => options[:port],
        "bind" => options[:bind],
        "token_ttl" => options[:token_ttl],
      }.reject { |k,v| v.nil? }

      require 'jerakia/server'
      Jerakia::Server.start(jerakia_opts, server_opts)
    end
  end
end