class Object

Public Instance Methods

evaluate_command(driver, cmd_arg) click to toggle source
# File lib/ddbcli/cli/functions.rb, line 69
def evaluate_command(driver, cmd_arg)
  cmd, arg = cmd_arg.split(/\s+/, 2).map {|i| i.strip }
  arg = nil if (arg || '').strip.empty?

  r = /\A#{Regexp.compile(cmd)}/i

  commands = {
    'help' => lambda {
      print_help(:pagerize => true)
    },

    ['exit', 'quit'] => lambda {
      exit 0
    },

    'timeout' => lambda {
      case arg
      when nil
        puts driver.timeout
      when /\d+/
        driver.timeout = arg.to_i
      else
        print_error('Invalid argument')
      end
    },

    'consistent' => lambda {
      if arg
        r_arg = /\A#{Regexp.compile(arg)}/i

        if r_arg =~ 'true'
          driver.consistent = true
        elsif r_arg =~ 'false'
          driver.consistent = false
        else
          print_error('Invalid argument')
        end
      else
        puts driver.consistent
      end
    },

    'iteratable' => lambda {
      if arg
        r_arg = /\A#{Regexp.compile(arg)}/i

        if r_arg =~ 'true'
          driver.iteratable = true
        elsif r_arg =~ 'false'
          driver.iteratable = false
        else
          print_error('Invalid argument')
        end
      else
        puts driver.iteratable
      end
    },

    'retry' => lambda {
      case arg
      when nil
        puts driver.retry_num
      when /\d+/
        driver.retry_num = arg.to_i
      else
        print_error('Invalid argument')
      end
    },

    'retry_interval' => lambda {
      case arg
      when nil
        puts driver.retry_intvl
      when /\d+/
        driver.retry_intvl = arg.to_i
      else
        print_error('Invalid argument')
      end
    },

    'debug' => lambda {
      if arg
        r_arg = /\A#{Regexp.compile(arg)}/i

        if r_arg =~ 'true'
          driver.debug = true
        elsif r_arg =~ 'false'
          driver.debug = false
        else
          print_error('Invalid argument')
        end
      else
        puts driver.debug
      end
    },

    'version' => lambda {
      print_version
    }
  }

  cmd_name, cmd_proc = commands.find do |name, proc|
    if name.kind_of?(Array)
      name.any? {|i| r =~ i }
    else
      r =~ name
    end
  end

  if cmd_proc
    cmd_proc.call
  else
    print_error('Unknown command')
  end
end
evaluate_query(driver, src, opts = {}) click to toggle source
# File lib/ddbcli/cli/evaluate.rb, line 1
def evaluate_query(driver, src, opts = {})
  ss = StringScanner.new(src.dup)
  buf = ''

  until ss.eos?
    if (tok = ss.scan %r{[^`'";\\/#]+}) #'
      buf << tok
    elsif (tok = ss.scan /`(?:[^`]|``)*`/)
      buf << tok
    elsif (tok = ss.scan /'(?:[^']|'')*'/) #'
      buf << tok
    elsif (tok = ss.scan /"(?:[^"]|"")*"/) #"
      buf << tok
    elsif (tok = ss.scan %r{/\*/?(?:\n|[^/]|[^*]/)*\*/})
      # nothing to do
    elsif (tok = ss.scan /--[^\r\n]*(?:\r\n|\r|\n|\Z)/)
      # nothing to do
    elsif (tok = ss.scan /#[^\r\n]*(?:\r\n|\r|\n|\Z)/)
      # nothing to do
    elsif (tok = ss.scan /(?:\\;)/)
      buf << ';' # escape of ';'
    elsif (tok = ss.scan /(?:;|\\G)/)
      src.replace(ss.rest)
      query = buf
      buf = ''

      if query.strip.empty?
        print_error('No query specified')
        next
      end

      start_time = Time.new
      out = driver.execute(query, opts.merge(:inline => (tok != '\G')))
      elapsed = Time.now - start_time

      if out.kind_of?(DynamoDB::Driver::Rownum)
        print_rownum(out, opts.merge(:time => elapsed))
      elsif out.kind_of?(String)
        puts out
      elsif out
        opts = opts.merge(:inline => (tok != '\G'), :time => elapsed)
        print_json(out, $stdout, opts)
      end
    elsif (tok = ss.scan /./)
      buf << tok # 落ち穂拾い
    end
  end

  src.replace(buf.strip)
  buf
end
parse_options() click to toggle source
# File lib/ddbcli/cli/options.rb, line 5
def parse_options
  options = OpenStruct.new
  options.access_key_id     = ENV['AWS_ACCESS_KEY_ID']
  options.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
  options.ddb_endpoint_or_region =
    ENV['AWS_REGION'] || ENV['AWS_DEFAULT_REGION'] || ENV['DDB_ENDPOINT'] || ENV['DDB_REGION'] || 'dynamodb.us-east-1.amazonaws.com'

  # default value
  options.timeout     = 60
  options.consistent  = false
  options.iteratable  = false
  options.retry_num   = 3
  options.retry_intvl = 10
  options.debug       = false

  ARGV.options do |opt|
    opt.on('-k', '--access-key=ACCESS_KEY')          {|v| options.access_key_id          = v      }
    opt.on('-s', '--secret-key=SECRET_KEY')          {|v| options.secret_access_key      = v      }
    opt.on('-r', '--region=REGION_OR_ENDPOINT')      {|v| options.ddb_endpoint_or_region = v      }

    url_opt = proc do |v|
      uri = v
      uri = "http://#{uri}" unless uri =~ %r|\A\w+://|
      uri = URI.parse(uri)
      raise URI::InvalidURIError, "invalid shceme: #{v}" unless /\Ahttps?\Z/ =~ uri.scheme
      options.ddb_endpoint_or_region = uri
    end

    opt.on('',   '--url=URL', &url_opt)
    opt.on('',   '--uri=URL (DEPRECATION)', &url_opt)

    opt.on('-e', '--eval=COMMAND')                   {|v| options.command                = v      }
    opt.on('-t', '--timeout=SECOND', Integer)        {|v| options.timeout                = v.to_i }

    opt.on('',   '--import=TABLE,JSON_FILE') {|v|
      v = v.split(/\s*,\s*/, 2)
      options.import = {:table => v[0], :file => v[1]}
    }

    opt.on('',   '--consistent-read')                {    options.consistent             = true   }
    opt.on('',   '--iteratable')                     {    options.iteratable             = true   }
    opt.on('',   '--retry=NUM', Integer)             {|v| options.retry_num              = v.to_i }
    opt.on('',   '--retry-interval=SECOND', Integer) {|v| options.retry_intvl            = v.to_i }
    opt.on('',   '--debug')                          {    options.debug                  = true   }

    opt.on('-h', '--help') {
      puts opt.help
      exit
    }

    opt.parse!

    if options.ddb_endpoint_or_region.kind_of?(URI) and not (options.access_key_id and options.secret_access_key)
      options.access_key_id = 'scott'
      options.secret_access_key = 'tiger'
      puts 'Warning: dummy auth key was set because ACCESS_KEY/SECRET_KEY is not set'
    end

    unless options.access_key_id and options.secret_access_key and options.ddb_endpoint_or_region
      puts opt.help
      exit 1
    end
  end

  options
end
print_error(errmsg, opts = {}) click to toggle source
print_help(options = {}) click to toggle source
print_json(data, out, opts = {}) click to toggle source
print_rownum(data, opts = {}) click to toggle source
print_version() click to toggle source