Module: Binnacle::Commands

Defined in:
lib/binnacle/commands/help.rb,
lib/binnacle/commands/tail.rb

Class Method Summary collapse

Class Method Details

.dispatch(opts) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/binnacle/commands/tail.rb', line 50

def self.dispatch(opts)
  # tail --follow
  if opts[:follow_given] && opts[:app_given]
    monitor(opts[:host], opts[:api_key], opts[:api_secret], opts[:app], opts[:environment], true, opts[:encrypted], opts[:payload])
  elsif opts[:follow_given] && opts[:channel_given]
    monitor(opts[:host], opts[:api_key], opts[:api_secret], opts[:channel], opts[:environment], false, opts[:encrypted], opts[:payload])
  end

  # tail --lines
  lines(opts[:host], opts[:api_key], opts[:api_secret], opts[:channel], opts[:lines], opts[:since], opts[:environment], opts[:encrypted]) if opts[:lines_given]
end

.helpObject



3
4
5
6
7
8
9
# File 'lib/binnacle/commands/help.rb', line 3

def self.help
  opts = Trollop::options do
    banner HELP_BANNER
  end

  Trollop::educate unless ENV["TEST_MODE"] == 'true'
end

.lines(host, api_key, api_secret, channel, lines, since, environment = 'production', encrypted = true, payload = false) ⇒ Object

tail –lines=50 –since=10 –host=my_host –channel=my_channel



90
91
92
93
94
95
96
97
98
# File 'lib/binnacle/commands/tail.rb', line 90

def self.lines(host, api_key, api_secret, channel, lines, since, environment = 'production', encrypted = true, payload = false)
  puts "Retrieving last #{lines} lines since #{since} minutes ago from Channel #{channel} ..."
  Binnacle.configuration.encrypted = encrypted
  client = Binnacle::Client.new(api_key, api_secret, host)

  client.recents(lines, since, channel).each do |e|
    print_event(e, payload)
  end
end

.monitor(host, api_key, api_secret, channel, environment, is_app = false, encrypted = true, payload = false) ⇒ Object

tail –follow –host=my_host –channel=my_channel



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/binnacle/commands/tail.rb', line 64

def self.monitor(host, api_key, api_secret, channel, environment, is_app = false, encrypted = true, payload = false)
  EM.run do
    Signal.trap("INT")  { EventMachine.stop }
    Signal.trap("TERM") { EventMachine.stop }

    ws_url = build_ws_url(host, api_key, api_secret, channel, environment, is_app, encrypted)
    ws = Faye::WebSocket::Client.new(ws_url)

    ws.on :open do |event|
      puts "Monitoring #{is_app ? 'App' : 'Channel'} #{channel} on #{host}..."
    end

    ws.on :message do |event|
      if event.data !~ /\s/ && event.data != 'X'
        print_event_from_json(event.data, payload)
      end
    end

    ws.on :close do |event|
      ws = nil
    end
  end
end

.tailObject



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
# File 'lib/binnacle/commands/tail.rb', line 9

def self.tail
  if ENV["TEST_MODE"] == 'true'
    String.disable_colorization = true
  end

  opts = Trollop::options do
    banner TAIL_BANNER
    opt(:host, "Binnacle Host", type: :string, default: 'localhost')
    opt(:channel, "Binnacle Channel", type: :string)
    opt(:app, "Binnacle App",  type: :string)
    opt(:api_key, "Binnacle API Key", type: :string, short: '-u')
    opt(:api_secret, "Binnacle API Secret", type: :string, short: '-p')
    opt(:follow, "Monitors a Binnacle Channel or App")
    opt(:lines, "Get the last n events on the Channel", type: :int, short: '-n')
    opt(:since, "Number of minutes in the past to search for events", type: :int)
    opt(:encrypted, "Use SSL/HTTPS", default: true)
    opt(:environment, "The target environment (Rails.env)", type: :string, default: 'production')
    opt(:payload, "Show JSON Payload", default: false)
  end

  if (errors = validate(opts)).empty?
    dispatch(opts)
  else
    puts "The following errors prevented the tail command from executing:"
    errors.each { |e| puts "  - #{e}" }
    puts "\nSUBCOMMAND"
    puts "      tail -- listen to a Binnacle channel or app\n\n"
    Trollop::educate unless ENV["TEST_MODE"] == 'true'
  end
end

.validate(opts) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/binnacle/commands/tail.rb', line 40

def self.validate(opts)
  errors = []
  errors << "No channel or app given" unless (opts[:channel_given] || opts[:app_given])
  errors << "No authentication information given" unless (opts[:api_key_given] && opts[:api_secret_given])
  errors << "Cannot use both 'follow' and 'lines'" if (opts[:follow_given] && opts[:lines_given])
  errors << "Cannot use both 'app' and 'channel'" if (opts[:channel_given] && opts[:app_given])
  errors << "Lines subcommand does not support montoring of Apps at this moment" if (opts[:lines_given] && opts[:app_given])
  errors
end