Class: CloneRemoteDb::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/clone_remote_db/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Loader

Returns a new instance of Loader.



6
7
8
# File 'lib/clone_remote_db/loader.rb', line 6

def initialize(script)
  @script = script
end

Instance Attribute Details

#scriptObject (readonly)

Returns the value of attribute script.



4
5
6
# File 'lib/clone_remote_db/loader.rb', line 4

def script
  @script
end

Instance Method Details



36
37
38
39
40
41
42
43
44
45
# File 'lib/clone_remote_db/loader.rb', line 36

def banner(opts)
  opts.banner = "Download and import a remote (PostgreSQL) database into a local database.\n\nUsage:\n      \#{script} [options]\n\nOptions:\n"
end

#die(key_or_msg, msg = nil, exit_code = -1)) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/clone_remote_db/loader.rb', line 26

def die(key_or_msg, msg = nil, exit_code = -1)
  if key_or_msg.is_a?(String)
    $stderr.puts "Error: #{key_or_msg}"
  else
    arg = '--' + key_or_msg.to_s.gsub('_', '-')
    $stderr.puts "Error: argument #{arg} #{msg}."
  end
  exit(exit_code)
end

#load_defaults!Object



68
69
70
71
72
73
74
75
76
# File 'lib/clone_remote_db/loader.rb', line 68

def load_defaults!
  @options = {
    local_dest: '~/pg_dumps/{remote_db}/%Y-%m/%Y-%m-%d_%H.%M.sql.gz',
    gzip_opts: %w(-9 --stdout),
    pg_dump_opts: %w(-c -O),
    exclude_table_data: %w(versions),
    remote_user: 'postgres'
  }.merge(options)
end

#opt(opts, key, msg, arg_opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/clone_remote_db/loader.rb', line 54

def opt(opts, key, msg, arg_opts = {})
  arg = "--#{key.to_s.gsub('_', '-')}"
  arg << " #{arg_opts[:arg]}" if arg_opts[:arg]
  on_args = [arg, msg]
  on_args.unshift(arg_opts[:short]) if arg_opts[:short]
  opts.on(*on_args) do |v|
    options[key] = v
  end
end

#optionsObject



64
65
66
# File 'lib/clone_remote_db/loader.rb', line 64

def options
  @options ||= {}
end

#parse_opts!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/clone_remote_db/loader.rb', line 10

def parse_opts!
  OptionParser.new do |opts|
    banner(opts)
    version(opts)
    opt(opts, :local_db, "Local postgresql database name", arg: 'DBNAME', short: '-l')
    opt(opts, :remote_db, "Remote postgresql database name", arg: 'DBNAME', short: '-r')
    opt(opts, :host, "Host of the postgresql database", arg: 'HOST', short: '-h')
    opt(opts, :import_only, "Skip download and provide path to dump.sql.gz file", arg: 'PATH')
    opt(opts, :local_dest, "The path to save the dump.sql.gz file to", arg: 'PATH')
    opt(opts, :remote_user, "Sudo as this user on the remote server", arg: 'USER')
    opt(opts, :exclude_table_data, "Do not download data from these table(s). Separate multiple tables by comma.", arg: 'TABLE')
    opt(opts, :dry_run, "Don't actually do anything", short: '-n')
  end.parse!
  options
end

#replace_variables(str) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/clone_remote_db/loader.rb', line 78

def replace_variables(str)
  new_str = str
  %w(local_db remote_db remote_user).each do |arg|
    new_str = new_str.gsub('{' + arg + '}', options[arg.to_sym])
  end
  new_str
end

#version(opts) ⇒ Object



47
48
49
50
51
52
# File 'lib/clone_remote_db/loader.rb', line 47

def version(opts)
  opts.on('--version', 'Show the version') do
    io.puts "clone_remote_db #{CloneRemoteDb::VERSION} (c) Josh McDade"
    exit(0)
  end
end