Module: Configuration

Included in:
Apprentice::Sentinel
Defined in:
lib/apprentice/configuration.rb

Instance Method Summary collapse

Instance Method Details

#get_configObject



5
6
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/apprentice/configuration.rb', line 5

def get_config
  options = OpenStruct.new
  options.ip = '0.0.0.0'
  options.port = 3307
  options.sql_port = 3306
  options.accept_donor = false

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: apprentice [options]\n"
    opts.separator ''
    opts.separator 'Specific options:'

    opts.on('-s SERVER', '--server SERVER',
      'Connect to SERVER') { |s| options.server = s }
    opts.on('-u USER', '--user USER',
      'USER to connect the server with') { |u| options.user = u }
    opts.on('-p PASSWORD', '--password PASSWORD',
      'PASSWORD to use') { |p| options.password = p }

    opts.on('-i', '--ip IP',
      'Local IP to bind to') { |i| options.ip = i }
    opts.on('--port PORT',
      'Local PORT to use') { |p| options.port = p }
    opts.on('--sql_port PORT',
      'Port of the MariaDB server to connect to') { |p| options.sql_port = p }
    opts.on('--[no-]accept-donor',
      'Accept cluster state "Donor/Desynced" as valid') { |ad| options.accept_donor = ad }

    opts.separator ''
    opts.separator 'Common options:'

    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit
    end
    opts.on_tail('-v', '--version', 'Show version') do
      puts "Apprentice #{Apprentice::VERSION}"
      exit
    end
  end

  begin
    ARGV << 's-h' if ARGV.size < 3
    opt_parser.parse!(ARGV)
    unless options.server && options.user && options.password
      $stderr.puts 'Error: you have to specify a user, a password and the server to connect to'
      $stderr.puts 'Try -h/--help for more options'
      exit
    end
    return options
  rescue OptionParser::ParseError
    $stderr.print "Error: #{$!}\n"
    exit
  end
end