Class: Msgr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/msgr/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CLI

Returns a new instance of CLI


7
8
9
10
11
12
13
14
15
16
# File 'lib/msgr/cli.rb', line 7

def initialize(options)
  @options = options

  if !File.exist?(options[:require]) ||
      (File.directory?(options[:require]) && !File.exist?("#{options[:require]}/config/application.rb"))
    raise <<~ERR
      Rails application or required ruby file not found: #{options[:require]}
    ERR
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


5
6
7
# File 'lib/msgr/cli.rb', line 5

def options
  @options
end

Class Method Details

.run(argv) ⇒ Object


58
59
60
# File 'lib/msgr/cli.rb', line 58

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

Instance Method Details

#runObject


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
# File 'lib/msgr/cli.rb', line 18

def run
  ENV['RACK_ENV'] = ENV['RAILS_ENV'] = options[:environment]

  if File.directory?(options[:require])
    require 'rails'
    if ::Rails::VERSION::MAJOR == 4
      require File.expand_path("#{options[:require]}/config/application.rb")
      ::Rails::Application.initializer "msgr.eager_load" do
        ::Rails.application.config.eager_load = true
      end
      require 'msgr/railtie'
      require File.expand_path("#{options[:require]}/config/environment.rb")
    else
      require 'msgr/railtie'
      require File.expand_path("#{options[:require]}/config/environment.rb")
    end
  else
    require(options[:require])
  end

  r, w = IO.pipe

  Signal.trap('INT') { w.puts 'INT' }
  Signal.trap('TERM') { w.puts 'TERM' }

  Msgr.logger = Logger.new(STDOUT)
  Msgr.client.start

  while readable = IO.select([r])
    case readable.first[0].gets.strip
      when 'INT', 'TERM'
        Msgr.client.stop
        break
      else
        exit 1
    end
  end
end