lib/ruby_lambda/execute.rb in ruby_lambda-0.2.2 vs lib/ruby_lambda/execute.rb in ruby_lambda-0.3.0

- old
+ new

@@ -1,32 +1,72 @@ require 'ruby_lambda/lambda_context' module RubyLambda class Execute - def initialize(current_directory) + def initialize(current_directory, options = {"config"=>"config.yml"}) @current_directory = current_directory @shell = Thor::Base.shell.new + @options = options end def run(mute: false) - config_file = "#{@current_directory}/config.yml" + @mute = mute - config_data = YAML.load_file config_file + begin + if @options.has_key?('handler') - lambda_function, lambda_handler = config_data['handler'].split('.') + lambda_function, lambda_handler = @options['handler'].split('.') + elsif @options.has_key?('config') + lambda_function, lambda_handler = load_handler_from_file + end - load "#{@current_directory}/#{lambda_function}.rb" + check_for_handler(lambda_function, lambda_handler) - event_json_file = File.read("#{@current_directory}/event.json") + load "#{@current_directory}/#{lambda_function}.rb" - event = JSON.parse(event_json_file) + event = JSON.parse(File.read("#{@current_directory}/event.json")) - context = LambdaContext.new() + context = LambdaContext.new() - if mute - send(:"#{lambda_handler}", event: event, context: context) - else - ap send(:"#{lambda_handler}", event: event, context: context) + if mute + send(:"#{lambda_handler}", event: event, context: context) + else + ap send(:"#{lambda_handler}", event: event, context: context), options = { :indent => -2 } + end + rescue LoadError + @shell.say('Handler file or function, can not be found', :red) unless @mute + + exit 1 + end + end + + def load_handler_from_file + begin + config_data = YAML.load_file "#{@current_directory}/#{@options['config']}" + + raise RubyLambda::ExecuteError.new('Invalid config file') unless config_data.is_a?(Hash) + + config_data['handler'].split('.') + rescue Errno::ENOENT + no_config_file_message = 'Config file missing, create a config.yml file or use the -c / --config flag to use a different config file. Alternativly you can use the -H flag to pass the name of the handler that should be executed' + + @shell.say(no_config_file_message, :red) unless @mute + + exit 1 + rescue RubyLambda::ExecuteError + @shell.say('Invalid config file', :red) unless @mute + + exit 1 + end + end + + def check_for_handler(lambda_function, lambda_handler) + if lambda_handler.nil? || lambda_handler.nil? + no_defined_handler_message = 'No config or handler function defined, create a config.yml file or use the -c / --config flag to use a different config file. Alternativly you can use the -H flag to pass the name of the handler that should be executed' + + @shell.say(no_defined_handler_message, :red) unless @mute + + exit 1 end end end end