Sha256: d4d9c58c2df5c2822d12e6e6a8d16833d27ae12b728e5cd7fa6b28ebd8f34d67

Contents?: true

Size: 1.86 KB

Versions: 23

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require 'phobos/cli/runner'

module Phobos
  module CLI
    class Start
      def initialize(options)
        @config_file = File.expand_path(options[:config]) unless options[:skip_config]
        @boot_file = File.expand_path(options[:boot])

        @listeners_file = File.expand_path(options[:listeners]) if options[:listeners]
      end

      def execute
        load_boot_file

        if config_file
          validate_config_file!
          Phobos.configure(config_file)
        end

        Phobos.add_listeners(listeners_file) if listeners_file

        validate_listeners!

        Phobos::CLI::Runner.new.run!
      end

      private

      attr_reader :config_file, :boot_file, :listeners_file

      def validate_config_file!
        File.exist?(config_file) || error_exit("Config file not found (#{config_file})")
      end

      def validate_listeners! # rubocop:disable Metrics/MethodLength
        Phobos.config.listeners.each do |listener|
          handler = listener.handler

          begin
            handler.constantize
          rescue NameError
            error_exit("Handler '#{handler}' not defined")
          end

          delivery = listener.delivery
          if delivery.nil?
            Phobos::CLI.logger.warn do
              Hash(message: "Delivery option should be specified, defaulting to 'batch'"\
                ' - specify this option to silence this message')
            end
          elsif !Listener::DELIVERY_OPTS.include?(delivery)
            error_exit("Invalid delivery option '#{delivery}'. Please specify one of: "\
              "#{Listener::DELIVERY_OPTS.join(', ')}")
          end
        end
      end

      def error_exit(msg)
        Phobos::CLI.logger.error { Hash(message: msg) }
        exit(1)
      end

      def load_boot_file
        load(boot_file) if File.exist?(boot_file)
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
phobos-2.1.6 lib/phobos/cli/start.rb
phobos-2.1.5 lib/phobos/cli/start.rb
phobos-2.1.4 lib/phobos/cli/start.rb
phobos-2.1.3 lib/phobos/cli/start.rb
phobos-2.1.2 lib/phobos/cli/start.rb
phobos-2.1.1 lib/phobos/cli/start.rb
phobos_temp_fork-0.0.4 lib/phobos/cli/start.rb
phobos_temp_fork-0.0.3 lib/phobos/cli/start.rb
phobos_temp_fork-0.0.2 lib/phobos/cli/start.rb
phobos_temp_fork-0.0.1 lib/phobos/cli/start.rb
phobos-2.1.0 lib/phobos/cli/start.rb
phobos-2.0.2 lib/phobos/cli/start.rb
phobos-2.0.1 lib/phobos/cli/start.rb
phobos-2.0.0.pre.beta1 lib/phobos/cli/start.rb
phobos-1.9.0 lib/phobos/cli/start.rb
phobos-1.9.0.pre.beta3 lib/phobos/cli/start.rb
phobos-1.9.0.pre.beta2 lib/phobos/cli/start.rb
phobos-1.9.0.pre.beta1 lib/phobos/cli/start.rb
phobos-1.8.3.pre.beta2 lib/phobos/cli/start.rb
phobos-1.8.3.pre.beta1 lib/phobos/cli/start.rb