class DBRotatorConfig

Constants

CONFIG
EXCLUDE_FROM_GENERATE_FILE
FILE
REQUIRED

Attributes

config[R]

Public Class Methods

new() click to toggle source
# File lib/db_rotator_config.rb, line 33
def initialize
  @config = {}
  @generate_output_file = nil
end

Public Instance Methods

add_default_values() click to toggle source
# File lib/db_rotator_config.rb, line 123
def add_default_values
  CONFIG.each do |key, pair|
    @config[key] ||= pair.last
  end
end
add_derived_values() click to toggle source
# File lib/db_rotator_config.rb, line 129
def add_derived_values
  # Figure out the dump filename
  cmd = if @config[:scp_command].strip.match(/^scp/)
          @config[:scp_command].split(':').last
        else
          @config[:scp_command]
        end

  pn = Pathname.new(cmd)
  @config[:dump_filename] = pn.basename.to_s
end
check_required() click to toggle source
# File lib/db_rotator_config.rb, line 111
def check_required
  REQUIRED.each { |k| raise "config option '#{k.to_s} is required'" if @config[k].nil? }
end
cli_parser() click to toggle source
# File lib/db_rotator_config.rb, line 75
def cli_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: db-rotator [options]"

    CONFIG.each do |key, pair|
      o = pair.first
      if o[3].nil?
        opts.on(o[0], o[1], o[2]) do |v|
          @config[key] = v
        end
      else
        opts.on(o[0], o[1], o[2], o[3]) do |v|
          @config[key] = v
        end
      end
    end

    opts.on("-g", "--generate-config FILE", "Generates .yml config file from given arguments.") do |v|
      @generate_output_file = v
    end

    opts.on("-v", "--verbose") do |v|
      @config[:verbose] = v
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end
end
config_syspath() click to toggle source
# File lib/db_rotator_config.rb, line 107
def config_syspath
  @config[:config_file] || [ENV['HOME'], FILE].join('/')
end
config_yaml() click to toggle source
# File lib/db_rotator_config.rb, line 119
def config_yaml
  @config.reject { |k, _| EXCLUDE_FROM_GENERATE_FILE.include?(k) } .to_yaml
end
configure() click to toggle source
# File lib/db_rotator_config.rb, line 38
def configure
  begin
    ARGV.empty? ? from_file : from_cli

    if @config[:config_file]
      from_file
    end

    check_required
    add_default_values
    add_derived_values

    if @generate_output_file
      generate_config
      exit
    end

  rescue FileNotFoundError => e
    puts "There was a problem loading configuration: #{e.message}"
    puts cli_parser.summarize
    exit
  end
end
from_cli() click to toggle source
# File lib/db_rotator_config.rb, line 62
def from_cli
  cli_parser.parse!(ARGV)
end
from_file() click to toggle source
# File lib/db_rotator_config.rb, line 66
def from_file
  raise FileNotFoundError, "no such config file -- #{config_syspath}" unless File.exists?(config_syspath)
  @config = YAML.load_file(config_syspath).each.with_object({}) { |(k, v), h| h[k.to_sym] = v }

  if !(missing = REQUIRED.delete_if { |k| @config[k] }).empty?
    raise "please set config option(s) in #{config_syspath}: #{missing.join(', ')}"
  end
end
generate_config() click to toggle source
# File lib/db_rotator_config.rb, line 115
def generate_config
  File.write(@generate_output_file, config_yaml)
end