Module: RDBr::CLIOptions

Defined in:
lib/rdbr/cli_options.rb

Class Method Summary collapse

Class Method Details

.add_query_options(parser, options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rdbr/cli_options.rb', line 45

def add_query_options(parser, options)
  parser.on('--statement-timeout MS', Integer, 'Query timeout in milliseconds (default: 5000)') do |timeout|
    options[:statement_timeout] = timeout
  end
  parser.on('--pool-size COUNT', Integer, 'Database connection pool size (default: 5)') do |size|
    options[:pool_size] = size
  end
  parser.on('--checkout-timeout MS', Integer, 'Pool checkout timeout in milliseconds') do |timeout|
    options[:checkout_timeout] = timeout
  end
  parser.on('--max-value-bytes BYTES', Integer, 'Maximum bytes returned for one value') do |bytes|
    options[:max_value_bytes] = bytes
  end
  parser.on('--max-results COUNT', Integer, 'Maximum rows per page (default: 100)') do |limit|
    options[:max_results] = limit
  end
end

.defaultsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rdbr/cli_options.rb', line 16

def defaults
  {
    host: '127.0.0.1',
    port: 9292,
    database_url: ENV['DATABASE_URL'],
    include_system: false,
    open_browser: true,
    statement_timeout: ENV.fetch('RDBR_STATEMENT_TIMEOUT', 5000),
    pool_size: ENV.fetch('RDBR_POOL_SIZE', 5),
    checkout_timeout: ENV.fetch('RDBR_CHECKOUT_TIMEOUT', 5000),
    max_value_bytes: ENV.fetch('RDBR_MAX_VALUE_BYTES', 1_048_576),
    max_results: ENV.fetch('RDBR_MAX_RESULTS', 100)
  }
end

.parse(arguments) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
# File 'lib/rdbr/cli_options.rb', line 7

def parse(arguments)
  command = arguments.first&.start_with?('-') ? 'serve' : (arguments.shift || 'serve')
  options = defaults
  parser(options).parse!(arguments)
  raise ArgumentError, 'A database URL is required for inspect' if command == 'inspect' && !options[:database_url]

  [ command, options ]
end

.parser(options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rdbr/cli_options.rb', line 31

def parser(options)
  OptionParser.new do |parser|
    parser.banner = 'Usage: rdbr [serve|inspect|version] [options]'
    parser.on('--database-url URL', 'Relational database URL'){|url| options[:database_url] = url }
    parser.on('--include-system', 'Include system namespaces'){ options[:include_system] = true }
    parser.on('--host HOST', 'Host to bind (default: 127.0.0.1)'){|host| options[:host] = host }
    parser.on('--port PORT', Integer, 'Port to bind (default: 9292)'){|port| options[:port] = port }
    parser.on('--[no-]open', 'Open the browser when the server is ready (default: true)') do |open|
      options[:open_browser] = open
    end
    add_query_options(parser, options)
  end
end