require 'net/ssh/gateway' module Arql class App class << self attr_accessor :log_io def config @@effective_config end def local_ssh_proxy_port @@local_ssh_proxy_port end end def initialize(options) require 'active_support/all' require 'active_record' require "arql/connection" require "arql/definition" @options = options Connection.open(connect_options) @definition = Definition.new(effective_config) load_initializer! end def connect_options connect_conf = effective_config.slice(:adapter, :host, :username, :password, :database, :encoding, :pool, :port, :socket) if effective_config[:ssh].present? connect_conf.merge!(start_ssh_proxy!) end connect_conf end def load_initializer! return unless effective_config[:initializer] unless File.exists?(effective_config[:initializer]) STDERR.puts "Specified initializer file not found, #{effective_config[:initializer]}" exit(1) end load(effective_config[:initializer]) end def start_ssh_proxy! ssh_config = effective_config[:ssh] @ssh_gateway = Net::SSH::Gateway.new(ssh_config[:host], ssh_config[:user], ssh_config.slice(:port, :password).symbolize_keys) @@local_ssh_proxy_port = @ssh_gateway.open(effective_config[:host], effective_config[:port], ssh_config[:local_port]) { host: '127.0.0.1', port: @@local_ssh_proxy_port } end def config @config ||= YAML.load(IO.read(@options.config_file)).with_indifferent_access end def selected_config if @options.env.present? && !config[@options.env].present? STDERR.puts "Specified ENV `#{@options.env}' not exists" end config[@options.env] end def effective_config @@effective_config ||= selected_config.deep_merge(@options.to_h) end def run! show_sql if should_show_sql? write_sql if should_write_sql? append_sql if should_append_sql? if effective_config[:code].present? eval(effective_config[:code]) elsif effective_config[:args].present? effective_config[:args].each { |rb| load(rb) } elsif STDIN.isatty run_repl! else eval(STDIN.read) end end def run_repl! Repl.new end def should_show_sql? effective_config[:show_sql] end def should_write_sql? effective_config[:write_sql] end def should_append_sql? effective_config[:append_sql] end def show_sql App.log_io ||= MultiIO.new ActiveRecord::Base.logger = Logger.new(App.log_io) App.log_io << STDOUT end def write_sql write_sql_file = effective_config[:write_sql] App.log_io ||= MultiIO.new ActiveRecord::Base.logger = Logger.new(App.log_io) App.log_io << File.new(write_sql_file, 'w') end def append_sql write_sql_file = effective_config[:append_sql] App.log_io ||= MultiIO.new ActiveRecord::Base.logger = Logger.new(App.log_io) App.log_io << File.new(write_sql_file, 'a') end end end