#!/usr/bin/env ruby $: << File.expand_path("#{File.dirname __FILE__}/../lib") require 'rubygems' require 'kumogata' require 'kumogata/argument_parser' options = nil parsed = Kumogata::ArgumentParser.parse! {|parser, cmd, args, opts| if (opts.access_key_id? and not opts.secret_access_key?) or (not opts.access_key_id? and opts.secret_access_key?) puts parser.help exit 1 end } begin command, arguments, options, output_result = parsed String.colorize = options.color? Diffy::Diff.default_format = options.color? ? :color : :text aws_opts = {} # Set config file path if options.config_path? unless File.exist?(options.config_path) raise "No such config file: #{options.config_path}" end AWSConfig.config_file = options.config_path end # Load .aws/config # See http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html if File.exist?(AWSConfig.config_file) unless AWSConfig.has_profile?(options.config_profile) raise "No profile : #{AWSConfig.config_file} => #{options.config_profile}" end { 'aws_access_key_id' => :access_key_id, 'aws_secret_access_key' => :secret_access_key, 'region' => :region, }.each do |config_key, option_key| option_value = AWSConfig[options.config_profile][config_key] aws_opts[option_key] = option_value if option_value end end [:access_key_id, :secret_access_key, :region].each do |key| aws_opts[key] = options[key] if options[key] end AWS.config(aws_opts) unless aws_opts.empty? if options.debug? Kumogata.logger.set_debug(true) AWS.config({ :http_wire_trace => true, :logger => Kumogata.logger, }) end out = Kumogata::Client.new(options).send(command, *arguments) if output_result and out puts out end rescue Exception => e $stderr.puts("[ERROR] #{e.message}".red) unless e.kind_of?(Interrupt) if options and options.debug? raise e else backtrace = Kumogata::Utils.filter_backtrace(e.backtrace) unless backtrace.empty? $stderr.puts " from #{backtrace.first}".red end end exit 1 end