Class: PgExport::ConfigurationParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_export/configuration_parser.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.helpObject



36
37
38
# File 'lib/pg_export/configuration_parser.rb', line 36

def help
  option_parser.to_s
end

.option_parser(h = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/pg_export/configuration_parser.rb', line 98

def option_parser(h = {})
  OptionParser.new do |o|
    o.banner = BANNER
    o.program_name = 'pg_export'
    o.version = PgExport::VERSION
    o.on('-g', '--gateway GATEWAY', %w[ftp ssh], O[:g]) { |v| h[:gateway] = v }
    o.on('-U', '--user USER',            String, O[:U]) { |v| h[:gateway_user] = v }
    o.on('-H', '--host HOST',            String, O[:H]) { |v| h[:gateway_host] = v }
    o.on('-P', '--password PASSWORD',    String, O[:P]) { |v| h[:gateway_password] = v }
    o.on('-d', '--database DATABASE',    String, O[:d]) { |v| h[:database] = v }
    o.on('-e', '--encryption_key KEY',   String, O[:e]) { |v| h[:encryption_key] = v }
    o.on('-a', '--algorithm ALGORITHM',  String, O[:a]) { |v| h[:encryption_algorithm] = v }
    o.on('-k', '--keep KEEP',           Integer, O[:k]) { |v| h[:keep_dumps] = v }
    o.on('-s', '--ssh',                          O[:s]) { h[:gateway] = 'ssh' }
    o.on('-f', '--ftp',                          O[:f]) { h[:gateway] = 'ftp' }
    o.on('-t', '--timestamped',                  O[:t]) { h[:logger_format] = 'timestamped' }
    o.on('-m', '--muted',                        O[:m]) { h[:logger_format] = 'muted' }
    o.on('-i', '--interactive',                  O[:i]) { h[:command] = :import_dump_interactively }
    o.on('-w', '--welcome',                      O[:w]) { h[:command] = :gateway_welcome }
    o.on('-c', '--configuration',                O[:c]) { h[:command] = :print_configuration }
    o.on('-v', '--version',                      O[:v]) { h[:command] = :print_version }
    o.on('-h', '--help',                         O[:h]) { h[:command] = :print_help }
    o.separator ''
    o.separator EXAMPLE
  end
end

.parseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pg_export/configuration_parser.rb', line 11

def parse
  h = {
    encryption_key: ENV['PG_EXPORT_ENCRYPTION_KEY'],
    encryption_algorithm: ENV['PG_EXPORT_ENCRYPTION_ALGORITHM'],
    gateway_user: ENV['PG_EXPORT_GATEWAY_USER'],
    gateway_host: ENV['PG_EXPORT_GATEWAY_HOST'],
    gateway_password: ENV['PG_EXPORT_GATEWAY_PASSWORD'],
    logger_format: ENV['PG_EXPORT_LOGGER_FORMAT'],
    keep_dumps: ENV['PG_EXPORT_KEEP_DUMPS'],
    gateway: ENV['PG_EXPORT_GATEWAY'],
    database: ENV['PG_EXPORT_DATABASE'],
    mode: ENV['PG_EXPORT_MODE'],
    command: :export_dump
  }

  option_parser(h).parse!
  h[:database] = ARGV.first unless ARGV.empty?

  Configuration.new(**h)
rescue OptionParser::ParseError => e
  error = Error.new(*e.args)
  error.reason = e.reason
  raise error
end