Sha256: 916b7050fa41435525d5a02cd5fc73bd9e22fa1845c19f795155cf7556cf259f

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module Dexter
  class Client
    attr_reader :arguments, :options

    def initialize(args)
      @arguments, @options = parse_args(args)
    end

    def perform
      STDOUT.sync = true
      STDERR.sync = true

      if options[:statement]
        fingerprint = PgQuery.fingerprint(options[:statement]) rescue "unknown"
        query = Query.new(options[:statement], fingerprint)
        Indexer.new(arguments[0], options).process_queries([query])
      elsif arguments[1]
        Processor.new(arguments[0], arguments[1], options).perform
      else
        Processor.new(arguments[0], STDIN, options).perform
      end
    end

    def parse_args(args)
      opts = Slop.parse(args) do |o|
        o.banner = %(Usage:
    dexter <database-url> [options]

Options:)
        o.boolean "--create", "create indexes", default: false
        o.array "--exclude", "prevent specific tables from being indexed"
        o.integer "--interval", "time to wait between processing queries, in seconds", default: 60
        o.float "--min-time", "only process queries that have consumed a certain amount of DB time, in minutes", default: 0
        o.string "--log-level", "log level", default: "info"
        o.boolean "--log-sql", "log sql", default: false
        o.string "-s", "--statement", "process a single statement"
        o.on "-v", "--version", "print the version" do
          log Dexter::VERSION
          exit
        end
        o.on "-h", "--help", "prints help" do
          log o
          exit
        end
      end

      arguments = opts.arguments

      if arguments.empty?
        log opts
        exit
      end

      abort "Too many arguments" if arguments.size > 2

      abort "Unknown log level" unless ["info", "debug", "debug2"].include?(opts.to_hash[:log_level].to_s.downcase)

      [arguments, opts.to_hash]
    rescue Slop::Error => e
      abort e.message
    end

    def log(message)
      $stderr.puts message
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pgdexter-0.1.3 lib/dexter/client.rb
pgdexter-0.1.2 lib/dexter/client.rb