Module: RDBr::CLI

Defined in:
lib/rdbr/cli.rb

Class Method Summary collapse

Class Method Details

.configure_web(options, catalog_reader, query_builder) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rdbr/cli.rb', line 56

def configure_web(options, catalog_reader, query_builder)
  catalog = read_catalog(options, catalog_reader)
  access_policy = Access::ReadOnly.new(statement_timeout: options.fetch(:statement_timeout))
  query_service = query_builder.call(
    database_url: options.fetch(:database_url),
    catalog: catalog,
    connection_options: options.slice(
      :statement_timeout, :pool_size, :checkout_timeout, :max_value_bytes
    ),
    access_policy: access_policy,
    max_results: options.fetch(:max_results)
  )
  Web::Application.set :catalog, catalog
  Web::Application.set :query_service, query_service
  Web::Application.set :access_policy, access_policy
  query_service
end

.read_catalog(options, catalog_reader) ⇒ Object



49
50
51
52
53
54
# File 'lib/rdbr/cli.rb', line 49

def read_catalog(options, catalog_reader)
  catalog_reader.call(
    database_url: options.fetch(:database_url),
    include_system: options.fetch(:include_system)
  )
end

.run(arguments, output: $stdout, error: $stderr, **dependencies) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rdbr/cli.rb', line 7

def run(
  arguments,
  output: $stdout,
  error: $stderr,
  **dependencies
)
  catalog_reader = dependencies.fetch(:catalog_reader, Catalog.method(:read))
  query_builder = dependencies.fetch(:query_builder, Query.method(:build))
  server_starter = dependencies.fetch(:server_starter, Rackup::Server.method(:start))
  browser_launcher = dependencies.fetch(:browser_launcher, BrowserLauncher)
  services = { catalog_reader: catalog_reader, query_builder: query_builder,
               server_starter: server_starter, browser_launcher: browser_launcher }
  command, options = CLIOptions.parse(arguments)
  case command
  when 'inspect'
    output.puts Catalog::Serializer.pretty_json(read_catalog(options, catalog_reader))
  when 'serve'
    serve(options, services, output)
  when 'version'
    output.puts VERSION
  else
    error.puts "Unknown command: #{command}"
    return 1
  end
  0
rescue Catalog::Error, Query::Error, OptionParser::ParseError, ArgumentError => e
  error.puts e.message
  1
end

.serve(options, services, output) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rdbr/cli.rb', line 37

def serve(options, services, output)
  if options[:database_url]
    query_service = configure_web(options, services.fetch(:catalog_reader), services.fetch(:query_builder))
  end
  if options[:open_browser]
    services.fetch(:browser_launcher).open_when_ready(host: options[:host], port: options[:port], output: output)
  end
  services.fetch(:server_starter).call(app: Web::Application, Host: options[:host], Port: options[:port])
ensure
  query_service.disconnect if query_service.respond_to?(:disconnect)
end