module Dag module CLI class Base < Thor include Dag::CLI::Utils::TerminalTable private attr_accessor :debug, :trace def local_store(&block) require "yaml/store" db = YAML::Store.new("#{File.dirname(options[:config])}/.cstore") db.transaction { block.call(db) } end def open_client(&block) cluster = block_given? ? block.call : local_store { |db| db[:cluster] } client.open(cluster) end def client(path = options[:config]) require 'oj' require 'dag' Oj.default_options = { mode: :compat } # create_config_directory if File.exist?(path) begin config = Oj.load(File.open(path, &:read)) rescue Oj::ParseError => ex abort "Error: Json format is invalid.\n #{ex.message}" end end if !config.nil? && valid?(config) self.debug = config["debug"] self.debug = options["debug"] unless options["debug"].nil? self.trace = options["trace"] options = { force_path_style: config["force_path_style"] || false, debug: self.debug || false } options.merge!(analysis_api: config["analysis_api"]) if config["analysis_api"] options.merge!(storage_api: config["storage_api"]) if config["storage_api"] return Dag::Client.new(config["access_key_id"], config["secret_access_key"], options) else error "Error: Please create config file to '#{path}'" if config.nil? abort <<-EOS { "access_key_id" : "YOUR ACCESS KEY ID", "secret_access_key" : "YOUR SECRET ACCESS KEY", "analysis_api" : "ANALYSIS API ENDPOINT", "storage_api" : "STORAGE API ENDPOINT" } EOS end end # def create_config_directory # begin # Dir.mkdir("#{Dir.home}/.dag") # rescue Errno::EEXIST # rescue => ex # error "Error: #{ex.message}" # exit 1 # end # end def valid?(config) unless config.key?("access_key_id") error "Error: Missing access_key_id. Please check your configuration." return false end unless config.key?("secret_access_key") error "Error: Missing secret_access_key. Please check your configuration." return false end true end def handle_api_failure(&block) begin block.call rescue Dag::Client::APIFailure => ex if self.debug require 'pp' PP.pp({ api_code: ex.api_code, api_message: ex.api_message, api_status: ex.api_status, api_request_id: ex.api_request_id, api_resource: ex.api_resource, message: ex.message }, STDERR ) end if ex.api_code && ex.api_message say_status(ex.api_code, "#{ex.api_message} (RequestId: #{ex.api_request_id})", :red) if ex.api_status == 500 error "An unexpected error has occurred. Please contact customer support." end else say_status( ex.cause, "#{ex.cause}: An unexpected error has occurred. Please contact customer support.", :red ) end exit 1 rescue => ex say_status("ERROR", ex.message, :red) error ex.backtrace * "\n" if self.trace exit 1 end end end end end